SendMessage函数.NET控制台应用程序 [英] SendMessage to .NET console application
问题描述
我一直在尝试从两个.NET的Windows发送消息窗体应用程序 WM_COPYDATA
,它的伟大工程。
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 accomplished with console applications.
在所有的 SendMessage函数
函数接受一个窗口句柄,我怎样才能得到一个控制台应用程序的窗口句柄?
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?
推荐答案
最常见的 IPC 一>方法(除了 WM_COPYDATA
)的内存映射文件和命名管道。我建议你检查了后者。
The most common IPC methods (aside from WM_COPYDATA
) are memory-mapped files and named pipes. I suggest you check out the latter.
MSDN有命名管道通信的例子。具体来说,你需要使用类 NamedPipeServerStream
和 NamedPipeClientStream
,它的行为在很大程度上就像普通的网络流一旦他们创建
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.
设置在控制台应用程序的实际的Windows消息循环是复杂的,至少可以这样说。如果你真的需要做到这一点,这里有关于这个问题相当冗长文章。我强烈建议您使用命名管道,而不是如果你想要做的是数据传输;如果你实际上并不需要处理标准的Windows消息,则这不会是值得的。
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.
这篇关于SendMessage函数.NET控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!