2

在我rundll32.exe url.dll,FileProtocolHandler c:\path\to\a.file用来打开文件的一个程序中。如果无法打开此文件,我想处理错误,但我不知道如何找出是否存在错误。那是我的代码:

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

startDetached()现在总是返回 true,因为它总是成功打开包含 rundll32.exe 的进程。那么我怎么知道我的文件是否可以找到/打开?

我在 *.bat 文件中尝试了 errorlevel-things 进行测试。

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

但是没有任何回应。我还尝试读取 %ERRORLEVEL%,但即使文件不存在,错误级别仍为 0。

有谁知道如何解决这个问题?

4

2 回答 2

2

在我看来,rundll32.exe 真的没有返回错误级别。如果您查看http://support.microsoft.com/kb/164787,您会看到,Rundll32 接口没有定义返回错误的方法。

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

顺便可以FileProtocolHandler直接调用url.dll导出的函数,不用启动rundll32.exe。你pszCmdLineBuffer可以给p_target_path. 尽管如此,您不会收到任何错误信息。

更新:顺便说一句,如果您rundll32.exe url.dll,FileProtocolHandler仅用于打开文件而不是 URL,则可以使用动词“打开”或 NULL(请ShellExecute参阅http://msdn.microsoft.com/en-us/library/bb776886.aspx)。在最简单的情况下,代码可能如下所示ShellExecuteEx

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中的返回值)

于 2010-07-30T15:30:20.520 回答
2

Yep, even before you wrote your comment, I started to read the documentation properly and in less then 2 minutes I had the solution:

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 );
}
于 2010-08-02T06:42:32.637 回答