将包含多个类型变量的 PVOID 数组传递给 _beginthreadex()
passing PVOID array, containing multiple type variables to _beginthreadex()
我想将句柄和 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;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '