簡體   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