代码之家  ›  专栏  ›  技术社区  ›  Exa

有没有方法从fileprotocolhandler或url.dll获取错误级别?

  •  2
  • Exa  · 技术社区  · 14 年前

    在我的某个程序中,我正在使用 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文件中尝试了错误级别的测试。

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

    但没有什么可以回应。我还试图读取%errorlevel%,但即使文件不存在,errorlevel仍为0。

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

    2 回复  |  直到 14 年前
        1
  •  2
  •   Oleg    14 年前

    在我看来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
    );
    

    顺便说一下,你可以调用函数 FileProtocolHandler 由url.dll直接导出,不启动rundll32.exe。AS pszCmdLineBuffer 你可以给 p_target_path . 不过,您不会收到任何错误信息。

    更新的 :顺便说一下,如果你用 rundll32.exe url.dll,FileProtocolHandler 只打开文件而不打开您无法使用的URL ShellExecute ShellExecuteEx 改为使用动词“open”或空(请参见 http://msdn.microsoft.com/en-us/library/bb776886.aspx )在最简单的情况下,代码可能如下所示

    hinstance hinst=shellexecute(空,文本(“open”), 文本(“c:\path\to\a.file”),空,空,0);

    你可以测试 hInst 有关错误(请参阅中的返回值 http://msdn.microsoft.com/en-us/library/bb762153.aspx )

        2
  •  2
  •   Exa    14 年前

    是的,甚至在你写评论之前,我就开始正确地阅读文档,不到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 );
    }