0

I need to share a variables (1000s) between 2 C++ Dlls. How should I do that?

MyVariables.Dll contains:

int a = 0;

ModifyMyVariables.Dll contains:

extern int a;    
a++; 
// do more stuff with a;

What am i supposed to write in the following files?

myvariables.h
myvariables.cpp
ModifyMyVariables.h
ModifyMyVariables.cpp
Oskar
  • 1,321
  • 9
  • 19
user1414260
  • 11
  • 1
  • 5

1 Answers1

1

You can share data between images (EXE, DLL...) using several fundamental mechanisms (using extern does not work to share data - it only instructs the linker and not the loader!)

  1. using import/export symbols (using the standard Import Address Table/Export Table)
  2. using static sections containing your data
  3. using dynamic sections containing your data

In your case, I would use the sections. This works pretty good. You must, of course, take care of the synchronisation when accessing (writing) these data from both sides.

mox
  • 6,084
  • 2
  • 23
  • 35
  • Hi Now I am having a problem:
    in MyVariable.cpp I wrote the following code. What do I need to do in the other files?
    #pragma data_seg(".bb") int a = 10000; #pragma data_seg() #pragma comment(linker, "/section:.bb,RWS")
    – user1414260 Jul 14 '12 at 23:48
  • See these samples: http://www.codeproject.com/Articles/240/How-to-share-a-data-segment-in-a-DLL and http://msdn.microsoft.com/en-us/library/h90dkhs0(v=vs.80).aspx – mox Jul 15 '12 at 10:19
  • dammen I still don't get it in myvariables.dll I declare a global variable in a section- #pragma data_seg(".mysecond") int test=0 ; #pragma data_seg() #pragma comment(linker, "/section:.mysecond,RWS") in ModifyMyVariables.dll I have extern test; and then test++; this won't compile: error LNK2001: unresolved external symbol "int test" (?test@@3HA). Any idea what is going wrong? – user1414260 Jul 18 '12 at 13:37
  • you don't need extern test anymore! the data are shared on the loader-basis not on the linker-basis anymore! – mox Jul 20 '12 at 06:17
  • Hi but with or wothout the extern I would get a error LNK2001: unresolved external symbol! – user1414260 Jul 27 '12 at 11:28
  • Can you share your code? I guess this is more than the snippet above. – mox Jul 27 '12 at 11:33