#include <windows.h>
DWORD Menuthread(LPVOID in) { return 0; }
int main()
{
CreateThread(NULL, NULL, Menuthread, NULL, NULL, NULL);
}
我收到以下错误消息:
error C2664: 'HANDLE CreateThread(LPSECURITY_ATTRIBUTES,SIZE_T,LPTHREAD_START_ROUTINE,LPVOID,DWORD,LPDWORD)': cannot convert argument 3 from 'DWORD (__cdecl *)(LPVOID)' to 'LPTHREAD_START_ROUTINE'
note: None of the functions with this name in scope match the target type
最佳答案
如果您在 32 位 visual c++ 上编译,默认调用约定是 __cdecl
。 CreateThread
需要一个 __stdcall
函数指针。最简单的解决方法是使用 WINAPI
宏,它应该被定义为您正在使用的平台的正确调用约定:
#include <windows.h>
DWORD WINAPI Menuthread(LPVOID in) { return 0; }
int main()
{
CreateThread(NULL, NULL, Menuthread, NULL, NULL, NULL);
}
或者使用 std::thread
并只使用默认调用约定,这也意味着您可以将参数传递给您的函数,而不必将它们转换为 void*
:
#include <windows.h>
#include <thread>
DWORD Menuthread() { return 0; }
int main()
{
std::thread thread(Menuthread);
}
关于c++ - CreateThread 的参数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904612/