简体   繁体   English

_beginthread中的ShellExecute

[英]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::threadstd::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的结果(与_beginthreadexCreateThread的结果形成对比)不安全,因为它可能无效,如文档中所述。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM