当预期返回 31 时,ShellExecuteEx 在 hInstApp 中返回 42(无文件关联)

发布于 12-06 06:52 字数 892 浏览 0 评论 0原文

当在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹2024-12-13 06:52:54

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.

油焖大侠2024-12-13 06:52:54

函数 ZeroMemory 应该用指针作为参数来调用:

ZeroMemory(@ExecInfo, sizeof(ExecInfo));

然后我将使用 Shell ExecuteEx 的结果继续:

if (ShellExecuteEx(@ExecInfo)) then

据我所知,你应该最终关闭句柄:

CloseHandle(ExecInfo.hProcess);

调用动词设置为 nil 的函数,会告诉Windows 使用标准动词,它比“open”更通用一些。

我制作了一个 示例 来等待应用程序结束,但您可以轻松替换WaitForSingleObject 和 WaitForInputIdle。

The function ZeroMemory should be called with a pointer as parameter:

ZeroMemory(@ExecInfo, sizeof(ExecInfo));

Then i would use the result of Shell ExecuteEx to proceed:

if (ShellExecuteEx(@ExecInfo)) then

As far as i know you should close the handle in the end:

CloseHandle(ExecInfo.hProcess);

Calling the function with the verb set to nil, will tell Windows to use the standard verb, it is a bit more generic than 'open'.

I made an example to wait on an application to end, but you could easily replace WaitForSingleObject with WaitForInputIdle.

著墨染雨君画夕2024-12-13 06:52:54

Nehpets:“它显然没有成功”。
实际上,它已经成功运行了 RunDll32.Exe。

确保 fMask 成员包含 SEE_MASK_FLAG_DDEWAIT。
如果没有的话。您可能会看到“打开方式”对话框。

Nehpets: "it plainly hasn't succeeded".
Actually, it has: it successfully ran RunDll32.Exe.

Make sure the fMask member contains SEE_MASK_FLAG_DDEWAIT.
If it doesn't. you may see the "Open With" dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文