Board index » delphi » Help with running a program from within my program

Help with running a program from within my program

Hi All,

I need to know how to run a program (e.g. notepad) from within my program
but I also need to know when the other program in closed. I am currently
using ShellExecute() to launch an application. This runs Notepad fine and
then returns control back to my program. I need to know when some one closes
notepad or any other application that I have spawned.

I have tried the followig but the computer crashes:

Could you please help me...

Thanks in advance
Vance

----------< code below >---------

var
SI : TStartupInfo;
PI : TProcessInformation;

begin

   if (CreateProcess (Nil, 'C:\Windows\Sol.exe', Nil, Nil, False, 0, Nil,
Nil, SI, PI)) then
   begin
     WaitForSingleObject (PI.hProcess,  INFINITE);
   end;
end;

--------< end of code >--------

 

Re:Help with running a program from within my program


Vance,
sorry I have no time to check what's wrong with your code (probably it
crashes because you do not initialize StartInfo) but here is my function
which _works_:

{----------------------------------------------------------------------}

function vt_ptl(AppHandle: HWND; AppDir: PChar; fName: PChar): boolean;
const
  Mess = 'Cannot create the process. OS error code = ';
var
  StartInfo: TStartupInfo;
  ProcInfo:  TProcessInformation;
  ExtStyle:  DWORD;
  s, key: string;
  Events: array[0..2] of THandle;
  theEvent:  DWORD;
  ExeName: string;
  i: integer;
begin

  ExeName := AppDir;
  ExeName := ExeName + 'ProtEdit.exe';

  { Create Events: }
  Events[0] := CreateEvent(nil, false, false, EventNameSaved);
  Events[1] := CreateEvent(nil, false, false, EventNameNotSaved);

  { Get StartupInfo of the calling process in order to use it for
    CreateProcess call: }
  GetStartupInfo(StartInfo);

  { Save ExtStyle of the calling process: }
  ExtStyle := GetWindowLong(AppHandle, GWL_EXSTYLE);

  try

    { Hide the calling application: }
    ShowWindow(AppHandle, SW_HIDE);
    { and remove it from the application chain: }
    SetWindowLong(AppHandle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);

    if CreateProcess(
      PChar(ExeName), // name of executable module
      PChar(ExeName + ' ' + fName), // command line string
      nil,        // pointer to process security attributes
      nil,        // pointer to thread security attributes
      false,      // handle inheritance flag
      0,          // creation flags
      nil,        // pointer to new environment block
      nil,        // pointer to current directory name
      StartInfo,  // pointer to STARTUPINFO
      ProcInfo    // pointer to PROCESS_INFORMATION
      ) then begin
         Events[2] := ProcInfo.hProcess;
         theEvent := WaitForMultipleObjects(3, @Events, false,INFINITE);
         { editor is done! }
         for i := 0 to 2 do
           CloseHandle(Events[i]);
         CloseHandle(ProcInfo.hThread);
       end
    else
      MessageBox(AppHandle, PChar(Mess + IntToStr(GetLastError)), nil,
                 MB_ICONHAND or MB_OK or MB_APPLMODAL);
  finally
    { restore the calling process state: }
    SetWindowLong(AppHandle, GWL_EXSTYLE, ExtStyle);
    ShowWindow(AppHandle, SW_SHOW);
  end;

  result := theEvent = WAIT_OBJECT_0;

end;

{----------------------------------------------------------------------}

You do not need all this stuff (I'm calling my own program, which calls
SetEvent to signal its state), but I hope you can do what you need on
this basis.

Happy coding, Eugene

Quote
On Wed, 25 Mar 1998, Vance Landall <fireb...@ihug.co.nz> wrote:
> I need to know how to run a program (e.g. notepad) from within my program
> but I also need to know when the other program in closed. I am currently
> using ShellExecute() to launch an application. This runs Notepad fine and
> then returns control back to my program. I need to know when some one closes
> notepad or any other application that I have spawned.

> I have tried the followig but the computer crashes:

> Could you please help me...

> Thanks in advance
> Vance

--
Eugene I Levin
Bielefeld, Germany

I'm sorry for corrupting my Email in the header.
Here is the right one:
L e v i n @ l h . b i c o s . d e

Other Threads