Click here to Skip to main content
15,887,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone I need some help .
So i have a project with few dlls and i need to get a variable from one dll in other one. My question is what is the best practice for this. I should share one class maybe function and a enum between the dlls . I read some stuff about pragmas , export (which i don't like ) extern and namespaces.
I should say once the variable in the second dll is changed the first one should work with the changed value. I don't want only to read it but to access it.
So according to you which is the better way to share variables between dlls and why?
Thx for the help
Posted
Comments
Argonia 26-Jul-12 7:13am    
Thx to all for your answers i decided to make it like pasztorpisti suggested ,but a little bit different. I just made the real function ,which is doing all the hard work in the same dll where it has access to the variable . I exported it and just made in the other dll other function , which takes the parameters calls the new function from the first dll and gives the params to it.

Use function calls either stand-alone or members of classes to pass variables between different parts of the code. Avoid global variables and shared memory as they often lead to obscure and difficult to diagnose problems.
 
Share this answer
 
If the DLLs are allowed to "know" each other and it doesn't bother that they have a link time dependency you can simply use a global variable that you declare in one of the DLLs and refer to it in the others. Let the linker and the DLL loader do all the hard work.
 
Share this answer
 
Comments
pasztorpisti 25-Jul-12 19:14pm    
Does this work in both cl.exe and g++ (maybe llvm), or just VC++?
nv3 26-Jul-12 3:41am    
Never tried it on anything other than Windows and VC++. It should actually work on anything you run on Windows and uses the Windows PE format and DLL loader. As far as I understand the Linux linker/loader it should work there as well.
There's a good article about that:

Step by Step: Calling C++ DLLs from VC++ and VB - Part 1[^]

But, to summarize:

extern is used to use a variable outside the h/cpp file you declared.

You have to declare it TWICE. Once in the .h file (extern int itest;) and once in a cpp file (int itest;).

The classes and functions are compiled from the dll as _declspec(dllexport). and from the project as __declspec(dllimport).

To use only one file, defines are used and relative paths (../ refers to the parent folder of the file).

Finally, for the project or dll to "know" the dll, you must link the .lib file of the dll in the linker properties.

Hope it helps.
 
Share this answer
 
The 2 DLLs are in the same address space in your process so don't use magic to communicate between two DLLs! To connect them you either have to have another module (maybe the exe) that calls the functions of both DLLs and passes some kind of data retrieved by one of the DLLs to the other to connect them, or one of the DLLs have to call the other DLL to retrieve some info that makes the connection. Its just a simple function call to the DLL, you have to export just one function from your DLL!

I would choose from these 2 approaches:

C language style:
1. Declare a struct that contains all data that you want to share between the 2 dlls (in a header file you include into both DLL projects). Make sure that the struct is compiled with the same alignment in both project regardless of the possibly different compile options, its practical to put the struct in a "#pragma pack" block. DLL_1 must define this data and DLL_2 has to a function of DLL_1 that returns a pointer to this data inside DLL_1. Now DLL_1 has data and DLL_2 has a pointer ot the same data.

C++ language style:
Declare an interface that is visible by both DLLs (in a header file you inclode into both DLL projects). On interface I mean a class that has only pure virtual methods. Don't use member variables in this interface, just pure virtual methods!!! (This is very important because at compile time the two DLLs can have totally different compile options, alignment, etc... but the virtual function table is always the same!!!) DLL_1 derives a class from this interface and implements all methods, it also exports a DLL function that returns an interface pointer to an instance of the previously dervied class. DLL_2 calls the interface getter function of DLL_1 and it can reach the same object as DLL_1. The interface can contain the SetData() and GetData() methods that modify/query the same piece of data for both DLL_1 and DLL_2.

My personal favorite is the C++ style solution. Usually I export only one method from the DLL that returns an interface. If I have lots of interfaces then all other interfaces are queryied by calling the methods of the first interface retrieved by the exported dll function, this way you have to do the dll like dirty export stuff only once, further class/code additions to the DLL do not require bothering with dll exports.
 
Share this answer
 
v5
Shared memory[^] is the best option you can use. The link also has the sample code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900