开发者

SendMessage to .NET console application

I have been experimenting with sending messages from two .NET Windows Forms applications using WM_COPYDATA, and it works great.

I would like to know if that can be acco开发者_运维百科mplished with console applications.

After all the SendMessage function takes in a window handle, 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 set up some kind of message loop in the console application to be able to receive messages?


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 application 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.


@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://thecodeking.github.com/XDMessaging.Net/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜