Hi, i'm havnig problem with calling CreateThread. The problem is that it gives me a conversion error from const void* to void*

DWORD   dwThreadId;
    HANDLE  myThread; 
    
    myThread = CreateThread( 
            NULL,                   
            0,                       
            MyThreadFunction,      
            "HELLO",           
            0,                      
            &dwThreadId);

dwThreadId id is the problem, the function is supose to take a pointer to a DWORD.

Thanks for any help
Cgris

Dani AI

Generated

A short, practical follow-up to the discussion by , and .

The compiler error came from passing a string literal into the thread parameter: string literals are const char[] in C++, while the Windows thread parameter type (LPVOID) is a non-const void. Modern compilers refuse an implicit conversion that would drop constness. Also note: printing the raw LPVOID with std::cout prints an address (the `void` overload), not the text — you must cast back to a character pointer to see the string.

Safer options that avoid casts and lifetime problems:

  • Prefer C++ threads: capture or pass a std::string by value so the string lifetime is managed automatically.
  • If you must use Win32 threads, allocate an argument object on the heap (or transfer ownership via smart pointer) and have the thread free it when done.
  • If your thread uses the C runtime (stdio/iostream), use _beginthreadex rather than CreateThread to avoid CRT issues.

Example (C++11, std::thread):

#include <thread>
#include <string>
#include <iostream>

void worker(std::string text) {
  std::cout << text << '\n';
  // ...
}

int main() {
  std::thread t(worker, std::string("HELLO"));
  t.detach(); // or t.join()
}

If you keep a void* parameter, cast explicitly when reading:

std::cout << reinterpret_cast<const char*>(lpParam);

Avoid casting away const just to silence the compiler; if you do cast, be certain the memory is mutable and has the correct lifetime.

Recommended Answers

All 10 Replies

Post a complete program that exhibits the problem, please. Also post the exact error message rather than summarizing.

OK, by no means is this safe. It was a meer test to get me started with threads.

#include <windows.h>
#include <iostream>

DWORD WINAPI MyThreadFunction( LPVOID lpParam );

int main()
{
    DWORD   dwThreadId;
    HANDLE  myThread; 
    
    myThread = CreateThread( 
            NULL,                   
            0,                       
            MyThreadFunction,      
            "HELLO",           
            0,                      
            &dwThreadId);  

    while(1){
             Sleep(3);
             std::cout << "main";
             } 
    std::cin.get();
    return 0;
}

DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
{ 
      std::cout << lpParam;
      while(1){
               Sleep(10);
    std::cout << "thread"; 
    } 
    return 0; 
}

exact error: 17 invalid conversion from `const void*' to `void*'

Thanks

What compiler are you using?

*cough*
Dev-Cpp

I'd be willing to bet that it's the string literal causing you problems. Try this:

#include <windows.h>
#include <iostream>

DWORD WINAPI MyThreadFunction( LPVOID lpParam );

int main()
{
  DWORD   dwThreadId;
  HANDLE  myThread;
  char *param = "HELLO";

  myThread = CreateThread(
    NULL,
    0,
    MyThreadFunction,
    param,
    0,
    &dwThreadId);

  while(1){
    Sleep(3);
    std::cout << "main";
  }
  std::cin.get();
  return 0;
}

DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
  std::cout << lpParam;
  while(1){
    Sleep(10);
    std::cout << "thread";
  }
  return 0;
}

*cough*
Dev-Cpp

THIS compiled ok on dev-cpp:

int main()
{
    DWORD   dwThreadId;
    HANDLE  myThread; 
    void *ptr=NULL;
    
    myThread = CreateThread( 
            NULL,                   
            0,                       
            MyThreadFunction,      
            ptr,           
            0,                      
            &dwThreadId);  

    while(1){
             Sleep(3);
             std::cout << "main";
             } 
    std::cin.get();
    return 0;
}

notice the void* ptr; I suspect problem is in your "HELLO", not in dwThreadId

[edit]And Narue's code compiles on Dev :)[/edit]

Ah that would make much more sense. i did wonder, thanks guys.

i feel like a fool now lol!

Chris

Just to mention, you don't have to declare char ptr, you can simply cast string: (LPVOID)"HELLO"; But, if you want your string to work inside function that you call, you have to typecast it back to (char*)

Just to mention, you don't have to declare char ptr, you can simply cast string: (LPVOID)"HELLO"; But, if you want your string to work inside function that you call, you have to typecast it back to (char*)

Yes, im aware of both of these, it was just the mere fact i was paying way to much time listening to my god damn compiler / IDE and not enough looking at what i had actually written!

Thanks,
Chris

nice

commented: This week's most useless bump award winner -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.