Sharing a global/static variable between a process and DLL

前端 未结 8 1618
借酒劲吻你
借酒劲吻你 2020-11-27 04:16

I\'d like to share a static/global variable only between a process and a dll that is invoked by the process. The exe and dll are in the same memory address space. I don\'t w

8条回答
  •  长情又很酷
    2020-11-27 04:40

    I have seen many answers to this question and since it is a bit tricky and unclear I would like to bring the following scenario. We want to share a global variable between a DLL and a main program, and also allow access to this variable from different modules in the DLL and in the main program.

    The variable is a BOOL indicating if the program should continue running or stop. The variable name is ShouldRun;

    In the main program we need to put:

    __declspec(dllexport)  bool ShouldRun;
    

    In the DLL's main module we need to put:

    extern "C" BOOL __declspec(dllexport) ShouldRun = TRUE;
    

    In any other module inside the DLL project we will use:

    extern  "C" BOOL ShouldRun;
    

提交回复
热议问题