Passing a pointer to a struct into a dll
windows 10
visual studio 2019 community
I have an exe project with a large struct in it. I also have a dll project. I pass a pointer to the struct in the exe into the dll after loading the dll with LoadLibrary.
If I inspect the struct whilst debugging the exe it is as it should be. If I call a function in the dll I can see (in the debugger) that immediately upon calling the function the 2nd half of the struct is corrupted (the data changes). Immediately upon the dll function returning to the exe I can see (in the debugger) that the corrupted data returns to what it should be.
It's my understanding that the exe and dll share the same address space, so why is the data in the struct corrupted when inside the dll and why does it return to normal when returning to the dll? Can I mark the struct in some way to make it stay valid when inside the dll? All I can think of is to copy the struct into the dll instead of passing a pointer but I'd rather not because the data in the struct changes over time.
In exe:
Code:
struct FOO
{
...
};
FOO foo =
{
...
};
In dll:
Code:
FOO* dllFoo = NULL;
dllFoo is exported in a def file, it's address is obtained using GetProceAddress, and a pointer to foo is copied into it. All this must be ok because the first half of the struct is correct when inside the dll (it's complex data not all zeros, and it's correct up to a point).