简体   繁体   中英

error: invalid conversion from 'int (*)()' to 'long unsigned int'

I'm rather new to C++ and I'm trying to understand the code over on this forum http://www.blizzhackers.cc/viewtopic.php?p=2483118 . I've managed to work out most of the errors but this one's got me stumped here's the code from the function giving me problems.

void LoadDll(char *procName, char *dllName)
{
    HMODULE hDll;
    unsigned long cbtProcAddr;

    hDll = LoadLibrary(dllName);
    cbtProcAddr = GetProcAddress(hDll, "CBTProc"); // The error points to this line

    SetWindowsHookEx(WH_CBT, cbtProcAddr, hDll, GetTargetThreadIdFromProcname(procName));
}

Change the definition of cbtProcAddr to:

HOOKPROC cbtProcAddr;

The compiler is upset because you are trying to store a pointer-type value in an variable declared to hold an integer. (You may need to cast the result of GetProcAddress() to HOOKPROC , since that function doesn't know the actual signature of the pointed-to function, but the usage of the pointer in the SetWindowsHookEx() call implies that it is compatible with the signature of the HOOKPROC function-pointer type.)

GetProcAddress returns a FARPROC (which, looking at the compiler error, is just a typedef for int(*)() ). unsigned long is not a FARPROC , and there's no implicit conversion between the two.

I can't fathom why you would store the result of GetProcAddress in an unsigned long . If you retrieve a function you want to store a function pointer. Use the correct type ( SetWindowsHookEx takes a HOOKPROC ) and cast:

HOOKPROC cbtProcAddr;

hDll = LoadLibrary(dllName);
cbtProcAddr = reinterpret_cast<HOOKPROC>(GetProcAddress(hDll, "CBTProc"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM