Board index » delphi » HELP: Waiting for a shelled program to finish.

HELP: Waiting for a shelled program to finish.

Can someone help, I am using D3, and trying to shell to a dos program,
and wait for it to finish. (see attempted code below)
I have tried the following code, but the waitforsingleobject returns
before the dos program finishes, so I guess that's not the right thing
to use.
Anyone got any ideas?

Dodgy

procedure progmodule;
var h:hwnd;
begin

h:=ShellExecute(GetDesktopWindow(),'open',PChar(extractfilepath(application.exename)+'myprogram.exe'),nil,nil,SW_SHOW);

  If WaitForSingleObject(h, 50000) <> WAIT_TIMEOUT then
  begin
//      Didn't work bit goes here!
  end;
end;

 

Re:HELP: Waiting for a shelled program to finish.


Hi Dodgy,

Use CreateProcess
ex
var SI: TStartupInfo; PI:TProgressInformation;
begin
   FillChar(PI,SizeOf(PI),#0);  
   FillChar(SI,SizeOf(PI),#0);  
   with SI do begin
        dwFlags:=STARTF_USESHOWWINDOW;
        wShowWindow:=SW_SHOW;
        end;
   if not CreateProcess(nil,
      PChar(extractfilepath(application.exename)+'myprogram.exe'),
      nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,SI,PI) then
      raise
      Exception.Create('Error CreateProcess');
      WaitForSingleObject(PI.dwProcessID, INFINITE);
   end;

Rudy,
=============================================================
Dodgy schreef:

Quote

> Can someone help, I am using D3, and trying to shell to a dos program,
> and wait for it to finish. (see attempted code below)
> I have tried the following code, but the waitforsingleobject returns
> before the dos program finishes, so I guess that's not the right thing
> to use.
> Anyone got any ideas?

> Dodgy

> procedure progmodule;
> var h:hwnd;
> begin

> h:=ShellExecute(GetDesktopWindow(),'open',PChar(extractfilepath(application.exename)+'myprogram.exe'),nil,nil,SW_SHOW);

>   If WaitForSingleObject(h, 50000) <> WAIT_TIMEOUT then
>   begin
> //      Didn't work bit goes here!
>   end;
> end;

Re:HELP: Waiting for a shelled program to finish.


Cheers Rudy.
Quick question...
Could I just use GetStartupInfo(SI) to fill the Startupinfo?

Dodgy.

On Mon, 15 Mar 1999 20:13:37 +0100, Rudy Versele
<rudy.vers...@pandora.be> waffled on about something:

Quote
>Hi Dodgy,

>Use CreateProcess
>ex
>var SI: TStartupInfo; PI:TProgressInformation;
>begin
>   FillChar(PI,SizeOf(PI),#0);      
>   FillChar(SI,SizeOf(PI),#0);      
>   with SI do begin
>    dwFlags:=STARTF_USESHOWWINDOW;
>    wShowWindow:=SW_SHOW;
>    end;
>   if not CreateProcess(nil,
>      PChar(extractfilepath(application.exename)+'myprogram.exe'),
>      nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,SI,PI) then
>      raise
>      Exception.Create('Error CreateProcess');
>      WaitForSingleObject(PI.dwProcessID, INFINITE);
>   end;

>Rudy,
>=============================================================
>Dodgy schreef:

>> Can someone help, I am using D3, and trying to shell to a dos program,
>> and wait for it to finish. (see attempted code below)
>> I have tried the following code, but the waitforsingleobject returns
>> before the dos program finishes, so I guess that's not the right thing
>> to use.
>> Anyone got any ideas?

>> Dodgy

>> procedure progmodule;
>> var h:hwnd;
>> begin

>> h:=ShellExecute(GetDesktopWindow(),'open',PChar(extractfilepath(application.exename)+'myprogram.exe'),nil,nil,SW_SHOW);

>>   If WaitForSingleObject(h, 50000) <> WAIT_TIMEOUT then
>>   begin
>> //      Didn't work bit goes here!
>>   end;
>> end;

Re:HELP: Waiting for a shelled program to finish.


Hi Dodgy,

Quote
Dodgy wrote:

> Could I just use GetStartupInfo(SI) to fill the Startupinfo?

Why do you want to do such thing?
If you use GetStartupInfo before running CreateProcess then
I think that you can't know the contents of the startupinfo.
Of course you can change them all.

 TStartupInfo = record
    cb: DWORD;
    lpReserved: Pointer;
    lpDesktop: Pointer;
    lpTitle: Pointer;
    dwX: DWORD;
    dwY: DWORD;
    dwXSize: DWORD;
    dwYSize: DWORD;
    dwXCountChars: DWORD;
    dwYCountChars: DWORD;
    dwFillAttribute: DWORD;
    dwFlags: DWORD;
    wShowWindow: Word;
    cbReserved2: Word;
    lpReserved2: PByte;
    hStdInput: THandle;
    hStdOutput: THandle;
    hStdError: THandle;
  end;

-------------------------------------------------------
ps: I maked some clerical errors in the previous respons

var SI: TStartupInfo; PI:TProgressInformation;
begin
   FillChar(PI,SizeOf(PI),#0);
   FillChar(SI,SizeOf(SI),#0);        //!
   with SI do begin
       cb         := SizeOf(SI);      //!
       dwFlags    := STARTF_USESHOWWINDOW;
       wShowWindow:= SW_SHOW;
       end;
   if not CreateProcess(nil,
      PChar(extractfilepath(application.exename)+'myprogram.exe'),
      nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,SI,PI) then
      raise
      Exception.Create('Error CreateProcess');
      WaitForSingleObject(PI.dwProcessID, INFINITE);
   end;

Re:HELP: Waiting for a shelled program to finish.


It was just something I saw in an example... I thought it was a clever
trick or something!
I'm a bit new when it comes to playing with the api, so you'll have to
forgive me being completely thick!

Dodgy.

On Tue, 16 Mar 1999 13:33:06 +0100, Rudy Versele
<rudy.vers...@pandora.be> waffled on about something:

Quote
>Hi Dodgy,

>Dodgy wrote:

>> Could I just use GetStartupInfo(SI) to fill the Startupinfo?

>Why do you want to do such thing?
>If you use GetStartupInfo before running CreateProcess then
>I think that you can't know the contents of the startupinfo.
>Of course you can change them all.

> TStartupInfo = record
>    cb: DWORD;
>    lpReserved: Pointer;
>    lpDesktop: Pointer;
>    lpTitle: Pointer;
>    dwX: DWORD;
>    dwY: DWORD;
>    dwXSize: DWORD;
>    dwYSize: DWORD;
>    dwXCountChars: DWORD;
>    dwYCountChars: DWORD;
>    dwFillAttribute: DWORD;
>    dwFlags: DWORD;
>    wShowWindow: Word;
>    cbReserved2: Word;
>    lpReserved2: PByte;
>    hStdInput: THandle;
>    hStdOutput: THandle;
>    hStdError: THandle;
>  end;

>-------------------------------------------------------
>ps: I maked some clerical errors in the previous respons

>var SI: TStartupInfo; PI:TProgressInformation;
>begin
>   FillChar(PI,SizeOf(PI),#0);
>   FillChar(SI,SizeOf(SI),#0);        //!
>   with SI do begin
>       cb         := SizeOf(SI);      //!
>       dwFlags    := STARTF_USESHOWWINDOW;
>       wShowWindow:= SW_SHOW;
>       end;
>   if not CreateProcess(nil,
>      PChar(extractfilepath(application.exename)+'myprogram.exe'),
>      nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,SI,PI) then
>      raise
>      Exception.Create('Error CreateProcess');
>      WaitForSingleObject(PI.dwProcessID, INFINITE);
>   end;

Re:HELP: Waiting for a shelled program to finish.


Quote
>>... Rudy Versele waffled on about something ...

"Waffled"? Sorry, I know: my english is rubbish.
I wanted to write you a friendly answer. Sorry, if it wasn't.

What I would to explain you was:
The Startupinfo change each time that an app use it.
If you get the startupinfo, without changing the contents,
then take you a gamble.

Rudy
--------------------------------------------------------------
procedure WaitForShelledProgram(sExe,sCmdLine: string);
var SI: TStartupInfo; PI:TProgressInformation;
begin
   FillChar(PI,SizeOf(PI),#0);
   FillChar(SI,SizeOf(SI),#0);        
   with SI do begin
       cb         := SizeOf(SI);    
       dwFlags    := STARTF_USESHOWWINDOW;
       wShowWindow:= SW_SHOW;
       end;
   if not CreateProcess(PChar(sExe),PChar(sCmdLine),
      nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,SI,PI) then
      raise
      Exception.Create('Error CreateProcess');
      WaitForSingleObject(PI.dwProcessID, INFINITE);
   end;
ex.
WaitForShelledProgram('c:\Windows\PBrush.exe', 'PBrush.exe xx.bmp');

Re:HELP: Waiting for a shelled program to finish.


Sorry about the waffle bit, it's not really that bad, it's just slang
for 'went on about something'.
It was nothing personal!
I really do appeciate the help.... I just have it configured on my
news reader to say that, as I got bored with the standard "so and so
said...." line!

Dodgy.

On Thu, 18 Mar 1999 09:19:07 +0100, Rudy Versele
<rudy.vers...@pandora.be> waffled on about something:

Quote
>>>... Rudy Versele waffled on about something ...

>"Waffled"? Sorry, I know: my english is rubbish.
>I wanted to write you a friendly answer. Sorry, if it wasn't.

>What I would to explain you was:
>The Startupinfo change each time that an app use it.
>If you get the startupinfo, without changing the contents,
>then take you a gamble.

>Rudy
>--------------------------------------------------------------
>procedure WaitForShelledProgram(sExe,sCmdLine: string);
>var SI: TStartupInfo; PI:TProgressInformation;
>begin
>   FillChar(PI,SizeOf(PI),#0);
>   FillChar(SI,SizeOf(SI),#0);        
>   with SI do begin
>       cb         := SizeOf(SI);    
>       dwFlags    := STARTF_USESHOWWINDOW;
>       wShowWindow:= SW_SHOW;
>       end;
>   if not CreateProcess(PChar(sExe),PChar(sCmdLine),
>      nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,SI,PI) then
>      raise
>      Exception.Create('Error CreateProcess');
>      WaitForSingleObject(PI.dwProcessID, INFINITE);
>   end;
>ex.
>WaitForShelledProgram('c:\Windows\PBrush.exe', 'PBrush.exe xx.bmp');

Other Threads