GetProcAddress() failing, error 127

17,401

Solution 1

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

Solution 2

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.

Solution 3

Symbols must be explicitly exported from Windows DLLs. Either declare your function as __declspec(dllexport) or pass a DEF file to the linker to instruct it to export your function. See this description of dllexport.

Solution 4

Please use Dependancy Walker and it will give hint about the method name which is registered inside dll.

http://www.dependencywalker.com/

If method name is - getNumber Dependency Walker will show the actual method name as- _Z9GetNumberd

Ex -

lpGetNumber = (LPGETNUMBER)GetProcAddress(hDLL, "_Z9GetNumberd");

Now LoadLibrary and GetProcAddress() both will work.

Share:
17,401
Moon
Author by

Moon

Just a student dabbling in code

Updated on June 04, 2022

Comments

  • Moon
    Moon about 2 years

    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