delphidelphi-xe2

CreateProcess call returns error code 50


Try to use Createprocess to start windows Explorer in a given path, but I keep getting

System Error. Code 50. The request is not supported.

What am i doing wrong?

procedure TfrmProjectManager.OpenFolderinExplorer(const aPath: string);
  function GetWinDir: String;
  var
    Buffer: array[0..MAX_PATH] of Char;
  begin
    GetWindowsDirectory(Buffer, SizeOf(Buffer));
    SetString(Result, Buffer, StrLen(Buffer));
  end;

var
  strCmdLine    : String;
  fStartInfo    : TStartupInfo;
  fProcessInfo  : TProcessInformation;

begin

  try
    if sysutils.DirectoryExists(aPath) or 
       (MessageDlg('Folder [%s] not found. Create it?', mtConfirmation, mbYesNo, 0)=mrYes) then
    begin
      sysutils.ForceDirectories(aPath);
      FillChar(fStartInfo,sizeof(fStartInfo),0);
      FillChar(fPRocessInfo, Sizeof(fProcessInfo),0);

      fStartInfo.cb:=sizeof(fStartInfo);
      fStartInfo.lpReserved := nil;
      fStartInfo.lpDesktop := nil;
      fStartInfo.lpTitle := nil;
      fStartInfo.dwFlags := STARTF_USESHOWWINDOW ;
      fStartInfo.wShowWindow := SW_SHOW;
      fStartInfo.cbReserved2 := 0;
      fStartInfo.lpReserved2 := nil;

      strCmdLine := '"' + GetWinDir + '\explorer.exe"';

    if not CreateProcess(nil,PChar(strCmdLine),nil,nil,False, 0,nil,PChar(aPath),fStartInfo,fProcessInfo) then
     RaiseLastOSError;

  end

  except
    on E:TObject do
      if not IsAbortException(E) then
        raise;
  end;
end;

I tried various combinations of parameters in CreateProcess, but just don't seem able to find the correct one.


Solution

  • I'd say that you shouldn't be using CreateProcess here. Rather than debugging your CreateProcess I'll offer you what I believe to be the right way to open a shell view onto a folder. Call ShellExecute.

    ShellExecute(0, '', PChar(aPath), '', '', SW_SHOWNORMAL);
    

    This way you let the shell decide on the appropriate way to display the folders contents to the user.