Example program that demonstrates cmd /c notepad.exe <bla> properly waits for notepad to close.
The same does not work for mstsc.exe. Could be a UAC thing, don't know.
program consolerdp;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes,Process;
var
ipstring: string;
startrdp: tprocess;
begin
// Demonstrates waiting for a process using cmd /c
if ParamCount<1 then
begin
writeln('Please specify the IP address/hostname as the first argument.');
writeln('Aborting');
halt(1); //pass error code, useful in batch files
end
else
begin
ipstring:=paramstr(1); // the first argument on the command line
end;
startrdp := TProcess.Create(nil);
try
// We're starting up a new command interpreter that immediately
// runs notepad with the given parameters.
// This one seems to work well:
startrdp.Executable := 'cmd.exe';
startrdp.Parameters.Add('/c');
startrdp.Parameters.Add('notepad.exe');
// We test parameter passing with this bogus parameter:
startrdp.Parameters.Add(ipstring);
startrdp.Options := startrdp.Options + [poWaitOnExit];
startrdp.Execute;
writeln('Finished with notepad. Let''s try remote desktop:');
startrdp.Parameters.clear;
{
BigChimp: this doesn't work either:
startrdp.Executable:='';
startrdp.CommandLine:='cmd /c "mstsc /v '+ipstring+'"';
}
startrdp.Executable := 'cmd.exe';
startrdp.Parameters.Add('/c');
startrdp.Parameters.Add('mstsc.exe');
startrdp.Parameters.Add('/v');
startrdp.Parameters.Add(ipstring);
startrdp.Options := startrdp.Options + [poNewConsole,poWaitOnExit];
startrdp.Execute;
writeln('Finished with mstsc.');
finally
startrdp.free
end;
end.