How to declare a global variable in the main program so that you can access it in the dll?
2 answers
The best way to access a variable in the main program from a DLL is to do this in the library for a function that will receive the address and continue to work with the variable through that address. Example:
__declspec(dllexport) void useData(T* data) { // используем data-> ... } In the program, call this function, passing the address of the global variable:
T gData; useData(&gData); |
In summary, as I did: in the main function, exe declared the function from the dll as follows:
__declspec(dllexport) void _stdcall setHwnd(HWND*, HHOOK*); Then, in dll I got two static - variables that I will store the values I need. And further, I called this function in the main program:
setHwnd(&hwnd_main, &hCBTHook); In dll, the function looks like this:
__declspec(dllexport) void _stdcall setHwnd(HWND* hwnd, HHOOK* hhook) { hwnd_main = *hwnd; hCBTHook = *hhook; } And it works. Thank.
- Something you do not have enough parameter names. And apparently self-attribution occurs, i.e. useless operation. - αλεχολυτ
- @alexolut just when I wrote the message, did not copy the implementation of the function from the studio, but added with my hands, forgetting about the parameters) - Range
|
_exportmark (and inexternlibraries), and you can link it (automatically, which is more difficult, or manually). - nick_n_a