gpt4 book ai didi

c++ - CreateThread 的参数不正确

转载 作者:行者123 更新时间:2023-11-30 03:13:13 27 4
gpt4 key购买 nike

#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++ 上编译,默认调用约定是 __cdeclCreateThread 需要一个 __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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com