Link to home
Start Free TrialLog in
Avatar of zefram
zefram

asked on

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.
Avatar of StTwister
StTwister

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...
Avatar of Russell Libby

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
Avatar of zefram

ASKER

Could you give an example please?
I was thinking of global memory objects (accessed via GlobalAlloc, GlobalLock and GLobalUnlock APIs)
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..
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zefram

ASKER

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.
Avatar of zefram

ASKER

*Very slow connection, didn't see the last posting containing code. Will check it out now.
Avatar of zefram

ASKER

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?

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;
Avatar of zefram

ASKER

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?
Avatar of zefram

ASKER

o, wait. I think I understand:

Is the lpTitle the title of the application of the handle?
Avatar of zefram

ASKER

It works!

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