CreateThread传递std::string作为参数

CreateThread passing std::string as argument

本文关键字:参数 string std CreateThread 传递      更新时间:2023-10-16

我需要在托管c++代码(CLR)中创建一个线程,以调用传递std::string作为参数的非托管c++类成员函数。正在调用线程,但是收到的std::string是作为空字符串接收的:

托管代码:

std::string param;
CreateThread(0, NULL, (LPTHREAD_START_ROUTINE) &MyThread.Start, &MyThread, (DWORD) &param, NULL);

非托管代码

class MyThread
{
    public:
        MyThread();
        static void Start(std::string &param);
};
void MyThread::Start(std::string &param)
{
    std::cout << param << std::endl; <<=== param is empty here
}

特别是在您的情况下,您将&MyThread传递为线程函数参数,并将param传递为CreateThread函数的dwCreationFlags参数,这指定了线程创建选项。

此外,您需要确保在线程的生命周期内保持param

希望对你有帮助。

相关文章: