簡體   English   中英

ShellExecuteEx在hInstApp中返回31(預期為31)(無文件關聯)

[英]ShellExecuteEx returning 42 in hInstApp when expecting a 31 (No file association)

當在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位上也獲得相同的結果。 當我希望得到31時(因為我沒有安裝Excel),僅對hInstApp值執行showmessage會返回42。

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;

ShelLExecuteEx返回值與ShelLExecute不同。 在此處閱讀文檔: http : //msdn.microsoft.com/zh-cn/library/bb762154%28v=VS.85%29.aspx 還要檢查是否在錯誤發生時在SHELLEXECUTEINFO中設置了正確的標志,以實現正確的行為。

Nehpets:“它顯然沒有成功”。 實際上,它具有:它已成功運行RunDll32.Exe。

確保fMask成員包含SEE_MASK_FLAG_DDEWAIT。 如果不是這樣。 您可能會看到“打開方式”對話框。

應該使用指針作為參數來調用功能ZeroMemory:

ZeroMemory(@ExecInfo, sizeof(ExecInfo));

然后,我將使用Shell ExecuteEx的結果進行操作:

if (ShellExecuteEx(@ExecInfo)) then

據我所知,您應該最后關閉手柄:

CloseHandle(ExecInfo.hProcess);

在將動詞設置為nil的情況下調用該函數將告訴Windows使用標准動詞,它比“ open”更為通用。

了一個示例 ,等待應用程序結束,但是您可以輕松地用WaitForInputIdle替換WaitForSingleObject。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM