Communication Between Programs

Started by
2 comments, last by Peon 21 years, 3 months ago
One thing that I've been wondering about is how programs can either communicate or intercept data from another program. A good example would be a popup killer. Someone writes a program that can detect if a popup is going to open, and close it before it does. But how did the program know a message was sent to open the window? That's what I'm curious about. On a related note, how could I communicate with another program? I can't think of a good example off the top of my head, but what if I wanted to write a program where you typed in a URL, pressed go, and then it opened internet explorer and went to that address? (ie: how do I let IE know what URL I want to go to, from my program?) There are a lot of ways I'd like to apply this. I like writing games and stuff, but I think it would also be fun to write an actually USEFUL program sometime. Thanks :D Peon [edited by - Peon on January 18, 2003 4:49:04 PM]
Peon
Advertisement
An easy way is just to use the FindWindow() Win32 call. You can use Spy++ to find out the class name of the window and get a handle to it. Now you can use the handle to send it messages. There is a harder way using Hooks but it requires writing a DLL and some other nastiness.
There are tons of ways to talk between processes, and depending on what you need to do, you can choose the best way. Search for IPC on MSDN.

If your app wants to launch a web page, you probably want to use ShellExecute(handle, "open", "http://www.gamedev.net", NULL, NULL, SW_SHOWNORMAL);. It''s not much of communicating between process, it''s a windows mechanism to start the pre-defined program.

- Windows messages is another way as AP mentioned. Typically old programs use this. It''s quite simple to use if you have two GUI applications. The sender does FindWindow followed by SendMessage/PostMessage. The receiver simply picks upp messages in it''s message loop. There are drawbacks with messages, for instance it''s harder to tighten security.

Sockets is one way, even if the two programs are on the same machine.

Pipes is another way, which probably performs best if you need to pass a lot of data.

Shared memory between two (or more) applications.

MSMQ can be used between processes, even on different machines. It''s good for reliability if you need to queue up messages and make sure they don''t get lost. It''s however way overkill if you need some simple IPC between two simple programs.

I would assume a popup killer to benefit from Internet Explorer plugins (ie. not be a standalone program). Not sure, but you could probably handle the DWebBrowserEvents2::OnBeforeNavigate event to prevent a popup window from getting created if certain conditions are fulfilled.

If you''re not passing data, but simply signalling between two applications, there are OS objects such as Mutexes.

There are even more ways to do IPC. Read more here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipc/base/interprocess_communications.asp
Thanks for the info! I''ll probably end up using the message method, as it does seem the easiest (security isn''t really an issue; I just wanted to write some really simple programs for home use, to figure out how to communicate between programs) I''m definetly going to keep IPC in mind; I checked the link to the MSDN but it just seems way over my head right now, hehe.
Peon

This topic is closed to new replies.

Advertisement