CreateThread函数参数传递错误

问题描述 投票:0回答:1

当我像这样使用 CreateThread 函数时

CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)MyModule::MyFunction, (LPVOID)&wsParam, 0, &dwThreadId);

参数的地址传递错误,导致 wstring(或任何其他类型的参数)无效。该地址根本不指向传递的数据。

作为新线程调用的函数:

    std::wstring MyModule::MyFunction(LPVOID stringParam) {
        std::wstring wsParam = *reinterpret_cast<std::wstring*>(stringParam); //Bad alloc
    }
c++ multithreading winapi
1个回答
0
投票

尽管 Windows API 中似乎没有记录(或者至少我没有找到它),但为新线程调用的函数必须返回

DWORD

更改被调用函数的标头以返回 DWORD 可以正确传递参数。

DWORD MyModule::MyFunction(LPVOID stringParam) {
    std::wstring wsParam = *reinterpret_cast<std::wstring*>(stringParam); //Bad alloc
}
© www.soinside.com 2019 - 2024. All rights reserved.