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.
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.
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...
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
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..
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
*Very slow connection, didn't see the last posting containing code. Will check it out now.
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?
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(WindowHandl e: 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.Handl e) and
(CompareText(lpTitle, Application.MainForm.Capti on) = 0) and
(CompareText(lpClass, Application.MainForm.Class Name) = 0) then
begin
// Found the other handle
Integer(Pointer(lParam)^): =WindowHan dle;
// 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(@EnumWindowPro c, Integer(@result));
end;
function GetOtherHandle: HWND;
function EnumWindowProc(WindowHandl
var lpClass: Array [0..1024] of Char;
lpTitle: Array [0..1024] of Char;
begin
GetWindowText(WindowHandle
GetClassName(WindowHandle,
if (WindowHandle <> Application.MainForm.Handl
(CompareText(lpTitle, Application.MainForm.Capti
(CompareText(lpClass, Application.MainForm.Class
begin
// Found the other handle
Integer(Pointer(lParam)^):
// 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(@EnumWindowPro
end;
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?
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?
ASKER
o, wait. I think I understand:
Is the lpTitle the title of the application of the handle?
Is the lpTitle the title of the application of the handle?
ASKER
It works!
Thanx! I'll be able to continue from here :-)
Thanx! I'll be able to continue from here :-)