Hi, and thanks for your answer.
> > When I import the DLL from a program, I can read the global variables
> > (e.g. CSC_FFSW_U), but when I try to write them I get a Segment
> > Violation exception.
>
> How do you "write them"?
> How are they declared to the C compiler?
I declare the variable in a header.h as:
extern myType CSC_FFSW_U;
In the DLL implementation, I define the actual variable:
#include "header.h"
myType CSC_FFSW_U;
In the code that uses the DLL, I import the header and use the variable as a regular value, not a pointer.
#include "header.h"
double k = CSC_FFSW_U.field; // this works
CSC_FFSW_U.field = 2.0; // this causes a segment violation
With the alternative of GetProcAddress, I do use pointers:
myType* pCSC_FFSW_U = (myType*)GetProcAddress(dllHandle, "_CSC_FFSW_U");
double k = pCSC_FFSW_U->field; // this works
pCSC_FFSW_U->field = 2.0; // this also works
> Win32 DLL-shared variables are really pointers (much like functions are
> in C); when reading, things works smoothly because the compiler silently
> perform the indirection; but on writing, you need to perform explicit
> dereference. One way is to do it is to code explicitly
> *CSC_FFSW_U = 1;
Does this mean that the variable should be accessed as a pointer regardless of its declaration in header.h?
> but for more than 20 years now, the solution uses to be to "decorate"
> the declaration of the variable with __declspec(dllimport); then the
> compiler will insert the required code.
Unfortunately I'm working with generated code (from Matlab) and I can't modify it to add decorations. That's why I took this complicated approach.
Thanks a lot,
Miguel