Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-02

TProcess

7 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Antonio Fortuny

Hi folks.

I'm trying to launch a new process from a running program. This runs on a WinCE hand held computer. To achieve this I do in the calling program:

with TProcess.Create(nil) do try
   CommandLine := wNewName;
   Options := [poNewProcessGroup];
   Logger.Add(Format('  Pocket program launching "%s"', [wNewName]), 0);
   try
     Execute;
   except
     on E: Exception do begin
       Msg := Format(DM.Constante[122], [E.Message]);
       Logger.Add(Format('  Pocket program launching error "%s"',
[E.Message]), 0);
       Result := False;
       Exit
     end;
   end;
finally
   Free
end;
The only thing I get is a "87" exception code: The parameter is incorrect.
Got the same error if I use CreateProcess, like:
function LaunchProcess(const APath: String): Boolean;
var
   si: TSTARTUPINFO;
   pi: TPROCESSINFORMATION;
   Msg: String;
   Error: Integer;
   {$IFDEF WINCE}
   wPath: WideString;
   {$ELSE}
   wPath: String;
   {$ENDIF}
begin
   FillChar(si,SizeOf(TSTARTUPINFO),#0);
   FillChar(pi,SizeOf(TPROCESSINFORMATION),#0);
   si.cb := SizeOf(TStartupInfo);
   si.dwFlags := STARTF_USESHOWWINDOW;
   si.wShowWindow := SW_SHOWDEFAULT;
   wPath := APath;
   {$IFDEF WINCE}
   Result := CreateProcess(PWideChar(wPath), nil, nil, nil, False,
CREATE_NEW_PROCESS_GROUP, nil, nil, si, pi);
   {$ELSE}
   Result := CreateProcess(nil, PChar(APath), nil, nil, False,
CREATE_NEW_PROCESS_GROUP, nil, nil, @si, @pi);
   {$ENDIF}
   if not Result then begin
     Error := GetLastError;
     Logger.Add(Format('  Pocket program "%s" launching error %d:"%s"',
[APath, Error, SysErrorMessage(Error)]), 0);
   end
end;

On a Win32 (Vista) platform it runs without problem using either TProcess or CreateProcess.
I cannot use ExecuteProcess because after launching the first program has to terminate. It is all in an automatic update process.

Any help would be appreciated.

Antonio.

Reply by email

Antonio Fortuny
to Antonio Fortuny

I have found one way to launch a new process and it works. The solution uses CreateProcess whose parameters are set accordingly to MS specifications: read http://msdn.microsoft.com/en-us/library/ms885182.aspx

In the TProcess.Execute, the parameters build for the call to CreateProcess must be adapted for WinCE. This function works either for Win32 or WinCE

function LaunchProcess(const APath: String): Boolean;
var
   pi: TPROCESSINFORMATION;
   Msg: String;
   Error: Integer;
   {$IFDEF WINCE}
   wPath: WideString;
   {$ELSE}
   wPath: String;
   {$ENDIF}
begin
   FillChar(pi,SizeOf(TPROCESSINFORMATION),#0);
   wPath := APath;
   {$IFDEF WINCE}
   Result := CreateProcess(PWideChar(wPath), nil, nil, nil, False,
CREATE_NEW_CONSOLE, nil, nil, nil, pi);
   {$ELSE}
   Result := CreateProcess(nil, PChar(APath), nil, nil, False,
CREATE_NEW_PROCESS_GROUP, nil, nil, @si, @pi);
   {$ENDIF}
   if not Result then begin
     Error := GetLastError;
     Logger.Add(Format('  Pocket program "%s" launching error %d:"%s"',
[wPath, Error, SysErrorMessage(Error)]), 0);
   end
end;

Antonio.

Reply by email

Sven Barth
to Antonio Fortuny
Antonio Fortuny wrote 7 lines
I have found one way to launch a new process and it works. The solution uses CreateProcess whose parameters are set accordingly to MS specifications: read http://msdn.microsoft.com/en-us/library/ms885182.aspx In the TProcess.Execute, the parameters build for the call to CreateProcess must be adapted for WinCE. This function works either for Win32 or WinCE

In that case you should be able to use TProcess if you set the Options accordingly (no poNewProcessGroup, but poNewConsole instead). You can see the WinCE implementation for TProcess in
$fpcsrc/packages/fcl-process/src/wince/process.inc

Just in case: No StdIn/StdOut support on WinCE (at least not without writing an additional driver).

Regards,
Sven

Reply by email

Antonio Fortuny
to Sven Barth

Le 21/02/2013 16:58, Sven Barth a ?crit :

Show quoted text 17 lines
On 21.02.2013 14:54, Antonio Fortuny wrote: > I have found one way to launch a new process and it works. The solution > uses CreateProcess whose parameters are set accordingly to MS > specifications: read > http://msdn.microsoft.com/en-us/library/ms885182.aspx > > In the TProcess.Execute, the parameters build for the call to > CreateProcess must be adapted for WinCE. This function works either for > Win32 or WinCE In that case you should be able to use TProcess if you set the Options accordingly (no poNewProcessGroup, but poNewConsole instead). You can see the WinCE implementation for TProcess in $fpcsrc/packages/fcl-process/src/wince/process.inc Just in case: No StdIn/StdOut support on WinCE (at least not without writing an additional driver).

Sounds good. I'll try it tomorrow mornig and post a comment

Show quoted text 10 lines
Regards, Sven -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Antonio Fortuny
to Sven Barth: In that case you should be able to use TProcess if you set the Options accordingly (no…

Le 21/02/2013 16:58, Sven Barth a ?crit :

Show quoted text 12 lines
On 21.02.2013 14:54, Antonio Fortuny wrote: > I have found one way to launch a new process and it works. The solution > uses CreateProcess whose parameters are set accordingly to MS > specifications: read > http://msdn.microsoft.com/en-us/library/ms885182.aspx > > In the TProcess.Execute, the parameters build for the call to > CreateProcess must be adapted for WinCE. This function works either for > Win32 or WinCE In that case you should be able to use TProcess if you set the Options accordingly (no poNewProcessGroup, but poNewConsole instead). You can
With these parameters, launching of the new process still fails with
same error code: 87
     with TProcess.Create(nil) do try
       ApplicationName := Config.PocketProgramName;
       Parameters.Clear;
       CommandLine := EmptyStr;
       Environment.Clear;
       Options := [poNewConsole];
       InheritHandles := False;
       try
         Execute;
       except
         got the exception error 87
       end
see the WinCE implementation for TProcess in $fpcsrc/packages/fcl-process/src/wince/process.inc

I've seen that FInheritHandles is forced to true in the first line of the Execute method whatever value is set in the property, while MS says it is not supported and shlould be false.

Show quoted text 14 lines
Just in case: No StdIn/StdOut support on WinCE (at least not without writing an additional driver). Regards, Sven -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Antonio Fortuny
to Antonio Fortuny

Le 22/02/2013 11:16, Antonio Fortuny a ?crit :

Show quoted text 17 lines
> In that case you should be able to use TProcess if you set the Options > accordingly (no poNewProcessGroup, but poNewConsole instead). You can With these parameters, launching of the new process still fails with same error code: 87 with TProcess.Create(nil) do try ApplicationName := Config.PocketProgramName; Parameters.Clear; CommandLine := EmptyStr; Environment.Clear; Options := [poNewConsole]; InheritHandles := False; try Execute; except got the exception error 87 end

in Win32 the above lines work as expected.

Reply by email

Sven Barth
to Antonio Fortuny: Le 21/02/2013 16:58, Sven Barth a ?crit : With these parameters, launching of the new…
Antonio Fortuny wrote 35 lines
Le 21/02/2013 16:58, Sven Barth a ?crit : > On 21.02.2013 14:54, Antonio Fortuny wrote: >> I have found one way to launch a new process and it works. The solution >> uses CreateProcess whose parameters are set accordingly to MS >> specifications: read >> http://msdn.microsoft.com/en-us/library/ms885182.aspx >> >> In the TProcess.Execute, the parameters build for the call to >> CreateProcess must be adapted for WinCE. This function works either for >> Win32 or WinCE > > In that case you should be able to use TProcess if you set the Options > accordingly (no poNewProcessGroup, but poNewConsole instead). You can With these parameters, launching of the new process still fails with same error code: 87 with TProcess.Create(nil) do try ApplicationName := Config.PocketProgramName; Parameters.Clear; CommandLine := EmptyStr; Environment.Clear; Options := [poNewConsole]; InheritHandles := False; try Execute; except got the exception error 87 end > see the WinCE implementation for TProcess in > $fpcsrc/packages/fcl-process/src/wince/process.inc I've seen that FInheritHandles is forced to true in the first line of the Execute method whatever value is set in the property, while MS says it is not supported and shlould be false.

Ah, ok. Then this could be the reason for the error. Could you please report a bug so that it isn't forgotten? (preferrably with a little test program)

Regards,
Sven

Reply by email