?
Solved

Communication between applications

Posted on 2005-03-17
13
Medium Priority
?
190 Views
Last Modified: 2010-04-05
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
Comment
Question by:zefram
[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
  • 3
  • 2
  • +1
13 Comments
 
LVL 2

Expert Comment

by:StTwister
ID: 13567766
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13567822

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
 

Author Comment

by:zefram
ID: 13567824
Could you give an example please?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:StTwister
ID: 13567895
I was thinking of global memory objects (accessed via GlobalAlloc, GlobalLock and GLobalUnlock APIs)
0
 
LVL 1

Expert Comment

by:Dissaster
ID: 13567919
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
 
LVL 26

Accepted Solution

by:
Russell Libby earned 400 total points
ID: 13568014
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
 

Author Comment

by:zefram
ID: 13568033
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
 

Author Comment

by:zefram
ID: 13568057
*Very slow connection, didn't see the last posting containing code. Will check it out now.
0
 

Author Comment

by:zefram
ID: 13568125
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13568258
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
 

Author Comment

by:zefram
ID: 13568320
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
 

Author Comment

by:zefram
ID: 13568345
o, wait. I think I understand:

Is the lpTitle the title of the application of the handle?
0
 

Author Comment

by:zefram
ID: 13568396
It works!

Thanx! I'll be able to continue from here :-)
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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 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