gpt4 book ai didi

c++ - 将包含多个类型变量的 PVOID 数组传递给 _beginthreadex()

转载 作者:行者123 更新时间:2023-11-28 04:51:15 26 4
gpt4 key购买 nike

我想将 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;
}

不幸的是,这不起作用..一个想法?

最佳答案

它不起作用,因为您没有正确地类型转换 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;
}

关于c++ - 将包含多个类型变量的 PVOID 数组传递给 _beginthreadex(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48170902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com