End Google Ads 201810 - BS.net 01 --> Hi again,

I am using data_segs to share an LPSTR variable but the sharing only seems to work with multiple instances of the same EXE - literally. When I retrieve the value of the var from another app, a different EXE, I get the default value of the variable originally initialized in the module.

Code in DLL:

#pragma data_seg("Shared") __declspec(dllexport) LPSTR lpsCommandParameter = NULL; #pragma data_seg() #pragma comment(linker,"/SECTION:Shared,S") extern "C" { __declspec(dllexport) VOID SetCommandParameter(LPSTR lpsParam); __declspec(dllexport) LPSTR GetCommandParameter(); } VOID SetCommandParameter(LPSTR lpsParam) { lpsCommandParameter = lpsParam; } LPSTR GetCommandParameter() { return lpsCommandParameter; }
Code in Form1.cpp (Form1.exe) - Button1_Click - This button sets the value of lpsCommandParameter to "Hello"
typedef VOID (*pSetCommandParameter)(char *lpsParam); HINSTANCE DllHandle = LoadLibraryW(L"my.dll"); if (DllHandle == NULL) { ShowMessage("NULL"); return; } pSetCommandParameter pSetParam = (pSetCommandParameter)GetProcAddress(DllHandle, "SetCommandParameter"); if (pSetParam == NULL) { ShowMessage("pSetParam is NULL"); return; } pSetParam("Hello");

Code in Form1.cpp (Form1.exe) - Button2_Click - This one retrieves the value of lpsCommandParameter:

typedef char *( *pGetCommandParameter )(); HINSTANCE DllHandle = LoadLibraryW(L"my.dll"); if (DllHandle == NULL) { ShowMessage("NULL"); return; } pGetCommandParameter pGetParam = (pGetCommandParameter)GetProcAddress(DllHandle, "GetCommandParameter"); if (pGetParam == NULL) { ShowMessage("pGetParam is NULL"); return; } ShowMessage(pGetParam());

Now the sharing works fine as long as multiple instances of that same Form1.exe are instantiated. Put the Button2 code in a second application, say Form2.exe and click the button to retrieve the value of lpsCommandParameter and you'll get a blank. I instantiated 4 instances of Form1.exe and the value of lpsCommandParameter from the DLL is returned fine but I keep getting blanks from a different EXE. I want this data to be visible among different EXEs as my project depends on shared data and I don't want to write anything on disk so I really want do this via data sharing in DLLs.