2

私のプログラムの1つで、rundll32.exe url.dll,FileProtocolHandler c:\path\to\a.fileファイルを開くために使用しています。このファイルを開くことができなかった場合のエラーを処理したいのですが、エラーがあったかどうかを確認する方法がわかりません。それが私のコードです:

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

startDetached()rundll32.exeを含むプロセスを開くのに常に成功するため、常にtrueを返すようになりました。では、ファイルが見つかった/開いた可能性があるかどうかをどのように知ることができますか?

テスト用に*.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
);

ちなみに、FileProtocolHandlerrundll32.exeを起動せずに、url.dllによってエクスポートされた関数を直接呼び出すことができます。pszCmdLineBufferあなたが与えることができるようにp_target_path。それでも、エラー情報は表示されません。

更新:ちなみに、使用rundll32.exe url.dll,FileProtocolHandlerできるURLではなくファイルのみを開く場合、ShellExecuteまたはShellExecuteEx代わりに動詞「open」または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);

エラーをテストできます( http://msdn.microsoft.com/en-us/library/bb762153.aspxhInstの戻り値を参照) 。

于 2010-07-30T15:30:20.520 に答える
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 );
}
于 2010-08-02T06:42:32.637 に答える