GetProcAddress() failing, error 127

Moon picture Moon · Sep 16, 2014 · Viewed 15.9k times · Source

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

Answer

egur picture egur · Sep 16, 2014

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");