• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Communication between applications

Is there an easier way to share a variable between applications? How?

The best way I can think of is to make both applications continually monitor the clipboard. The apps can communicate between each other by setting the clipboard, and using some kind of protocol.
0
zefram
Asked:
zefram
  • 7
  • 3
  • 2
  • +1
1 Solution
 
StTwisterCommented:
U can use the PostMessage API and set some custom messages for your applications. U could send numeric variables as the message's parameters, or include a pointer to the data...
0
 
Russell LibbySoftware Engineer, Advisory Commented:

You could use any of the available IPC methods (dde/wm_copydata/mm files/etc), but for simple data passing, WM_COPYDATA might be the easiest for you to use.

This message must be passed using SendMessage, and not PostMessage. It should also be noted that pointer addresses are process specific, so you can't pass an address from one application to another. (numbers are fine, string data, pointer data is not).

Regards,
Russell
0
 
zeframAuthor Commented:
Could you give an example please?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
StTwisterCommented:
I was thinking of global memory objects (accessed via GlobalAlloc, GlobalLock and GLobalUnlock APIs)
0
 
DissasterCommented:
I can think of some possible answers, although I don't know exactly how to implement them (i've heard of them but not really used them) maybe someone over here can explain about them, or even tell me that i'm worng..

however, the first aproach would be using mutex, i've used them to define a variable to check if i'm running an application twice.

Other aproach would be using com. I believe this is the ideal way to stablish communication between programs, but my experience in using them is practinally null..
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Simple WM_COPYDATA example passing a string (the data can be a pointer to anything, just make sure to set the size correctly)

unit Unit1;

interface

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

type
  TForm1         = class(TForm)
     Button1:    TButton;
     procedure   Button1Click(Sender: TObject);
  private
     // Private declarations
  protected
     // Protected declarations
     procedure   RecvData(var Msg: TWMCopyData); message WM_COPYDATA;
     procedure   SendData(OtherHandle: HWND; Data: String);
  public
     // Public declarations
  end;

var
  Form1:         TForm1;

implementation
{$R *.DFM}

procedure TForm1.RecvData(var Msg: TWMCopyData);
begin

  // For simplicity sake, just display a message box with the string data
  MessageBox(0, PChar(Msg.CopyDataStruct.lpData), nil, MB_OK);

end;

procedure TForm1.SendData(OtherHandle: HWND; Data: String);
var  lpCDS:         TCopyDataStruct;
     MyString:      String;
begin

  // The dwData field can be set to anything you want, but it is a good idea to
  // use it as a data type identifier, so the other application can determine
  // WHAT the data is supposed to represent (unless you only pass one specific
  // type of data)
  lpCDS.dwData:=0;

  // Set length of string data (including the null)
  lpCDS.cbData:=Succ(Length(Data));

  // Set the actual data
  lpCDS.lpData:=PChar(Data);

  // Send data to window
  SendMessage(OtherHandle, WM_COPYDATA, Handle, Integer(@lpCDS));

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  // Send data to ourselves. What you need to do is pass the window handle for the
  // other application
  SendData(Handle, 'Hello world');

end;

end.
0
 
zeframAuthor Commented:
I only know basic delphi. I have no idea how or where to go looking for, what you call, API's.

I don't even know what it stands for, application something interface?

Could you please give some code examples? Just something basic please :-)   If possible of-course.
0
 
zeframAuthor Commented:
*Very slow connection, didn't see the last posting containing code. Will check it out now.
0
 
zeframAuthor Commented:
ok,
just check rllibby's code out. I think i undestand the code. Just one thing:

How do I get the handle of another program. Say another instance of the same program?

Is there some way to get a list of all the handles? or am i understanding it wrong?

0
 
Russell LibbySoftware Engineer, Advisory Commented:
Its really up to you to determine HOW you identify the other window / program that you are to going to communicate with. A small examle of getting the main window handle from the same application (different instance) could be done as follows:

function GetOtherHandle: HWND;

  function EnumWindowProc(WindowHandle: HWND; lParam: Integer): BOOL; stdcall;
  var   lpClass:       Array [0..1024] of Char;
        lpTitle:       Array [0..1024] of Char;
  begin
     GetWindowText(WindowHandle, @lpTitle, SizeOf(lpTitle));
     GetClassName(WindowHandle, @lpClass, SizeOf(lpClass));
     if (WindowHandle <> Application.MainForm.Handle) and
        (CompareText(lpTitle, Application.MainForm.Caption) = 0) and
        (CompareText(lpClass, Application.MainForm.ClassName) = 0) then
     begin
        // Found the other handle
        Integer(Pointer(lParam)^):=WindowHandle;
        // Stop enumerating
        result:=False;
     end
     else
        // Keep enumerating
        result:=True;
  end;

begin

  // Set default
  result:=0;

  // Attempt to get the window handle for the other instance of the same application
  EnumWindows(@EnumWindowProc, Integer(@result));

end;
0
 
zeframAuthor Commented:
ok, will try that now.

but u said "Attempt to get the window handle for the other instance of the same application"

Does that mean that one can only communcate with one other instance?

Isn't there some way to get all the handle's of all the program's running?
0
 
zeframAuthor Commented:
o, wait. I think I understand:

Is the lpTitle the title of the application of the handle?
0
 
zeframAuthor Commented:
It works!

Thanx! I'll be able to continue from here :-)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 7
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now