简体   繁体   中英

CreateThread passing std::string as argument

I need to create a thread in a managed C++ code (CLR) to call an unmanaged C++ class member function passing a std::string as a parameter. The thread is being called, but the received std::string is being received as an empty string:

The managed code:

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

The unmanaged code:

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
}

Specifically in your case, you're passing &MyThread as the thread function parameter and passing the param as the dwCreationFlags parameter of the CreateThread function, which specifies thread creation options.

Additionally, you'll need to make sure you keep param around for the lifetime of the thread.

Hope that helps.

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