繁体   English   中英

CreateThread 的参数不正确

[英]Incorrect argument to CreateThread

#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 位可视 c++ 上编译,则默认调用约定是__cdecl CreateThread需要一个__stdcall function 指针。 最简单的解决方法是使用WINAPI宏,该宏应定义为您正在使用的平台的正确调用约定:

#include <windows.h>

DWORD WINAPI Menuthread(LPVOID in) { return 0; }

int main()
{
    CreateThread(NULL, NULL, Menuthread, NULL, NULL, NULL);
}

或者使用std::thread并仅使用默认调用约定,这也意味着您可以将参数传递给 function 而不必将它们强制转换为void*

#include <windows.h>
#include <thread>

DWORD Menuthread() { return 0; }

int main()
{
    std::thread thread(Menuthread);
}

暂无
暂无

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

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