将包含多个类型变量的 PVOID 数组传递给 _beginthreadex()
passing PVOID array, containing multiple type variables to _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;
}
不幸的是,这不起作用..一个想法?
它不起作用,因为您没有正确地进行类型转换 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;
}
我想将 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;
}