Reputation: 73
I have a third party dll with no lib or header file to use in my c++ app. That's why I use LoadLibrary(_T("xxx.dll")) to load it. And I can reach its functions with GetProcAddress() function.
There is a struct in this dll and I have to reach it too. What should I do to reach and use it? I have looked over the site but found just examples with c# not c++. What is the way of doing this in c++?
Thanks...
Upvotes: 5
Views: 6980
Reputation: 522
Short Answer: You can use GetProcAddress to retrieve the address of a variable declared as a struct. From the GetProcAddress man page:
Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
The name of that function should be GetSymAddress rather than GetProcAddress, but I digress.
Long Answer:
Provided that the DLL contains a symbol instance of type struct Foo:
typedef struct { int x; int array[100]; } Foo;
__declspec( dllexport ) struct Foo Bar =
{
0xdeadbeef,{0}
};
You can retrieve the address of the variable Bar provided you have already loaded the dll and have its hModule handle somewhere with:
struct Foo* Bar = GetProcAddress(hModule,"Bar");
You cannot retrieve the definition of the structure itself, but I do not think that's what you meant anyhow.
Note: I have overlooked name mangling, extern "C" declarations and maybe some other things you need to keep into consideration to make the point clear, I assume the reader already has a basic grasp of the topics at hand.
Upvotes: 2
Reputation: 6914
If you know the syntax of the struct, for example from a C# definition then you can simply add its definition to a .h file, include it in your C++ source file and use it. that's all. but if your struct is some kind of class and has member methods then you should know the decorated name of member methods, load them from .dll dynamically and call them as C functions.
Upvotes: 2