|
C++ calling a dll.
Ok lets say we have a function we want to call in C++. We would normally do this: /* declare the function */ int MyFunction(char*,int); int main(void) { returnMyFunction("hello", 5); } /* our function */ int MyFunction(char*,int) { /* do something */ return 0; } ---------------------------------------------------------------------------------- But in our case that same function is in a dll. Ok let's see how we do it. Your dll is located on the C:\ drive for example and is named "MyDLL.dll". It contains a function called "MyFunction". Create a function that calls the dll's function like so: int CallMyDLL(void) { /* get handle to dll */ HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\MyDLL.dll"); /* get pointer to the function in the dll*/ FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MyFunction"); /* Define the Function in the DLL for reuse. This is just prototyping the dll's function. A mock of it. Use "stdcall" for maximum compatibility. */ typedef int (__stdcall * pICFUNC)(char *, int); pICFUNC MyFunction; MyFunction = pICFUNC(lpfnGetProcessID); /* The actual call to the function contained in the dll */ int intMyReturnVal = MyFunction("hello", 5); /* Release the Dll */ FreeLibrary(hGetProcIDDLL); /* The return val from the dll */ returnintMyReturnVal; } You can just copy and paste the above into your project and change the prototyping to match your dll's function. |