views:

261

answers:

3

I have been experimenting with Sending Messages from 2 .NET Winform applications using WM_COPYDATA .. works great.

What I would like to know is if that can be accomplished with Console applications.

After all the SendMessage function takes in a window handle, so how can I get the window handle of a console application ?

[DllImport("User32.dll")]
public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

Also do I need to setup some kind of message Loop in the console application to be able to receive messages ?

+2  A: 

The most common IPC methods (aside from WM_COPYDATA) are memory-mapped files and named pipes. I suggest you check out the latter.

MSDN has an example of named-pipe communication. Specifically the classes you need to use are NamedPipeServerStream and NamedPipeClientStream, which behave largely like ordinary network streams once they're created.

The nice thing is that it also works over a network, although you can obviously use it on a single machine as well.

Setting up an actual Windows message loop in a console app is complicated, to say the least. If you really need to do it, here's a rather long-winded article on the subject. I'd strongly recommend using named pipes instead if all you want to do is transfer data; if you don't actually need to handle standard Windows messages then this isn't going to be worth the effort.

Aaronaught
Yes I have hear about Named Pipes, never actually tried to implement them.Thank you for the nice resource links
GX
+1  A: 

Here are the articles on CodeProject that makes use of WM_COPYDATA via a simple library, and here is another article that simply explains how it is implemented..

Hope this helps, Best regards, Tom.

tommieb75
A: 

@tommieb75: XDMessaging is actually my library :) WM_COPYDATA doesn't work for console applications as they don't have a message pump. The library does however include a IOStream based IPC implementation that works for console apps and services. http://xdmessaging.codeplex.com/

TheCodeKing