Recent

Author Topic: TProcess running MSTSC issue with Windows 7 [SOLVED]  (Read 23276 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TProcess running MSTSC issue with Windows 7
« Reply #15 on: September 14, 2012, 01:20:31 pm »
yes I know and have already got around this message with the option not to warn on the default RDP settings. I thought I had mentioned this in my previous post :-\
Doesn't matter, having that on or off still causes the process to return to quickly.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: TProcess running MSTSC issue with Windows 7
« Reply #16 on: September 14, 2012, 01:36:43 pm »
yes I know and have already got around this message with the option not to warn on the default RDP settings. I thought I had mentioned this in my previous post :-\
Doesn't matter, having that on or off still causes the process to return to quickly.

BigChimp

  I ran your modified program on the PC in question and the program does close before the mstsc finishes with an exit code of zero if this is any help , if I run it on a Windows XP machine it does appear to behave as it should do - very strange indeed  %)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TProcess running MSTSC issue with Windows 7
« Reply #17 on: September 14, 2012, 01:44:08 pm »
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.

Code: [Select]
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.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: TProcess running MSTSC issue with Windows 7
« Reply #18 on: September 14, 2012, 01:47:17 pm »
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.

Code: [Select]
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.

I have just tried it but with the poWaitOnExit and poUsePipes together as the options and this appears to work using this test program. Can you do the same and confirm this - although it may behave only when outputting the startrdp into a stringlist variable as I have just tried it with the poUsePipes without doing this and it still dropped out.
« Last Edit: September 14, 2012, 01:56:36 pm by timcs »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TProcess running MSTSC issue with Windows 7
« Reply #19 on: September 14, 2012, 01:58:59 pm »
Just using poWaitOnExit and poUsePipes together still doesn't work.
Please post your code.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12980
  • FPC developer.
Re: TProcess running MSTSC issue with Windows 7
« Reply #20 on: September 14, 2012, 02:11:46 pm »

The same does not work for mstsc.exe. Could be a UAC thing, don't know.

That would be my guess too. Escalating privileges can meanthat exe's restart themselves (with higher permissions). Tprocess waits for the first child only.

So see if you can print the PID waited on, and then look in taskmgr at the pid of the surviving process?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TProcess running MSTSC issue with Windows 7
« Reply #21 on: September 14, 2012, 02:28:21 pm »
Am going to have a look. Presumably the processids increase only? So if I have the originating processID:
Code: [Select]
    startrdp.Execute;
    processid:=startrdp.ProcessID;
... then if there are multiple mstsc processes, I know I only have to look at higher IDs?

Messy, but I suppose it's the best way...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: TProcess running MSTSC issue with Windows 7
« Reply #22 on: September 14, 2012, 02:29:05 pm »
Just using poWaitOnExit and poUsePipes together still doesn't work.
Please post your code.

as requested :

Code: [Select]
unit rdpt;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,Process;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormActivate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  startrdp : TProcess;
  ipstring : string;
  logfile : TStringlist;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormActivate(Sender: TObject);
begin
  startrdp := TProcess.Create(nil);
  ipstring := '';
  form1.hide;
  form1.WindowState:=wsMinimized;
  logfile := TStringList.create;
  if ParamCount >0 then
  begin
       ipstring := paramstr(1);
       startrdp.Executable := 'mstsc ';
       startrdp.Parameters.Add('/v');
       startrdp.Parameters.Add(ipstring);
       startrdp.Options := startrdp.Options + [poWaitOnExit, poUsePipes];
       startrdp.Execute;
       logfile.LoadFromStream(startrdp.Output);
       messagedlg('Thinks this link has finished',mtinformation,[mbok],0);
       startrdp.Free;
       logfile.SaveToFile('c:\rdplink\rdptest.log');
       logfile.free;
       messagedlg('closing program now',mtinformation,[mbok],0);
       close;
       application.Terminate;

  end
  else
  begin
      messagedlg('No parameters found closing program',mtinformation,[mbok],0);
      close;
      application.Terminate;
  end;



end;

end.



timcs

  • Full Member
  • ***
  • Posts: 213
Re: TProcess running MSTSC issue with Windows 7
« Reply #23 on: September 14, 2012, 02:32:22 pm »

The same does not work for mstsc.exe. Could be a UAC thing, don't know.

That would be my guess too. Escalating privileges can meanthat exe's restart themselves (with higher permissions). Tprocess waits for the first child only.

So see if you can print the PID waited on, and then look in taskmgr at the pid of the surviving process?

You may well be onto to something here. Typical for Windows vista/7 to be like this  ::)

Am going to have a look. Presumably the processids increase only? So if I have the originating processID:
Code: [Select]
    startrdp.Execute;
    processid:=startrdp.ProcessID;
... then if there are multiple mstsc processes, I know I only have to look at higher IDs?

Messy, but I suppose it's the best way...

BigChimp

  thanks for this , if you can crack it that would be great! if you need me to do anything let me know. My only problem will be changing actual settings on the PC as at work such changes have been restricted  ::)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TProcess running MSTSC issue with Windows 7
« Reply #24 on: September 14, 2012, 02:32:30 pm »
Great timcs, your last code snippet http://lazarus.freepascal.org/index.php/topic,18189.msg102472.html#msg102472 actually seems to work correctly on my Vista box!
If this works for you, perhaps it's the least messy way...
« Last Edit: September 14, 2012, 02:39:02 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: TProcess running MSTSC issue with Windows 7
« Reply #25 on: September 14, 2012, 02:51:08 pm »
Great timcs, your last code snippet http://lazarus.freepascal.org/index.php/topic,18189.msg102472.html#msg102472 actually seems to work correctly on my Vista box!
If this works for you, perhaps it's the least messy way...

Do you think it worked because of the logging? I only did this to see if it reported anything but it does not write anything back into the file. It is odd though that this works is it not?  %)  %) %) %) %)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TProcess running MSTSC issue with Windows 7
« Reply #26 on: September 14, 2012, 03:05:30 pm »
I think it's just the pipes... the program below works, too, even though it does not save the stringlist contents.
And yes, it's very odd that this works!

Perhaps some gurus can shed some light on this.

Code: [Select]
program rdptest;

{$mode objfpc}{$H+}


uses
  Classes, SysUtils,Process;


var
  startrdp : TProcess;
  ipstring : string;
  logfile : TStringlist;

begin
  ipstring := '';
  if ParamCount<1 then
  begin
    writeln('Please specify host ip/address as first argument.');
    writeln('Aborting.');
    halt(1);
  end;
  ipstring := paramstr(1); 
  logfile := TStringList.create;
  startrdp := TProcess.Create(nil); 
  try
    startrdp.Executable := 'mstsc ';
    startrdp.Parameters.Add('/v');
    startrdp.Parameters.Add(ipstring);
    startrdp.Options := startrdp.Options + [poWaitOnExit, poUsePipes];
    startrdp.Execute;
    logfile.LoadFromStream(startrdp.Output);
    writeln('This link has finished');
    //Note we do nothing with the pipe output in stringlist:
    //logfile.SaveToFile('c:\rdplink\rdptest.log');
  finally
    logfile.free;
    startrdp.free;
  end;
end.

PS: note how my .free statements occur in the finally part of the try..finally... (instead of in the try part) you might want to do that yourself in your code ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: TProcess running MSTSC issue with Windows 7
« Reply #27 on: September 14, 2012, 03:16:51 pm »
I think it's just the pipes... the program below works, too, even though it does not save the stringlist contents.
And yes, it's very odd that this works!

Perhaps some gurus can shed some light on this.

Code: [Select]
program rdptest;

{$mode objfpc}{$H+}


uses
  Classes, SysUtils,Process;


var
  startrdp : TProcess;
  ipstring : string;
  logfile : TStringlist;

begin
  ipstring := '';
  if ParamCount<1 then
  begin
    writeln('Please specify host ip/address as first argument.');
    writeln('Aborting.');
    halt(1);
  end;
  ipstring := paramstr(1); 
  logfile := TStringList.create;
  startrdp := TProcess.Create(nil); 
  try
    startrdp.Executable := 'mstsc ';
    startrdp.Parameters.Add('/v');
    startrdp.Parameters.Add(ipstring);
    startrdp.Options := startrdp.Options + [poWaitOnExit, poUsePipes];
    startrdp.Execute;
    logfile.LoadFromStream(startrdp.Output);
    writeln('This link has finished');
    //Note we do nothing with the pipe output in stringlist:
    //logfile.SaveToFile('c:\rdplink\rdptest.log');
  finally
    logfile.free;
    startrdp.free;
  end;
end.

PS: note how my .free statements occur in the finally part of the try..finally... (instead of in the try part) you might want to do that yourself in your code ;)

Cheers for this BigChimp and all who have contributed to this. I will take in what you have said about the try..finally

 

TinyPortal © 2005-2018