c++ - CreateThread 的参数不正确

标签 c++ c dword

#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/

相关文章:

c - C 中 char[] 和 char* 的区别

c++ - OpenSSL Boost context.use_private_key_file 和 context.use_rsa_private_key_file 无一异常(exception)地终止程序

c++ - 我的窗口在 OpenGL 中正在调整大小

c++ - 将 32 位地址的类型转换为 (BYTE *) 和 (DWORD *) 有什么区别

java - 将 Java 字符串转换为 WORD、DWORD 或 QWORD

c++ - 在 C++ 中从 vector<int> 生成子列表

c++ - 将函数参数从 void* 转换为 actual

c++ - win32 GetConsoleMode() 错误代码 6

c++ - 为什么在将字符串类型转换为 DWORD 时在运行时得到不同的值?

c++ - C++中 vector 的叉积