Solved

Call Delphi Program from another Delphi program

Posted on 2004-04-03
7
297 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now