Search code examples
qtdllrundll32

Is there a way to get an errorlevel from FileProtocolHandler or url.dll?


In one of my programs I am using rundll32.exe url.dll,FileProtocolHandler c:\path\to\a.file to open files. I would like to handle errors in case this file could not be opened but I can't figure out how to find out if there was an error or not. That's my code:

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

startDetached() now always returns true, because it's always succesfull in opening a process containing rundll32.exe. So how do I know if my file could be found/opened or not?

I tried errorlevel-things in a *.bat file for testing.

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

But there is nothing being echoed. I also tried to read the %ERRORLEVEL%, but even if the file is not existing the errorlevel remains 0.

Does anyone know a way to find out how to deal with this?


Solution

  • It seems to me that rundll32.exe is really not return an erorlevel. It you look at http://support.microsoft.com/kb/164787 you can see, that Rundll32 Interface has no defined way to return error.

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

    By the way you can call the function FileProtocolHandler exported by url.dll directly without starting rundll32.exe. As pszCmdLineBuffer you can give p_target_path. Nevertheless you will receive no error information.

    UPDATED: By the way if you use rundll32.exe url.dll,FileProtocolHandler to open files only and not URLs than you can use ShellExecute or ShellExecuteEx instead with the verb "open" or NULL (see http://msdn.microsoft.com/en-us/library/bb776886.aspx). In the simplest case the code could looks like following

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

    You can test hInst for the errors (see Return Value in http://msdn.microsoft.com/en-us/library/bb762153.aspx)