Communicating between processes is essential part of every big systems which we generally call as Inter Process Communication. If its large distributed systems the processes will be in different computers which we classify as servers and clients. Often we use http / http(s) based or other socket based communication mechanisms to do the job. In .Net world most of the people use WCF.
But in some cases there will be big systems running on same computer as separate processes and would like to communicate each other. We can use WCF here also with net.pipe binding. Since its same machine there are some more available options. Lets see what are those options. This post is about such an alternative which uses SendMessage API provided by Windows OS.
Clipboard
One of the dangerous forms of communication. Dangerous as user or any other application can copy new data to clipboard before our intended process reads data. But I have seen enterprise systems which uses this technique and working well for years.
EventLog
The sender can write to event log and the receiver subscribe to the event log entries to receive message. This will leave a trace of what has send back and forth between apps. But we should never use this for secure communication unless encrypted.
Standard In and Out of processes
One process can write to StdIn stream of another process and other can write to sender's StdId for two way communications. I have used this technique in one of my recent projects as it spawns processes frequently where publishing a unique WCF end endpoint was little difficult to achieve.
FileWatcher
Still many systems uses this technique. If there is a designated folder for messages and files are unique, it will work. in .Net works we can use FileSystemWatcher APIs to achieve the functionality.
SendMessage
Here we leverage the SendMessage windows API function to send a message to another process. That message then will be available to the message pump of receiving application. As we know the windows apps are running based on the messaging infrastructure. In simple words, the OS sends appropriate messages to apps about what is going on (eg:MouseClicked) with required contextual data and apps just process them. Its same for WPF as well though its hidden for normal apps. But there is a way to capture the message pump in WPF apps.
A sample has been uploaded to github which shows how communication can be established between WinForms app and WPF app using SendMessage. Since the code is the true documentation, there is no more explanation required in this post.
https://github.com/joymon/Win32IPCDemo
Happy coding.
No comments:
Post a Comment