- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如我需要运行:
ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE);
作为新线程,但我不知道如何。我试过这个:
HANDLE hThread = (HANDLE) _beginthread(ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE), 0, NULL);
WaitForSingleObject( hThread, INFINITE );
但显然是错误的,无法编译。我应该怎么做?
最佳答案
你的尝试确实明显是错误的,但问题是你是否理解它的错误所在。 _beginthread
将指向函数的指针(具有特定原型(prototype)和调用约定)作为其第一个参数。
当你写作时
HANDLE hThread = (HANDLE) _beginthread(ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE), 0, NULL);
您正在尝试将调用 ShellExecute
(在当前线程中)的结果传递给 _beginthread
,这是一个HINSTANCE
,而 _beginthread
期望一个 void( __cdecl *)( void * )
(指向一个 __cdecl
函数的指针,它采用一个 void *
参数并返回 void
).
不仅你的代码不起作用,因为你试图传递一个 HINSTANCE
,其中一个函数指向指针,它没有任何意义。你读过_beginthread
documentation吗? ?那里有例子。复数。
你想写的是:
HANDLE hThread = (HANDLE) _beginthread(ThreadFunc, 0, NULL);
给出:
void __cdecl ThreadFunc(void*) {
ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE);
}
或者,以更紧凑和易于阅读的形式:
HANDLE hThread = (HANDLE)
_beginthread([](void*) {
ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE);
},
0, NULL);
除非你正在做一些超出我们在这里看到的事情,否则大卫的评论可能是正确的,你应该使用 std::thread
或 std::async
.
还请注意,将 _beginthread
的结果(与 _beginthreadex
或 CreateThread
的结果进行对比)是不安全的,因为它可能不安全有效,如文档中所述。不仅如此,_beginthread
的返回值并不是真正的HANDLE
(它有点像句柄,但不是HANDLE
!), 所以你不能在上面 WaitForSingleObject
:
The
_beginthreadex
function gives you more control over how the thread is created than_beginthread
does. The_endthreadex
function is also more flexible. 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. You can also use the thread handle that's returned by_beginthreadex
with the synchronization APIs, which you cannot do with_beginthread
.It's safer to use
_beginthreadex
than_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. 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.
由于这个线程只调用一个函数就退出了,几乎最大化了这个句柄无效的几率。即使是,您仍然不能将它用于 WaitForSingleObject
。
关于c++ - ShellExecute 在_beginthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41723033/
我想使用 ShellExec 在“配置”模式下运行屏幕保护程序。我使用这个 (Delphi) 调用: i:= ShellExecute(0, 'open', PChar('c:\temp\test.
我需要使用 ShellExecute 从我的 VB6 代码执行一个外部程序:我面临的问题是,当路径字符串有任何空间时,它不起作用: Dim Path As String Path = "E:\PROY
我是shell脚本的新手,最近我在脚本中看到一个命令“:>文件”,我不明白它的意思,谁能告诉我这个命令在做什么? 提前致谢 最佳答案 :是一个空操作。 >是重定向,因此文件被 no-op 命令的输出覆
有了这个 ShellExecute(Handle, 'print', PChar(ExtractFilePath(ParamStr(0))+'Test.txt'), nil, nil, SW_H
我尝试通过delphi在shell中执行命令,但它不起作用。我使用这个脚本: var shellexecommand:string; begin ShellExecute(0, nil, 'cmd.e
我正在尝试使用ShellExecute在 Excel 中打开文件。我在MSDN论坛上阅读了有关该函数的信息,发现了以下有关句柄的信息,即第一个参数: “hwnd [in] 用于显示用户界面 (UI)
我有一个程序在执行 ShellExecute() 指令时崩溃。如果我使用 system() 方法,一切正常,但我读过 system() 是邪恶的...... 我使用的是 Visual Studio 2
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
例如我需要运行: ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_HIDE); 作为新线程,但我不知道如何。我试过这个: HANDLE
我的任务是每周检查一次数据库并检查所有链接。我通常在 PHP 中工作,但在 PHP 中这样做会非常慢(实际上它会在大约 100 个 URL 后使页面超时),所以我决定制作一个快速的 C++ 应用程序。
我正在使用 ShellExecute(NULL, L"open", szSomeDirectory, 0, 0, SW_SHOWDEFAULT) 在资源管理器中打开一个目录。 我正在尝试打开一个目录,
我正在使用 ShellExecute WIN API 来运行 DOS 命令,因为在我运行应用程序时会看到 DOS 命令闪烁。下面是 ShellExecute API 调用。 ret = ShellEx
Createprocess API 有一个选项可以创建带有 CREATE_SUSPENDED 标志的进程。相似之处是,ShellExecute API 是否有可能在挂起状态下创建进程。 最佳答案 没有
程序 1 使用 SetEnvironment 并调用 ShellExecute 来启动程序 2。程序 2 调用 GetEnvironment 并从中检索值。令人惊讶的是,正如我在 MSDN 中读到的那
我正在学习如何使用 Python 打印文件。我找到了很多方法来做到这一点,我见过的最常见的方法之一是使用 win32api 模块。 import win32api win32api.ShellExec
成功时,ShellExecute 返回一个句柄。 我们是否需要关闭这个句柄,如果需要,如何关闭? 根据我微软公布的例子,我们不需要关闭这个句柄。但是 ShellExecute 本身的文档对这个主题保持
我有一个 VB6 应用程序,它使用以下方法打开文件及其关联的应用程序: ShellExecute(0, "open", filename, params, vbNullString, vbNormal
我计划使用 ShellExecute 作为用 delphi 2007 编写的应用程序的更新程序,我想知道 ShellExecute 是否可以在大多数使用 Win 的计算机上运行,或者是否会因为某些
我有一个 Delphi 应用程序,它使用 ShellExecute 在按下按钮时调用第二个 Delphi 应用程序。 应用程序存储在同一服务器上的同一网络共享上。它们的路径格式如下: const
我有一个希望很快的问题:是否可以稍微延迟 ShellExecute 的执行? 我有一个带有自动更新程序的应用程序。下载所有必需的文件等后,它将当前文件重命名为 *.OLD,并将新文件重命名为以前的文件
我是一名优秀的程序员,十分优秀!