传递PVOID数组,包含多个类型变量到_beginthreadex()

时间:2018-01-09 14:56:38

标签: c++ multithreading winapi beginthreadex

我想将HANDLE和HWND变量传递给_beginthreadex函数,我不想将这些变量设置为全局变量。

我尝试过的事情:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR 
lpCmdLine, int nShowCmd)
{
    HANDLE t = NULL;
    HWND wnd = NULL;

    // initialization of wnd and t by their functions

   PVOID args[2];
   args[0] = &t;
   args[1] = &wnd;

   _beginthreadex(NULL, 0, threadfunc, args, NULL,NULL,NULL);

   // doing some additional stuff
   return 0;
} 

unsigned int __stdcall threadfunc(PVOID args){
    waitforsingleobject(*(PHANDLE)args[0], INFINITE);
    EnableWindow((PHWND)args[1]) ;
    return 0;
}
不幸的是,这不是一件好事吗?

1 个答案:

答案 0 :(得分:1)

它不起作用,因为您没有正确地输入args。你需要更像这样的东西:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR 
lpCmdLine, int nShowCmd)
{
    HANDLE t = NULL;
    HWND wnd = NULL;

    // initialization of wnd and t by their functions

    void* args[2];
    args[0] = &t;
    args[1] = &wnd;

    _beginthreadex(NULL, 0, threadfunc, args, NULL, NULL, NULL);

    // doing some additional stuff
    // make sure args, t and wnd don't go out of scope before the thread terminates...

    return 0;
} 

unsigned int __stdcall threadfunc(PVOID args)
{
    void **pargs = (void**) args;
    HANDLE t = * (HANDLE*) pargs[0];
    HWND wnd = * (HWND*) pargs[1];

    WaitForSingleObject(t, INFINITE);
    EnableWindow(wnd, TRUE);

    return 0;
}

或者,使用动态分配:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR 
lpCmdLine, int nShowCmd)
{
    HANDLE t = NULL;
    HWND wnd = NULL;

    // initialization of wnd and t by their functions

    void **args = new void*[2];
    args[0] = &t;
    args[1] = &wnd;

    if (!_beginthreadex(NULL, 0, threadfunc, args, NULL, NULL, NULL))
        delete[] args;

    // doing some additional stuff
    // make sure t and wnd don't go out of scope before the thread terminates...

    return 0;
} 

unsigned int __stdcall threadfunc(PVOID args)
{
    void **pargs = (void**) args;
    HANDLE t = * (HANDLE*) pargs[0];
    HWND wnd = * (HWND*) pargs[1];

    WaitForSingleObject(t, INFINITE);
    EnableWindow(wnd, TRUE);

    delete[] pargs;
    return 0;
}

更好的解决方案是使用结构而不是数组:

struct MyHandles
{
    HANDLE t;
    HWND wnd;
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR 
lpCmdLine, int nShowCmd)
{
    HANDLE t = NULL;
    HWND wnd = NULL;

    // initialization of wnd and t by their functions

    MyHandles arg;
    arg.t = t;
    arg.wnd = wnd;

    _beginthreadex(NULL, 0, threadfunc, &arg, NULL, NULL, NULL);

    // doing some additional stuff
    // make sure arg doesn't go out of scope before the thread terminates...

    return 0;
} 

unsigned int __stdcall threadfunc(PVOID args)
{
    MyHandles *parg = (MyHandles*) args;

    WaitForSingleObject(parg->t, INFINITE);
    EnableWindow(parg->wnd, TRUE);

    return 0;
}

或者,使用动态分配:

struct MyHandles
{
    HANDLE t;
    HWND wnd;
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR 
lpCmdLine, int nShowCmd)
{
    HANDLE t = NULL;
    HWND wnd = NULL;

    // initialization of wnd and t by their functions

    MyHandles *arg = new MyHandles;
    arg->t = t;
    arg->wnd = wnd;

    if (!_beginthreadex(NULL, 0, threadfunc, arg, NULL, NULL, NULL))
        delete arg;

    // doing some additional stuff
    return 0;
} 

unsigned int __stdcall threadfunc(PVOID args)
{
    MyHandles *parg = (MyHandles*) args;

    WaitForSingleObject(parg->t, INFINITE);
    EnableWindow(parg->wnd, TRUE);

    delete parg;
    return 0;
}