0

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

3
Contributors
9
Replies
11
Views
8 Years
Discussion Span
Last Post by Freaky_Chris
Featured Replies
  • 1
    Narue 5,707   8 Years Ago

    I'd be willing to bet that it's the string literal causing you problems. Try this: [code=cplusplus] #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 << … Read More

  • [QUOTE=Freaky_Chris;714187]*cough* Dev-Cpp[/QUOTE] THIS compiled ok on dev-cpp: [CODE=cplusplus] 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; }[/CODE] notice the [ICODE]void* ptr;[/ICODE] I suspect problem is in your "HELLO", not in dwThreadId … Read More

0

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

0

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

1

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;
}
1

*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]

0

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

i feel like a fool now lol!

Chris

0

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*)

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.