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
__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.