[英]invalid cast from void(WINAPI*)(…) to LPVOID
i'm trying to use this code [RasDialAsync.c] ( https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/master/netds/ras/rasdialasync/RasDialAsync.c ) inside QT , i have only one error that i can't figure it out . 我正在尝试在QT内部使用此代码[RasDialAsync.c]( https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/master/netds/ras/rasdialasync/RasDialAsync.c ),我只有一个错误我无法弄清楚。 what is this ?
这是什么 ? how can i fix this ?
我怎样才能解决这个问题 ? here is the function header :
这是函数头:
My RasDialFunc function : 我的RasDialFunc函数:
void WINAPI RasDialFunc(UINT unMsg,
RASCONNSTATE rasconnstate,
DWORD dwError )
and here is part of my codes : 这是我的代码的一部分:
LPRASDIALPARAMS lpRasDialParams = NULL; // Structure to store the RasDial parameters
HRASCONN hRasConn = NULL; // Handle to RAS connection
DWORD nRet = 0; // Return value from a function
lpRasDialParams = (LPRASDIALPARAMS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RASDIALPARAMS));
if (NULL == lpRasDialParams)
{
qDebug() << "HeapAlloc failed\n";
}
lpRasDialParams->dwSize =sizeof(RASDIALPARAMS);
StringCchCopy(lpRasDialParams->szEntryName, CELEMS(lpRasDialParams->szEntryName), rasConn);
StringCchCopy(lpRasDialParams->szUserName, CELEMS(lpRasDialParams->szUserName), username);
StringCchCopy(lpRasDialParams->szPassword, CELEMS(lpRasDialParams->szPassword), password);
hRasConn = NULL;
nRet = RasDial(NULL, NULL, lpRasDialParams, 0,&RasDialFunc, &hRasConn);
if (nRet != 0)
and here is the error 这是错误
main.cpp:202: error: invalid conversion from 'void (__attribute__((__stdcall__)) *)(UINT, tagRASCONNSTATE, DWORD) {aka void (__attribute__((__stdcall__)) *)(unsigned int, tagRASCONNSTATE, long unsigned int)}' to 'LPVOID {aka void*}' [-fpermissive]
nRet = RasDial(NULL, NULL, lpRasDialParams, 0,&RasDialFunc, &hRasConn);
^
These Following Comments are my answer : 这些以下评论是我的回答:
It is, technically, illegal to cast between a function pointer and an object pointer.
从技术上讲,在函数指针和对象指针之间进行强制转换是非法的。 The standard allows them to have different sizes.
该标准允许它们具有不同的大小。 For better or worse, WinAPI requires such a cast in a number of places (and I seem to recall there are some POSIX functions that do, too).
不管好坏,WinAPI在很多地方都需要这样的转换(而且我似乎还记得有些POSIX函数也可以这样做)。 MSVC compiler never complained about such a cast, but you are using GCC, which seems to want to be pedantic about it.
MSVC编译器从未抱怨过这样的强制转换,但是您使用的是GCC,它似乎想对它进行脚。 I suspect there's might be some command line option that would make GCC accept it - look for that (I myself am not familiar with GCC).
我怀疑可能会有一些命令行选项会让GCC接受它-寻找那个(我本人对GCC并不熟悉)。 – Igor Tandetnik
– 伊戈尔·坦德尼克 ( Igor Tandetnik)
And the final code is : 最后的代码是:
nRet = RasDial(NULL, NULL, lpRasDialParams, 0,reinterpret_cast<void*>(&RasDialFunc) , &hRasConn);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.