简体   繁体   English

错误:从'int(*)()'到'long unsigned int'的无效转换

[英]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 . 我对C ++很陌生,我试图在这个论坛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: cbtProcAddr的定义更改为:

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()的结果GetProcAddress()HOOKPROC ,因为该函数不知道指向函数的实际签名,但SetWindowsHookEx()调用中指针的使用意味着它与HOOKPROC函数指针类型的签名。)

GetProcAddress returns a FARPROC (which, looking at the compiler error, is just a typedef for int(*)() ). GetProcAddress返回一个FARPROC (查看编译器错误,它只是int(*)()的typedef)。 unsigned long is not a FARPROC , and there's no implicit conversion between the two. unsigned long不是FARPROC ,并且两者之间没有隐式转换。

I can't fathom why you would store the result of GetProcAddress in an unsigned long . 我无法理解为什么要将GetProcAddress的结果存储在unsigned long If you retrieve a function you want to store a function pointer. 如果检索要存储函数指针的函数。 Use the correct type ( SetWindowsHookEx takes a HOOKPROC ) and cast: 使用正确的类型( SetWindowsHookEx采用HOOKPROC )并HOOKPROC

HOOKPROC cbtProcAddr;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译器错误:无效转换从int *到unsigned int * [-fpermissive] - Compiler Error: Invalid Conversion from int* to unsigned int* [-fpermissive] 从std :: size_t *到long unsigned int的转换无效* - Invalid conversion from std::size_t* to long unsigned int* 没有从long unsigned int转换为long unsigned int& - No conversion from long unsigned int to long unsigned int& 错误:从&#39;void *&#39;到&#39;test :: apr_size_t * {aka long unsigned int *}&#39;的无效转换&#39;[-fpermissive] - error: invalid conversion from ‘void*’ to ‘test::apr_size_t* {aka long unsigned int*}’ [-fpermissive] 从int到unsigned int的无效转换 - Invalid conversion of int to unsigned int 错误消息“从&#39;void *&#39;到&#39;unsigned int&#39;的无效转换” - Error Message “invalid conversion from ‘void*’ to ‘unsigned int’” 错误:从&#39;short unsigned int *&#39;到&#39;short unsigned int&#39;的无效转换| (在C ++中) - error: invalid conversion from 'short unsigned int*' to 'short unsigned int'| (in c++) 从“int (*)(int)”到“int”错误的无效转换? - invalid conversion from ‘int (*)(int)’ to ‘int’ error? 在C ++中从无符号long long转换为unsigned int - Conversion from unsigned long long to unsigned int in C++ 错误:&#39;18446744069414584320ull&#39;从&#39;long long unsigned int&#39;到&#39;int&#39;的缩小转换{} [-Wnarrowing] - error: narrowing conversion of ‘18446744069414584320ull’ from ‘long long unsigned int’ to ‘int’ inside { } [-Wnarrowing]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM