OLE Automation Server and Inter-Application Communication

I need to create an OLE Automation Server which can look for another application(probably through the windows handle), check that the application is up and running,  and then communicate commands to it (and be able to receive messages when necessary).  Currently the application communicates with the control program (both are Delphi 4 programs) through posting and checking for text files.  This, however, does not allow for the level of communciation necessary.  If anyone has an example program that can do the following:
  1.  Locate an application and insure that it is running.
  2.  Send messages between the two programs.
dianaJoyAsked:
Who is Participating?
 
RBertoraConnect With a Mentor Commented:
Ok to create a ole server :

new app:
new activeX automation object: I called mine "One"
click ok.

it takes you to the type libary editor :
click on interface icon in tree view (red icon)
now click on new method in tool bar at top (green icon)
in tree view type in your method name : "ShowString"
click on parameters tab
click add
change type of param from int to widesting drop down
click on refresh implementation (white icon)
press F12


click on unit2
fill in the showstring procedure:

procedure Tone.ShowString(const Param1: WideString);
begin
  showmessage('Hello');
end;



ok so now you have the following files in the project

unit1.pas
unit2.pas
project1_tlb

(you should rename these to meanigfullnames later)
keep that all in one directory.

this is what they look like:

unit Unit1; //blank form this is your main form of program

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

end.


unit Unit2; // this handles your oleserver bit

interface

uses
  ComObj, ActiveX, Project1_TLB,Dialogs;

type
  Tone = class(TAutoObject, Ione)
  protected
    procedure ShowString(const Param1: WideString); safecall;
    { Protected declarations }
  end;

implementation

uses ComServ;

procedure Tone.ShowString(const Param1: WideString);
begin
  showmessage('Hello');
end;

initialization
  TAutoObjectFactory.Create(ComServer, Tone, Class_one,
    ciMultiInstance, tmApartment);
end.



unit Project1_TLB; // this file you need to include in your client apps

// ************************************************************************ //
// WARNING                                                                  //
// -------                                                                  //
// The types declared in this file were generated from data read from a     //
// Type Library. If this type library is explicitly or indirectly (via      //
// another type library referring to this type library) re-imported, or the //
// 'Refresh' command of the Type Library Editor activated while editing the //
// Type Library, the contents of this file will be regenerated and all      //
// manual modifications will be lost.                                       //
// ************************************************************************ //

// PASTLWTR : $Revision:   1.11.1.63  $
// File generated on 23/11/99 15:58:58 from Type Library described below.

// ************************************************************************ //
// Type Lib: C:\Program Files\Borland\Delphi4\Bin\Project1.tlb
// IID\LCID: {A2D98B62-A1B1-11D3-8A5B-00104B464D81}\0
// Helpfile:
// HelpString: Project1 Library
// Version:    1.0
// ************************************************************************ //

interface

uses Windows, ActiveX, Classes, Graphics, OleCtrls, StdVCL;

// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:      //
//   Type Libraries     : LIBID_xxxx                                    //
//   CoClasses          : CLASS_xxxx                                    //
//   DISPInterfaces     : DIID_xxxx                                     //
//   Non-DISP interfaces: IID_xxxx                                      //
// *********************************************************************//
const
  LIBID_Project1: TGUID = '{A2D98B62-A1B1-11D3-8A5B-00104B464D81}';
  IID_Ione: TGUID = '{A2D98B63-A1B1-11D3-8A5B-00104B464D81}';
  CLASS_one: TGUID = '{A2D98B65-A1B1-11D3-8A5B-00104B464D81}';
type

// *********************************************************************//
// Forward declaration of interfaces defined in Type Library            //
// *********************************************************************//
  Ione = interface;
  IoneDisp = dispinterface;

// *********************************************************************//
// Declaration of CoClasses defined in Type Library                     //
// (NOTE: Here we map each CoClass to its Default Interface)            //
// *********************************************************************//
  one = Ione;


// *********************************************************************//
// Interface: Ione
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {A2D98B63-A1B1-11D3-8A5B-00104B464D81}
// *********************************************************************//
  Ione = interface(IDispatch)
    ['{A2D98B63-A1B1-11D3-8A5B-00104B464D81}']
    procedure ShowString(const Param1: WideString); safecall;
  end;

// *********************************************************************//
// DispIntf:  IoneDisp
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {A2D98B63-A1B1-11D3-8A5B-00104B464D81}
// *********************************************************************//
  IoneDisp = dispinterface
    ['{A2D98B63-A1B1-11D3-8A5B-00104B464D81}']
    procedure ShowString(const Param1: WideString); dispid 1;
  end;

  Coone = class
    class function Create: Ione;
    class function CreateRemote(const MachineName: string): Ione;
  end;

implementation

uses ComObj;

class function Coone.Create: Ione;
begin
  Result := CreateComObject(CLASS_one) as Ione;
end;

class function Coone.CreateRemote(const MachineName: string): Ione;
begin
  Result := CreateRemoteComObject(MachineName, CLASS_one) as Ione;
end;

end.

-----------------------------------------

Right now time to do the client app:

New application,
add Project1_TLB.pas to the project
then in your form 1 add 2 buttons.
this is what your unit1.pas looks like:


unit Unit1;

interface

// note the uses clause we add the TLB.pas file:
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Project1_TLB,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    MyServerApp : IOne;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

// attach to the server (run it if it is not running)
procedure TForm1.Button1Click(Sender: TObject);
begin
  MyServerApp := CoOne.Create();
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  MyServerApp.ShowString('Server :Hello world!');
end;

end.

run this app , clicking button one will launch the server app
if it isn't running and clicking button 2 will send it the message

cool thats interprocess communication.. if you want to
communicate both ways then make sure your client is also a server like
the first one

you don't need to check that it is up and running
this is automatic and you can't disable that feature.


Also you don't need to free anything delphi does all that for you.


Rob ;-)

*whew a lot of typing for 100pts question*
0
 
TuvokCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was 'stuck' against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select 'View Question History' (or click the member link below) to navigate through any open or locked questions you may have to update and finalize them.

This is the Community Support link, if help is needed, along with the link to 'All Topics' since many new ones were recently added.

Memberlink:
http://www.experts-exchange.com/jsp/memberProfile.jsp?mbr=dianaJoy&showQHistory=true

Community Support:
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

All Topics:
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thanks,
Tuvok
Moderator @ Experts Exchange
0
All Courses

From novice to tech pro — start learning today.