[英]ShellExecute in _beginthread
I need to run for example: 我需要运行例如:
ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE);
as new thread, but I don't know how. 作为新线程,但我不知道如何。 I tried this:
我尝试了这个:
HANDLE hThread = (HANDLE) _beginthread(ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE), 0, NULL);
WaitForSingleObject( hThread, INFINITE );
but obviously it's wrong and can't be compiled. 但显然这是错误的,无法编译。 How should I do this?
我应该怎么做?
What you tried is indeed obviously wrong, but the question is whether you understand what's wrong with it. 您尝试过的确确实是错的,但问题是您是否了解问题所在。
_beginthread
takes a pointer to function (with a specific prototype and calling convention) as its first parameter. _beginthread
将指向函数的指针 (具有特定的原型和调用约定)作为其第一个参数。
When you write 当你写
HANDLE hThread = (HANDLE) _beginthread(ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE), 0, NULL);
you're trying to pass _beginthread
the result of calling ShellExecute
(in the current thread) which is an HINSTANCE
, while _beginthread
expects a void( __cdecl *)( void * )
(pointer to a __cdecl
function taking one void*
parameter and returning void
). 您正在尝试向
_beginthread
传递调用ShellExecute
(在当前线程中)的结果 ,这是一个HINSTANCE
,而_beginthread
期望一个void( __cdecl *)( void * )
(指向具有一个void*
参数并返回void
的__cdecl
函数的指针)。
Not only your code doesn't work because you're trying to pass an HINSTANCE
where a function to pointer is expected, it doesn't make any sense. 不仅您的代码不起作用,是因为您试图传递一个
HINSTANCE
来实现预期指向指针的功能,但这没有任何意义。 Have you read the _beginthread
documentation ? 您阅读过
_beginthread
文档吗? There are examples there. 那里有例子。 Plural.
复数。
What you meant to write is: 您的意思是:
HANDLE hThread = (HANDLE) _beginthread(ThreadFunc, 0, NULL);
given: 给出:
void __cdecl ThreadFunc(void*) {
ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE);
}
Or, in a more compact and easy to read form: 或者,以更紧凑,更易于阅读的形式:
HANDLE hThread = (HANDLE)
_beginthread([](void*) {
ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE);
},
0, NULL);
Unless you're doing something beyong what we're seeing here, David'scomment is probably right and you should be using std::thread
or std::async
. 除非您在我们在此处看到的内容之外没有做任何事情,否则David的评论可能是正确的,您应该使用
std::thread
或std::async
。
Also note that taking the result of _beginthread
(int contrast to the result of _beginthreadex
or CreateThread
) in unsafe because it may not be valid, as noted in the documentation. 还要注意,将
_beginthread
的结果(与_beginthreadex
或CreateThread
的结果形成对比)不安全,因为它可能无效,如文档中所述。 Not only that, but _beginthread
's return value isn't really a HANDLE
(it is some sore of a handle, but not a HANDLE
!), so you can't WaitForSingleObject
on it: 不仅如此,
_beginthread
的返回值实际上并不是一个HANDLE
(它HANDLE
句柄,但不是HANDLE
!),因此您不能在上面WaitForSingleObject
:
The
_beginthreadex
function gives you more control over how the thread is created than_beginthread
does.该
_beginthreadex
功能为您提供了线程的创建超过控制_beginthread
一样。 The_endthreadex
function is also more flexible._endthreadex
函数也更加灵活。 For example, with_beginthreadex
, you can use security information, set the initial state of the thread (running or suspended), and get the thread identifier of the newly created thread.例如,使用
_beginthreadex
,您可以使用安全性信息,设置线程的初始状态(运行或暂停),并获取新创建的线程的线程标识符。 You can also use the thread handle that's returned by_beginthreadex
with the synchronization APIs, which you cannot do with_beginthread
.您还可以将
_beginthreadex
返回的线程句柄与同步API一起使用,而_beginthread
则不能使用 。It's safer to use
_beginthreadex
than_beginthread
.使用
_beginthreadex
比_beginthread
更安全。 If the thread that's generated by_beginthread
exits quickly, the handle that's returned to the caller of_beginthread
might be invalid or point to another thread.如果
_beginthread
生成的线程快速退出,则返回给_beginthread
调用者的句柄可能无效或指向另一个线程。 However, the handle that's returned by_beginthreadex
has to be closed by the caller of_beginthreadex
, so it is guaranteed to be a valid handle if_beginthreadex
did not return an error.但是,这是由返回的句柄
_beginthreadex
必须由调用者关闭_beginthreadex
,所以它是保证一个有效的句柄,如果_beginthreadex
没有返回一个错误。
Since this thread only calls one function and exits, it almost maximizes the chance of this handle not being valid. 由于此线程仅调用一个函数并退出,因此它几乎最大化了此句柄无效的机会。 And even if it were, you still couldn't use it to
WaitForSingleObject
. 即使是,您仍然无法将其用于
WaitForSingleObject
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.