Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top