Solved

Call Delphi Program from another Delphi program

Posted on 2004-04-03
7
301 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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