Incorrect argument to CreateThread

966 Views Asked by At
#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
1

There are 1 best solutions below

1
Alan Birtles On

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);
}