Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Failing to call a function using GetProcAddress in my own dll

I have two projects, one is AddExec and the other is DllAdd.
AddExec loads DllAdd and calls the Add function after retrieving the address through GetProcAddress..

Here’s my main.cpp for AddExec

#include <iostream>
#include <windows.h>
typedef int(__cdecl*AddPtrType) (int x, int y);
int main()
{
    HMODULE DllAddModule = LoadLibraryA("DllAdd.dll");
    if (DllAddModule == NULL)
    {
        DWORD error = GetLastError();
        std::cerr << "Failed to get address : " << error;
        return -1;
    }
        
    AddPtrType AddLocal = (AddPtrType)GetProcAddress(DllAddModule, "Add");
    if (AddLocal == NULL)
    {
        DWORD error = GetLastError();
        std::cerr << "Failed to get address : " << error;
        return -1;
    }
    int x = AddLocal(10, 15);
    std::cout << x;
    return 0;
}

And here’s my main.cpp for DllAdd.dll

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

__declspec(dllexport) int __cdecl Add(int x, int y)
{
    return x + y;
}

The function inside DllAdd is called Add. But when I attempt to retrieve its address I get error code 127

ERROR_PROC_NOT_FOUND
127 (0x7F)
The specified procedure could not be found.

I don’t know why it can’t find the address.. I Am exporting it and using the exact function names

>Solution :

Your compiler is manging the name of your Add function in DllAdd. Because you’re compiling it with the a C++ compiler which allows for function name overloading. The way to resolve this is as I mentioned either compile it as a C project or prevent your compiler from renaming or name mangling the function like so.

extern "C" __declspec(dllexport) int __cdecl Add(int x, int y)
{
    return x + y;
}

extern "C" will prevent your compiler from altering it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading