有没有办法从FileProtocolHandler或url.dll获取错误级别?

时间:2010-07-30 08:53:24

标签: qt dll rundll32

在我的一个程序中,我使用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%,但即使文件不存在,errorlevel也保持为0.

有没有人知道如何处理这个问题?

2 个答案:

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

答案 1 :(得分: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 );
}