Solved

Call Delphi Program from another Delphi program

Posted on 2004-04-03
7
299 Views
Last Modified: 2011-04-14
Hi all..

Let's say I have 2 Delphi programs.. just call it Father and Son.

Father has 50 statements and Son has 10 statements to execute when they are called/runned.

If first we run Father, at statement no 20, Father will call Son. When this happens, Son will execute his 10 statements. I want to make it such that: the Father will only continue to run his statements (statement 21 - 50) AFTER Son has finished executing his 10 statements. So it's like Father will wait until the Son finishes his statements then the Father will continue his statements.

Is there any way I can do this? I think for calling another delphi program, we have to use winexec is it?

Thankss for your help :)
0
Comment
Question by:pipinana2002
7 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10747820
Better use CreateProcess for this purpose

procedure ExecExtern;
const
  MAXTIME=5000;           // max wait 5 seconds
var
  tp,tk: dword;
  exitcode: dword;
  procinf: PROCESS_INFORMATION;
  stinfo: STARTUPINFO;
begin
      GetStartupInfo(stinfo);
// if You want to show Son
      stinfo.wShowWindow := SW_SHOW
// if You want to hide Son
      stinfo.wShowWindow := SW_HIDE;
// start son
      CreateProcess(nil,PChar('YourSon.exe'),nil,nil,FALSE,0,nil,nil,stinfo,procinf);
// waiting for Son end not more then MAXTIME
      tp := GetTickCount;
      while tp + MAXTIME > GetTickCount do
          begin
//          Application.ProcessMessages;
          if not GetExitCodeProcess(procinf.hProcess,exitcode) then
            break;
          if exitcode <> STILL_ACTIVE then
            break;
          end;
end;
0
 

Accepted Solution

by:
TomGrills earned 50 total points
ID: 10747830
Here you go....  Just add this procedure to Father and call it when Father completes line 20...   Son will run and then Father will pick up on 21....

Procedure RunSon;
var Params,CmdStr,tempFileName:string;
    Info: TShellExecuteInfo;
    Show:Word;
    ExitCode:Longint;
    OK,Wait:Boolean;
begin
  Params:='';  <<<  Paramter List for Son.exe  (I assumed that Son.exe would have none)
  show:=SW_SHOWNORMAL;  <<< Window Show State for Son.exe
  CmdStr:='C:\Your Folder Name\Son.exe";
  FillChar(Info, SizeOf(Info), Chr(0));
  Info.cbSize := SizeOf(Info);
  Info.fMask := SEE_MASK_NOCLOSEPROCESS;
  Info.lpVerb := pchar('open');
  Info.lpFile := pchar(CmdStr);
  Info.lpParameters := pchar(Params);
  Info.lpDirectory := pchar(extractFileDir(CmdStr));
  Info.nShow := show;

  wait:=True;  <<<  This tells Father to wait for the Son...  you can use this as a paramter if you create any other Sons that will run concurrently

  OK := Boolean(ShellExecuteEx(@Info)); <<  This executes Son and gets the result;

  if OK then
  begin
    if wait then
    begin
      windowstate:=WSMinimized; <<<  minimizes Father till Son ends....

      while WaitForSingleObject(Info.hProcess,0)<>WAIT_OBJECT_0 do application.ProcessMessages ;  <<<  Waiting....

      OK := GetExitCodeProcess(Info.hProcess, DWord(ExitCode));   <<<  Gets Son's exit code....

      windowstate:=WSNormal;  <<<  Restore's Father to nromal state

    end;
 end else
   showmessage('Son.exe Application Execution Failure');
end;
0
 

Expert Comment

by:TomGrills
ID: 10747837
you really don't need the tmpFileName variable in my example...  I modified some old code I had.....
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:pipinana2002
ID: 10750593
Hi all..

Thank you for all your answer.. I will test the code first and I'll get back to u :) Thanksssss...
0
 
LVL 3

Expert Comment

by:BJThomsen
ID: 10769367
Is the a reason for Son to be a exe? If not why not make Son a dll. Then simply call it at line 20.
0
 
LVL 1

Expert Comment

by:bnemmers
ID: 10785235
I wrote a IPC components designed to allow InterProcess Communications between to processes
try this link at torrys  http://www.torry.net/vcl/system/appcommunications/bmipc.zip

What I used it for in the past was to send a message to another one of my apps to do something.
So what you could do is have the father wait until the son sends a message to continue on with the rest of the process

Bill
0
 

Author Comment

by:pipinana2002
ID: 10790009
HI all.. thanks for all your answer, especially to TomGrills.. hehehe I finally can make i work ;)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question