Ben
Ben

Reputation: 57257

CreateThread string to LPVOID

I'm trying to make a thread to handle ZIP archiving:

 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.

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)}'|

I've tried several things - passing by reference, writing to a buffer, a reinterpret_cast, and others, but the error persists. How to fix this?

Upvotes: 0

Views: 1918

Answers (1)

Qaz
Qaz

Reputation: 61920

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...

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:

DWORD (WINAPI *lpStartAddress)(LPVOID)

The three problems with your definition are:

  1. Your function does not use the WINAPI (A.K.A __stdcall) calling convention.
  2. Your function has a std::string parameter instead of LPVOID (A.K.A. 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.

Upvotes: 1

Related Questions