IProcess.CommandLine fails - Occasionally

delphi package - easy access to kernel objects etc.
Post Reply
stpe
Posts: 2
Joined: Tue Nov 28, 2006 8:02 pm

IProcess.CommandLine fails - Occasionally

Post by stpe »

Hi there,

It seems that the IProcess.CommandLine call ocasionally fails for remote processes. (As it's not a perfect world (screws break, things falls down) it does not come as a total supprise, but I would appreciate any hints as to the cause (probably myself, I know.. :wink: ))

I have isolated the problem in a small test program (1 button, and a RichEdit) and a loop where I continously do a Process('wordpad.exe') and gets the commandline, print the result and sleeps for 100 ms.

I have this program running when I start 'wordpad' as 'C:\Programmer\Windows NT\Tilbehør\wordpad.exe' and the small test-program reports this start-path correctly as the commandline.... most of the time :sorry:

If I start/stop 'wordpad.exe' a few times, suddenly the test-program reports the commandline '"wordpad.exe"' and not 'C:\Programmer\Windows NT\Tilbehør\wordpad.exe' as expected ??

Any ideas..

1) Why this happens ?
2) How you get 'back-to-normal' (i.e. start retrieving the correct path-again, without restarting the entire test-program)

Code sniplet is attached... (TurboDELPHI)

Code: Select all

procedure TForm6.Button1Click(Sender: TObject);
var ip : IProcess;
    cnt, noCLcnt: Integer;
    cl: string;
begin
  stopIt := false;  cnt := 0; noCLcnt := 0;
  while not stopIt do begin
    // I start wordpad.exe as C:\Programmer\Windows NT\Tilbehør\wordpad.exe
    ip := Process('wordpad.exe');

     if ip.IsStillRunning then begin
       cl := ip.CommandLine;
       RichEdit1.Lines.Add(intToStr(cnt)+' running: ' + cl);
     end ;

     if cl='"wordpad.exe"' then begin
       RichEdit1.Lines.Add(intToStr(cnt)+'------- ERROR: No command line!!');
       inc(noCLcnt);
       if noCLcnt>10 then stopIt := True;
     end;

     inc(cnt);

     SendMessage( RichEdit1.Handle, EM_SCROLLCARET, 0, 0);

     Application.ProcessMessages;  //Not too efficient, but it's just a test
     sleep(100);

  end;
end;
Normally I see output like:

Code: Select all

  ...
  52 running: "C:\Programmer\Windows NT\Tilbehør\wordpad.exe"  
  53 running: "C:\Programmer\Windows NT\Tilbehør\wordpad.exe" 
  54 running: "C:\Programmer\Windows NT\Tilbehør\wordpad.exe" 
  55 running: "C:\Programmer\Windows NT\Tilbehør\wordpad.exe" 
  ...
... occasionally I get:

Code: Select all

  ...
  30 running: "wordpad.exe"
  30------- ERROR: No command line!!
  31 running: "wordpad.exe"
  31------- ERROR: No command line!!
  32 running: "wordpad.exe"
  32------- ERROR: No command line!!
  ...
Kind Regards

Stefan
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

There is a specific phase where asking the command line doesn't work because the process is not fully initialized yet. If you ask the command line at that point in time you'll get only the name reported but not the command line.

The problem with refreshing is that as long as the IProcess instance is valid, madKernel keeps on reusing it. And since the command line property has a value, it's not refreshed internally. Probably I should fix that. Anyway, you can work around it by moving the command line stuff into a seperate function like this:

Code: Select all

function GetProcessCommandLine(exeFile: string) : string;
begin
  result := Process(exeFile).CommandLine;
end;
This should solve the refresh problem, I think.
stpe
Posts: 2
Joined: Tue Nov 28, 2006 8:02 pm

Post by stpe »

Great!.. You're right.. When I add the CommandLine query to a seperate function, as you suggest, I manage to get an updated (and correct) CommandLine value.

(I still get just the exe-name, right at the beginning, when the process is not initialised fully yet, but that' seems fair enough)

Thanks! :D

Stefan ;-)
Post Reply