I need to dynamically load a dll in C++.
I have followed this tutorial http://msdn.microsoft.com/en-us/library/ms235636.aspx to create the dll and everything worked fine.
Then I followed this one http://msdn.microsoft.com/en-us/library/64tkc9y5.aspx and I've adapted the console application as follow:
typedef DOUBLE(CALLBACK* DllFunc)(DOUBLE, DOUBLE);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hDLL; // Handle to DLL
DllFunc dllFunc1;
DOUBLE p1 = 1.0, p2 = 2.0, r;
hDLL = LoadLibrary(L"MathFuncsDLL");
if (hDLL != NULL)
{
cout << "DLL loaded: " << hDLL << endl;
dllFunc1 = (DllFunc)GetProcAddress(hDLL, "MyMathFuncs@MathFuncs@Multiply");
if (!dllFunc1)
{
// handle the error
FreeLibrary(hDLL);
cout << "Function not found!" << endl;
return -1;
}
else
{
// call the function
r = dllFunc1(p1, p2);
cout << "The result is: " << r << endl;
}
}
else {
cout << "Dll not found" << endl;
return -1;
}
cout << "Press any key to exit." << endl;
int i;
cin >> i;
return 0;
}
The DLL is loaded correctly and it is not null. The problem is the GetProcAddress() function which always return 0.
I've tried with every combination of Namespace, ClassName, Method name. I've tried to use the scope operator (::) instead of the @ in the function name.
I've tried to define the entire namespace as extern "C" but nothing changes. Every time i run or debug the console application, it cannot find the 'Multiply' function.
I think I'm missing something... Where am I wrong?
EDIT
Dependency Walker exposed me the following Export Table:
Now I wonder what the last part of the function name means... Why does the __declspec(dllexports) add those symbols?
Use a tool like dumpbin or Dependency Viewer to inspect the exported function names. This will allow you to diagnose which of the likely failure modes applies to your case: