2010-07-30 92 views
2

在我的一個程序中,我使用rundll32.exe url.dll,FileProtocolHandler c:\path\to\a.file來打開文件。我想處理錯誤,如果這個文件無法打開,但我不知道如何找出是否有錯誤。 這是我的代碼:有沒有辦法從FileProtocolHandler或url.dll中獲取錯誤級別?

QProcess::startDetached(QString("rundll32.exe url.dll,FileProtocolHandler " + p_target_path)); 

startDetached()現在總是返回true,因爲它總是在打開包含RUNDLL32.EXE進程succesfull。那麼如何知道我的文件是否可以被找到/打開?

我試過錯誤級別的東西在一個* .bat文件進行測試。

rundll32.exe url.dll,FileProtocolHandler c:\not_existing.exe >nul || echo Could not open file. 

但沒有任何迴應。我也嘗試讀取%ERRORLEVEL%,但即使文件不存在,錯誤級別仍然爲0.

有沒有人知道如何處理這個問題?

回答

2

在我看來,rundll32.exe是真的不返回erorlevel。你看看http://support.microsoft.com/kb/164787你可以看到,Rundll32接口沒有定義的方式來返回錯誤。

VOID CALLBACK FileProtocolHandler (
    __in HWND hwnd, 
    __in HINSTANCE ModuleHandle, 
    __in PCTSTR pszCmdLineBuffer, 
    __in INT nCmdShow 
); 

到時候你可以調用URL.DLL導出的函數FileProtocolHandler的情況下直接啓動RUNDLL32.EXE的方式。由於pszCmdLineBuffer你可以給p_target_path。儘管如此,你將不會收到錯誤信息。

修訂:順便說一下,如果你使用rundll32.exe url.dll,FileProtocolHandler只,而不是打開文件的網址比你可以使用ShellExecuteShellExecuteEx與替代動詞「打開」或NULL(見http://msdn.microsoft.com/en-us/library/bb776886.aspx)。在最簡單的情況下代碼可能如下所示

HINSTANCE hInst = ShellExecute(NULL,TEXT(「open」), TEXT(「c:\ path \ to \ a.file」),NULL,NULL,0 );

可以測試hInst的錯誤(見http://msdn.microsoft.com/en-us/library/bb762153.aspx返回值)

+0

Thx爲您的幫助,這看起來就像它可能是解決方案......但我怎樣才能使用ShellExecute和hInst值與startDetached()一起?以及如何給var ShellExecute中的第三個參數? TEXT(p_target_path)將不起作用,因爲宏將在開始時添加一個L ... – Exa 2010-08-02 05:48:33

+0

我調試了一下並測試了您發佈的代碼。當爲ShellExecute提供c:\ windows \ system32 \ cmd.exe時,hInst是「0x0000002a」...但是cmd未被打開... – Exa 2010-08-02 06:05:01

+0

我測試了其他程序並且它們確實工作正常,但hInst仍然是0x0000002a。 .. – Exa 2010-08-02 06:10:38

2

是的,你寫您的評論甚至之前,我就開始正確讀取文檔,並在更短的,然後2分鐘後,我有了解決方案:

void main_window::open_test(QString p_target_path) 
{ 
    p_target_path = p_target_path.remove("\""); 

    HINSTANCE res = ShellExecute(NULL, TEXT("open"), (LPCWSTR) p_target_path.utf16(), NULL, NULL, SW_SHOWNORMAL); 

    QString err_str = ""; 

    int res_code = (int) res; 

    switch(res_code) 
    { 
    case 0: 
     err_str = "Your operating system is out of memory or resources."; 
     break; 
    case ERROR_FILE_NOT_FOUND: 
     err_str = "The specified file was not found."; 
     break; 
    case ERROR_PATH_NOT_FOUND: 
     err_str = "The specified path was not found."; 
     break; 
    case ERROR_BAD_FORMAT: 
     err_str = "The .exe file is invalid (non-Win32 .exe or error in .exe image)."; 
     break; 
    case SE_ERR_ACCESSDENIED: 
     err_str = "Your operating system denied access to the specified file."; 
     break; 
    case SE_ERR_ASSOCINCOMPLETE: 
     err_str = "The file name association is incomplete or invalid."; 
     break; 
    case SE_ERR_DDEBUSY: 
     err_str = "The DDE transaction could not be completed because other DDE transactions were being processed."; 
     break; 
    case SE_ERR_DDEFAIL: 
     err_str = "The DDE transaction failed."; 
     break; 
    case SE_ERR_DDETIMEOUT: 
     err_str = "The DDE transaction could not be completed because the request timed out."; 
     break; 
    case SE_ERR_DLLNOTFOUND: 
     err_str = "The specified DLL was not found."; 
     break; 
    case SE_ERR_NOASSOC: 
     err_str = "There is no application associated with the given file name extension.\nThis error will also be returned if you attempt to print a file that is not printable."; 
     break; 
    case SE_ERR_OOM: 
     err_str = "There was not enough memory to complete the operation."; 
     break; 
    case SE_ERR_SHARE: 
     err_str = "A sharing violation occurred."; 
     break; 
    default: 
     return; 
    } 

    QMessageBox::warning(this, "Error", err_str); 
} 
相關問題