Jump to content

Don't use ShellExecute on a windows Service


Abaksoft

Recommended Posts

Good moorning Friends,

Just wanted to advise.

- ShellExecute works fine on VCL Desktop applications only.

- ShellExecute does'nt work on a Windows Service !   

Because of Windows Security (Session0, Session 1)  introduced after Windows 7 

 

use instead CreateProcessAsUser  like this :



procedure TMainForm.RunApp(cmdLine, WorkDir: String);
var
  hToken: THandle;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  res: boolean;

begin
   GetStartupInfo(StartupInfo);

   if WTSQueryUserToken(WtsGetActiveConsoleSessionID, hToken) then
   begin

    { 1. For an application.exe  (exemple : NotePad.exe)

     res := CreateProcessAsUser(hToken,
               PChar(Path_appName),
               PChar(cmdLine),
               nil,
               nil,
               False,
               CREATE_NEW_CONSOLE,
               nil,
               PChar(WorkDir),
               StartupInfo,
               ProcessInfo);
    }



    {2. For a DOS command line   (Example FireBird :
           cmdLine:='gbak.exe -user sysdba -pas masterkey -b -nt "D:\Base.fdb" "D:\toto.fbk" ';
           WorkDir:='C:\Program Files\Firebird\Firebird_2_5\bin';
    }
     res := CreateProcessAsUser(hToken,
               Nil,
               PChar('cmd.exe /C ' + cmdLine),
               nil,
               nil,
               True,
               CREATE_NO_WINDOW,
               nil,
               PChar(WorkDir),
               StartupInfo,
               ProcessInfo);


     if res then WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
   end;

end;

That will save you much time :)

Regards.

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...