IPC the legendary way in .NET

October 16, 2006

Remoting is IPC in .NET, and its really cool. If you are a Win32 guy you’ll love this blog entry.

RegisterWindowMessage is the API which is used to register custom messages on windows platform. This is how you can add you message to the global messages of Win32 system. Make sure you have added a refrence to System.Runtime.InteropServices namespace, and heres the interop in C#:

[DllImport(“user32″, EntryPoint=”RegisterWindowMessage”)] public static extern int RegisterWindowMessageA(string lpString) ;

The server application uses this API to register the new messsage, for example WM_CUSTOM_MSG. The first call will return a integer which points to the new registered message. The server application then uses the PostMessage API to send the message to the client application. Inside the WinProc() [i.e the message handler] of the client application, we’ll check for this integer value.

Server process code:

int WM_CUSTOM_MSG=Win32Apis.RegisterWindowMessageA(“CUSTOM_MSG”);
System.Diagnostics.Process[] lProcesses=System.Diagnostics.Process.GetProcessesByName(“TEST”);
Win32Apis.PostMessageA( (int) lProcesses[0].MainWindowHandle.ToInt32(),WM_CUSTOM_MSG,0x0,0x0);

Client process code:

int WM_CUSTOM_MSG=0;
protected override void WndProc(ref Message m)
{
if (WM_CUSTOM_MSG == 0)
{
WM_CUSTOM_MSG=Win32Apis.RegisterWindowMessageA(“CUSTOM_MSG”);
}
if (m.Msg ==WM_CUSTOM_MSG)
{
System.Diagnostics.Debug.WriteLine(“Process caught a custom message:” + m.ToString());
// Do something here…
return;
}

base.WndProc (ref m);
}
I hope the code might help the Win32 lovers in some way 🙂

-Bugs!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: