Solved

Call Delphi Program from another Delphi program

Posted on 2004-04-03
7
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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