Hi, I have seen examples of using typedefs for function pointers, but I cannot get the following code to compile without "error C2440: 'type cast' : cannot convert from 'FARPROC' to 'MyLibFuncFnPtr'"
The DLL is a C++ wrapper for a C# .Net managed assembly, which is called from a C++ unmanaged application. I am trying to manually load an instance of the C++ wrapper DLL and get the wrapper function pointer, using GetProcAddress, but I can't get past the compile error. I must be defining the typedef incorrectly, but I don't know how.
// Typedef for member function pointer to DLL lib function
typedef void ( __stdcall MyOrg::ABC::MyClass::*MyLibFuncFnPtr )(std::wstring, std::wstring );
static MyLibFuncFnPtr g_pfnMyLibFunc = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hInst = LoadLibrary(L"C:\\MyLib.dll");
if ( hInst)
{
// This generates "error C2440: 'type cast' : cannot convert from 'FARPROC' to 'MyLibFuncFnPtr'"
g_pfnMyLibFunc = (MyLibFuncFnPtr)GetProcAddress(hInst, "MyOrg::ABC::MyClass::MyLibFunc");
if (g_pfnMyLibFunc)
{
//GetProcAddress succeeded
}
}
return 0;
}
There's a more serious problem than that compiler error.
Because of name mangling, the function that's exported in the DLL will look nothing like what you have there. For one, the function's return and parameter types will be embedded in the name. Different compilers and even different compiler versions might generate different names. There's no way of knowing for sure what a function will C++ linkage will be mangled to, which is why C linkage is used when it comes to dynamic linking.
In other words, you can't export member functions to DLLs.
The alternative is compiling the DLL and reading it to see what the symbol is actually named. Not a very pretty thing to do, really, and it also breaks easily.