简体   繁体   中英

Incorrect argument to CreateThread

#include <windows.h>

DWORD Menuthread(LPVOID in) { return 0; }

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

I'm getting the following error message:

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

If you are compiling on 32-bit visual c++ the default calling convention is __cdecl . CreateThread expects a __stdcall function pointer. The simplest fix to this is to use the WINAPI macro which should be defined to the correct calling convention for the platform you are using:

#include <windows.h>

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

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

Alternatively use std::thread and just use the default calling convention, this also means you can pass parameters to your function without having to cast them to void* :

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

DWORD Menuthread() { return 0; }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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