Solved

Call Delphi Program from another Delphi program

Posted on 2004-04-03
7
304 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
Independent Software Vendors: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

631 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