0

i'm trying to get this struct into my thread, however i'm getting trash in it.

struct i'm using:

typedef struct {
    HWND hWnd;
    int cntrlid;
    TCHAR text[BUFF];
}parametros;

in the message switch i have the case to control a button witch starts the thread:

    case IDC_TAB_A:
        threaddata.hWnd=hWnd;
        threaddata.cntrlid=IDC_TAB_A;
        _tcscpy(threaddata.text, _T("Carregou A"));
        hThreadsender=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadSenderProc, (LPVOID) &threaddata, 0, NULL);

        break;

and the function to handle the thread i got:

DWORD WINAPI ThreadSenderProc(LPVOID param){

    parametros temp=*((parametros *)param);
    EnviaLetra(temp.hWnd, temp.text,temp.cntrlid);

    return 0;
}

something must be wrong, but i cant get what it is, can anyone help?

Thanks in advance! (sorry bad english!)

1 Answers1

2

Your structure is destroyed before the thread can access it. Either use an event to synchronize, or allocate your structure dynamically.

PS: Remove that hideous (LPTHREAD_START_ROUTINE) cast. If you declare your function correctly, you won't need it. Function pointer casts are to ban unless you know exactly what you're doing and why. Oh, and the (LPVOID) cast is useless too.

Medinoc
  • 6,577
  • 20
  • 42
  • its the copy paste:P this is a uni homework so i was just copying class examples, going to mallock that struct and check what happens thanks for your answer – Roger Martins May 07 '13 at 18:36
  • god, mate, i love you, i puled so mutch hair today, after 4 pomodoros that problem happened, 5 later here we hare, thanks! – Roger Martins May 07 '13 at 19:03
  • @RogerMartins Please accept the answer. FWIW, using heap allocation is the right solution here. Using an event to sync is heavy duty and costs more. It's a poor solution. Heap allocation is the way to go. And yes, stop all that nasty casting! – David Heffernan May 07 '13 at 22:34
  • 1
    @DavidHeffernan i think i accepted it now, new to posting here, usualy i lurk – Roger Martins May 07 '13 at 23:59