当预期返回 31 时,ShellExecuteEx 在 hInstApp 中返回 42(无文件关联)
当在 Delphi 7 中使用 ShellExecuteEx 使用动词打开文件时,我似乎总是在 hInstApp 中得到 42 作为结果,即使我期望得到失败和 31 结果,因为没有文件关联。我将从 ShellExecute 迁移到 ShellExecuteEx,以便可以将 WaitForInputIdle 与进程句柄一起使用。
当我在未安装 Excel 的情况下尝试打开 XLS 文件时,ShellExecute 按预期返回 31,但 ShellExecuteEx 似乎成功并返回 42,即使它实际上失败并弹出默认的 Windows 文件关联对话框。
我做错了什么吗?在 WinXP 和 Win7 上使用 Delphi 7。
下面的示例代码。在 Win XP 32 位操作系统上使用 Delphi 7,但在 Win 7 64 位操作系统上也得到相同的结果。仅对 hInstApp 值执行 showmessage 返回 42,而我预计会得到 31,因为我没有安装 Excel。
var
ExecInfo: TShellExecuteInfo;
begin
ZeroMemory(ExecInfo, sizeof(ExecInfo));
with ExecInfo do
begin
cbSize := sizeOf(ExecInfo);
fMask := SEE_MASK_NOCLOSEPROCESS;
lpVerb := PChar('open');
lpFile := PChar('c:\windows\temp\test.xls');
nShow := 1;
end;
ShellExecuteEx(@ExecInfo);
if ExecInfo.hInstApp<32
then WaitForInputIdle(ExecInfo.hProcess, 10000);
end;
When using ShellExecuteEx in Delphi 7 to open a file using a verb, I always seem to be getting 42 back as a result in hInstApp, even though I'm expecting to get a failure and a 31 result, as there is no file association. I am moving from ShellExecute, to ShellExecuteEx so that I can use WaitForInputIdle with the process handle.
ShellExecute returns a 31 as expected when I try to open an XLS file when I dont have Excel installed, but ShellExecuteEx appears to succeed and return 42, even though it has actually failed and popped up the default Windows file association dialog.
Am i doing something wrong? Using Delphi 7 on WinXP and Win7.
Sample code below. Using Delphi 7 on a Win XP 32 bit OS, but also getting the same results on Win 7 64 bit. Just doing a showmessage on the hInstApp value returns 42 when I would expect to get 31 as I don't have Excel installed.
var
ExecInfo: TShellExecuteInfo;
begin
ZeroMemory(ExecInfo, sizeof(ExecInfo));
with ExecInfo do
begin
cbSize := sizeOf(ExecInfo);
fMask := SEE_MASK_NOCLOSEPROCESS;
lpVerb := PChar('open');
lpFile := PChar('c:\windows\temp\test.xls');
nShow := 1;
end;
ShellExecuteEx(@ExecInfo);
if ExecInfo.hInstApp<32
then WaitForInputIdle(ExecInfo.hProcess, 10000);
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
函数 ZeroMemory 应该用指针作为参数来调用:
ZeroMemory(@ExecInfo, sizeof(ExecInfo));
然后我将使用 Shell ExecuteEx 的结果继续:
if (ShellExecuteEx(@ExecInfo)) then
据我所知,你应该最终关闭句柄:
CloseHandle(ExecInfo.hProcess);
调用动词设置为 nil 的函数,会告诉Windows 使用标准动词,它比“open”更通用一些。
我制作了一个 示例 来等待应用程序结束,但您可以轻松替换WaitForSingleObject 和 WaitForInputIdle。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
ShelLExecuteEx 返回值与 ShelLExecute 不同。请阅读此处的文档:http://msdn。 microsoft.com/en-us/library/bb762154%28v=VS.85%29.aspx。另请检查您是否在 SHELLEXECUTEINFO 中设置了正确的标志,以便在发生错误时采取正确的行为。
ShelLExecuteEx return values are not the same as ShelLExecute. Read the documentation here: http://msdn.microsoft.com/en-us/library/bb762154%28v=VS.85%29.aspx. Also check you have set the proper flags in SHELLEXECUTEINFO for the proper behaviour when an error occurs.