CreateThread传递std::string作为参数
CreateThread passing std::string as argument
我需要在托管c++代码(CLR)中创建一个线程,以调用传递std::string
作为参数的非托管c++类成员函数。正在调用线程,但是收到的std::string
是作为空字符串接收的:
托管代码:
std::string param;
CreateThread(0, NULL, (LPTHREAD_START_ROUTINE) &MyThread.Start, &MyThread, (DWORD) ¶m, NULL);
非托管代码
class MyThread
{
public:
MyThread();
static void Start(std::string ¶m);
};
void MyThread::Start(std::string ¶m)
{
std::cout << param << std::endl; <<=== param is empty here
}
特别是在您的情况下,您将&MyThread
传递为线程函数参数,并将param
传递为CreateThread
函数的dwCreationFlags
参数,这指定了线程创建选项。
此外,您需要确保在线程的生命周期内保持param
。
希望对你有帮助。
相关文章:
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- <string> 如何在使用 SWIG 时将 int 数组和 List 作为参数传递给 C# C++
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 在 C++17 中使用 const std::string& 参数有意义吗?
- 将 std::string 作为参数从一个 DLL 传递到另一个 DLL 引发访问冲突错误
- 变量参数列表 后面的'const std::string&'弄乱了堆栈
- 为什么我不能像使用 std::string::size_type 那样使用 QList::size_type?(模板参数错误)
- 在基于 arm 的 Android 设备上使用带有参数 -DCMAKE_BUILD_TYPE:STRING=Releas
- q致命参数:将 QString 转换为常量字符* 会导致警告"format string is not a string literal"
- std::string 参数是如何从原始字符数组文本传递的
- 无法从'std::string'中推断出'const std::reverse_iterator<_RanIt> &'的模板参数
- 'int main(int, int, std::__cxx11::string, std::__cxx11::string)'只需要零或两个参数 [-Wmain]
- 'const std::string&s = nullptr' 如何作为可选参数工作
- 没有匹配函数调用 std::forward(const std::string &) 与可变参数
- 为什么在类构造函数方法中,std::string 参数在调试时显示不同的结果?
- "Any value" 对于 'const std::string&' 参数
- "vector<string>"作为函数参数传递,我得到一个错误
- 如何检测我何时向可变参数函数传递"std::string"而不是"c_str()"
- 为什么带有Bool参数的函数接受String
- 如何初始化结构字段 std::map<std::string, std::string>称为参数