Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: warz on September 29, 2006, 01:31 AM

Title: GetProcAddress returning NULL - why?
Post by: warz on September 29, 2006, 01:31 AM
My call to GetProcAddress looks as such...


HOOKPROC procAddr = (HOOKPROC)GetProcAddress(hDll, "CBTProc");


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


#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);
};


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.
Title: Re: GetProcAddress returning NULL - why?
Post by: MyndFyre on September 29, 2006, 04:03 AM
Make CBTProc:


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


Alternatively you could create a .def (module definition) file:

LIBRARY ADDRPTCH
EXPORTS
    CBTProc #1


For more information, see the MSDN article (http://msdn2.microsoft.com/en-us/library/z4zxe9k8.aspx) about exporting from a DLL.
Title: Re: GetProcAddress returning NULL - why?
Post by: RealityRipple on September 29, 2006, 04:45 AM
BTW, it's intents and purposes.
Title: Re: GetProcAddress returning NULL - why?
Post by: TheMinistered on September 29, 2006, 01:53 PM
Perhaps you didn't export by name, but instead by ordinal?  Check it with dependency walker.  Dunno I could be wrong ;)
Title: Re: GetProcAddress returning NULL - why?
Post by: Win32 on September 29, 2006, 05:58 PM
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
Title: Re: GetProcAddress returning NULL - why?
Post by: K on September 29, 2006, 06:13 PM
Quote from: Win32 on September 29, 2006, 05:58 PM
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

You can override the compiler name mangling.


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


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:


LIBRARY somedll
EXPORTS
myfunction=_myfunction@8
Title: Re: GetProcAddress returning NULL - why?
Post by: warz on October 02, 2006, 03:45 AM
I tried the .def file route first and it worked. So I guess I'll continue to use that method.