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

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?

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

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.