0

I need to write a DLL (with MSVS 2010 PS1) that calls a function exported from exe:

(dll.h):  extern "C" __declspec(dllimport) void*  __stdcall SomeFunction();

The problem is that void pointer can point to any datatype, and exe developers do not provide any guidelines on how to use this function. Luckily exe project is opensource and I can see that SomeFunction actually returns a pointer to a structure, which is declared in structures.h file, which is used to compile exe:

struct exeStructure {int    useme;  int usecallback; <...> };
  1. How do I use this information to set usecallback variable from DLL? Could you please give an example?
  2. How should I dereference void* SomeFunction into exeStructure?
  3. Do I need to copy and include structures.h file in my DLL project?
Buchas
  • 3
  • 2

1 Answers1

0

You should assign the value of the void * to an exeStructure * type.

You can do that via static_cast (better):

exeStructure * exeStrPtr = static_cast<exeStructure*>(voidPtr);

Or via C-style cast (essentially the same in effect for this purpose, but not advised) :

exeStructure * exeStrPtr = (exeStructure *)voidPtr;

Then you can use the proper pointer as you would expect.

exeStrPtr->usecallback = 1;
exeStrPtr->useme = 334;

For more details on the difference between the most common types of casting in C++ refer to this question:

Community
  • 1
  • 1
vordhosbn
  • 333
  • 4
  • 16