Here's my DLL code:
#include <Windows.h>
#include <iostream>
int sysLol(char *arg);
int sysLol(char *arg)
{
std::cout<<arg<<"\n";
return 1;
}
And here's my application code:
#include <Windows.h>
#include <iostream>
#include <TlHelp32.h>
#include <stdlib.h>
typedef int (WINAPI* Lol)(char* argv);
struct PARAMETERS
{
DWORD Lol;
};
int main()
{
PARAMETERS testData;
HMODULE e = LoadLibrary(L"LIB.dll"); //This executes without problem
if (!e) std::cout<<"LOADLIBRARY: "<<GetLastError()<<"\n";
else std::cout<<"LOADLIBRARY: "<<e<<"\n";
testData.Lol = (DWORD)GetProcAddress(e,"sysLol"); //Error 127?
if (!testData.Lol) std::cout<<testData.Lol<<" "<<GetLastError()<<"\n";
else std::cout<<"MESSAGEBOX: "<<testData.Lol<<"\n";
std::cin.ignore();
return 1;
}
So, my LIB.dll is successfully loaded using LoadLibrary()
, yet GetProcAddress()
fails with 127.
This seems to be because it's not finding my function name, but I don't see why that would fail.
Assistance is greatly appreciated! :) ~P
Since that tag is C++, you'll need to declare a C
name for the function:
extern "C" int sysLol(char *arg);
You can see the actual name the compiler gave your C++ function with Dependency Walker.
When successful, cast the function to pointer returned by GetProcAddress to the actual function type:
typedef int (*sysLol_t)(char *arg);
sysLol_t pFunc = GetProcAddress(e,"sysLol");
That is ERROR_PROC_NOT_FOUND
which means that there is no exported function with that name.
There's not much more to say. Perhaps you've got the name wrong. It could be a simple mismatch of letter case. Perhaps the DLL has been built incorrectly and is not exporting the function. Perhaps the DLL is decorating or mangling the names. Certainly from the code in the question, there's no evidence that you attempted to export the function, or indeed suppress decoration/mangling.
Use a tool like dumpbin
or Dependency Walker to inspect the names of the functions that are exported. That may shed some light on the problem.
Rather than linking at runtime with LoadLibrary
and GetProcAddress
, it is much more convenient to link at load time. Use the .lib import library generated when you build the DLL to do that.
It's also worth pointing out that the calling conventions don't match. You have cdecl on the DLL side, and stdcall on the executable side. And don't cast pointers to DWORD
. That ends badly when you compile for 64 bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With