If you're using LoadLibrary, why are you trying to link to an exported function name?? There is no need to link anything if you're using LoadLibrary.
Look here:
Code:
#include <windows.h>
#include <iostream>
int main()
{
HMODULE hMod = LoadLibrary("mydll.dll");
if ( hMod && GetProcAddress(hMod, "MyFunctionName") != NULL )
std::cout << "The function MyFunctionName was found";
if ( hMod )
FreeLibrary( hMod );
}
There is no need to link anything from mydll. The function pointer is returned by GetProcAddress() at
runtime, so trying to find the function at link time makes no sense whatsoever. What you need to do is probably remove
all of your user-defined import libraries from your linker options. You don't need them if you're using LoadLibrary.
The only thing you need to make sure is that the "HelloWorld" function is exported, so that GetProcAddress() can find it when you call LoadLibrary on the module that contains HelloWorld.
In other words, you don't need any of this in the code where you want to call this function:
Code:
main.c:
On dll, .cpp file, on top:
__declspec(dllimport) void HelloWorld(void);
There is no "HelloWorld" function that lives in the executable you want to call it from. You have LoadLibrary, GetProcAddress, and a function pointer returned by GetProcAddress that points to an exported function. Nothing else. So trying to write a prototype for HelloWorld and including the prototype is a waste of time if that function is not there.
Within the module that
defines HelloWorld(), then yes, you would build the module with the prototype, just like any other C/C++ module. But from the module you are calling HelloWorkd from using GetProcAddress(), you should
not be prototyping HelloWorld() function, because that function doesn't exist at link time anywhere, as it will be "retrieved" at runtime using GetProcAddress.
What you want to do is define a function
pointer that is compatible with the one returned by GetProcAddress.
Code:
typedef void (*HelloWorldFn)();
//...
HelloWorldFn fn = (HelloWorldFn)GetProcAddress(hMod, "HelloWorld");
if ( fn )
fn(); // call the HelloWorld function
Something like this.
Also, I believe you should use GetModuleHandle() from the DLL, so that you get the HMODULE of the executable that is running. It serves the same purpose as LoadLibrary() without having to go through the DllMain() and other initializations that LoadLibrary() will do.
Regards,
Paul McKenzie