Valhalla Legends Forums Archive | C/C++ Programming | GetProcAddress returning NULL - why?

AuthorMessageTime
warz
My call to GetProcAddress looks as such...

[code]
HOOKPROC procAddr = (HOOKPROC)GetProcAddress(hDll, "CBTProc");
[/code]

hDll is a properly returned handle from LoadLibrary. Within the dll being used, CBTProc looks like so...

[code]
#include <windows.h>

void ourfunc(void);
int PatchAddress(int type, DWORD AddressToPatch, DWORD AddressToUse, DWORD PadSize);

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
    switch (dwReason) {
case DLL_PROCESS_ATTACH: {
// PatchAddress(2, 0x004120d4, (DWORD)&ourfunc, NULL);
break;
}

        case DLL_PROCESS_DETACH:
break;
    }

return true;
}

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
    return CallNextHookEx(0, nCode, wParam, lParam);
};
[/code]

For all intensive purposes, that is my entire basic dll. Does anyone know why GetProcAddress would be returning NULL, and GetLastError is returning ERROR_PROC_NOT_FOUND? CBTProc exists.
September 29, 2006, 6:31 AM
Myndfyr
Make CBTProc:

[code]
LRESULT CALLBACK __declspec(dllexport) CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
      return CallNextHookEx(0, nCode, wParam, lParam);
}
[/code]

Alternatively you could create a .def (module definition) file:
[code]
LIBRARY ADDRPTCH
EXPORTS
    CBTProc #1
[/code]

For more information, see the MSDN article about exporting from a DLL.
September 29, 2006, 9:03 AM
RealityRipple
BTW, it's intents and purposes.
September 29, 2006, 9:45 AM
TheMinistered
Perhaps you didn't export by name, but instead by ordinal?  Check it with dependency walker.  Dunno I could be wrong ;)
September 29, 2006, 6:53 PM
Win32
For whatever reason I've never been able to dynamically import by-name, until atleast I realized by compiler added additional characters to the export names.


-Matt
September 29, 2006, 10:58 PM
K
[quote author=Win32 link=topic=15803.msg159173#msg159173 date=1159570682]
For whatever reason I've never been able to dynamically import by-name, until atleast I realized by compiler added additional characters to the export names.


-Matt
[/quote]

You can override the compiler name mangling.

[code]
// do not add c++ type info to name; export as a C function
extern "C" {
_declspec(dllexport) void __stdcall myfunction(int a, int b) {/ * ... */ }
}
[/code]

with no additional changes, this will be exported as _myfunction@8 by microsoft's c++ compiler (on a 32-bit system).

you can add the following .def file to cause the function to be exported as myfunction:

[code]
LIBRARY somedll
EXPORTS
myfunction=_myfunction@8
[/code]
September 29, 2006, 11:13 PM
warz
I tried the .def file route first and it worked. So I guess I'll continue to use that method.
October 2, 2006, 8:45 AM

Search