3

I'm new to DLL programming and I'm tryng to develop a program with some DLLs that use a single global variable defined inside the main application (.exe).

In short words, I don't understand how to import the global variable inside different DLLs and allow them to modify it.

I try to use this code to declare the resource in the .h of DLL

extern "C" __declspec(dllexport) varType varName;

but it does not seem to work, in fact different DLLs use different structures.

Also using

extern "C" __declspec(dllimport) varType varName;

the program desn't work properly.

What is the correct way to allow the sharing of the resource among main process and DLLs?

I know that share resources from main process to DLL is not right but I'm forced to do in this way. Furthermore I can't use functions that receive the pointer to the structure that I want to modify.

frankborty
  • 90
  • 9
  • 2
    https://stackoverflow.com/questions/4911994/sharing-a-global-static-variable-between-a-process-and-dll?rq=1 – TDk Sep 11 '17 at 12:24
  • 2
    While this can be done, the correct solution is to write setter/getter functions. There should exist no scenario where you need to share spaghetti globals across several DLLs. It is bad enough to use globals in one single program. – Lundin Sep 11 '17 at 13:30
  • Yes I know, but unfortunately I must implement the functions int this way because the caller program use this (terrible) behavior and I can't change it. Anyway I try to apply a setter/getter schema and avoid to share resurce. Thank you. – frankborty Sep 11 '17 at 14:04
  • this is operating system specific – Basile Starynkevitch Sep 12 '17 at 06:56
  • It is better to avoid DLLs. The main case when DLL is necessary is when you want to sell library (in DLL form). – i486 Sep 12 '17 at 08:38

0 Answers0