Reputation: 73
Let's say I have the following definition of a struct in a DLL:
typedef struct {
double varOne;
double varTwo;
} structOne;
structOne myStruct;
It is then possible to do the following from the main application which loads the DLL:
structOne * sPtr = (structOne *)GetProcAddress(libraryHandle, "myStruct");
My question is if it would be possible to do something of the likes of:
double * dPtr = (double *)GetProcAddress(libraryHandle, "myStruct.varOne");
Regards and hopeful for answers!
Upvotes: 1
Views: 2176
Reputation: 10557
No, this is not possible. GetProcAddress
has access only to the dynamic linker info. Information about the layout of the classes/structures is part of the compiler info. Compiler places this info into PDB files. It is not directly present in the binary modules. GetProcAddress
has access only to the info that is stored in the EXE/DLL files. PDB files are primarily used by debuggers with a really few exceptions like StackWalk
.
Upvotes: 3
Reputation: 180235
The offset itself (0) is implicitly present in the DLL, but the symbol corresponding to that offset (".varOne"
) isn't.
Workaround:
struct member_t { char const* type; char const* member; size_t offset };
#define MEMBER(T, M) {#T, #M, offset_of(T, M) }
member_t exports[] = {
MEMBER(myStruct, varOne)
};
Upvotes: 0
Reputation: 5341
As mentioned in other answers, this is not possible.
To solve your problem, you could export the struct definition from the DLL, export a global function that returns the address of your global variable. From the exe, import this function and call this to get the pointer to the global struct object defined in the dll. Code follows...
Add a structOne.h file and move the structure definition to that file. Modify the definition as this
#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif
struct TESTDLL_API structOne{
double varOne;
double varTwo;
} ;
Add the C++ pre-processor macro TESTDLL_EXPORTS to your DLL.
define a global function like this in the structOne.cpp file and export it too.
structOne myStruct; // your global variable
TESTDLL_API structOne* getStruct(){return &myStruct;}
Then build the DLL.
From the .exe, use the following code to call the method. Also, include the structOne.h header file
typedef structOne* (*getStruct)();
HMODULE libraryHandle = ::LoadLibraryA("TestDLL.dll");
getStruct fn = (getStruct)::GetProcAddress(libraryHandle, "getStruct");
structOne* s = (*fn)();
Upvotes: 1
Reputation: 6324
As matter of fact it is NOT possible, BECAUSE the Functions (Symbols) accessible using GetProcAddress are ONLY the one exported by the Export Address Table!
Upvotes: 1