简体   繁体   English

CreateThread字符串转换为LPVOID

[英]CreateThread string to LPVOID

I'm trying to make a thread to handle ZIP archiving: 我正在尝试创建一个线程来处理ZIP归档:

 HANDLE hThread = CreateThread(
        NULL,
        0,
        ZipProcess,
        (LPVOID) cmdline.c_str(),
        0,
        NULL);

I'm passing the command line argument as a string in the lpParameter . 我在lpParameter中将命令行参数作为字符串传递。

I keep getting this error: 我不断收到此错误:

...argument of type 'void (MyClass::)(std::string) {aka void (MyClass::)(std::basic_string)}' does not match 'LPTHREAD_START_ROUTINE {aka long unsigned int ( )(void )}'| ...类型为'void(MyClass ::)(std :: string){aka void(MyClass ::)(std :: basic_string)}的参数与'LPTHREAD_START_ROUTINE {aka long unsigned int( )(void ) }'|

I've tried several things - passing by reference, writing to a buffer , a reinterpret_cast , and others, but the error persists. 我尝试了几件事-通过引用传递, 写入缓冲区reinterpret_cast等,但是错误仍然存​​在。 How to fix this? 如何解决这个问题?

You're looking in the wrong spot. 您正在寻找错误的位置。 The compiler is complaining about the third argument, the thread procedure. 编译器抱怨第三个参数,线程过程。 Your error looks GCCish, and that says something along the lines of Error passing in argument 3... 您的错误看起来像是GCCish,这表明在Error的意义上传入了参数3 ...

To fix it, you need a function signature that actually matches what the function takes (this is an expanded version of the LPTHREAD_START_ROUTINE typedef), namely: 要解决此问题,您需要一个实际上与功能需要匹配的功能签名(这是LPTHREAD_START_ROUTINE typedef的扩展版本),即:

DWORD (WINAPI *lpStartAddress)(LPVOID)

The three problems with your definition are: 您定义的三个问题是:

  1. Your function does not use the WINAPI (AKA __stdcall ) calling convention. 您的函数不使用WINAPI(AKA __stdcall )调用约定。
  2. Your function has a std::string parameter instead of LPVOID (AKA void * ). 您的函数具有std::string参数,而不是LPVOID (又称为void * )。
  3. Your function is a class member. 您的职能是班级成员。 You need either a static member or a free function in order for it not to expect an additional this argument, causing a signature mismatch. 您需要一个静态成员或一个自由函数,以使其不期望额外的this参数,从而导致签名不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM