SendMessage 到 .NET 控制台应用程序

发布于 2024-08-24 12:41:04 字数 369 浏览 6 评论 0原文

我一直在尝试使用 WM_COPYDATA 从两个 .NET Windows 窗体应用程序发送消息,效果非常好。

我想知道这是否可以通过控制台应用程序来完成。

SendMessage 函数接收窗口句柄之后,如何获取控制台应用程序的窗口句柄?

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

另外,我是否需要在控制台应用程序中设置某种消息循环才能接收消息?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-08-31 12:41:04

最常见的 IPC 方法(除了 WM_COPYDATA)是内存映射文件和命名管道。我建议你看看后者。

MSDN 有一个命名管道通信示例。具体来说,您需要使用的类是 NamedPipeServerStreamNamedPipeClientStream,它们在创建后的行为很大程度上类似于普通的网络流。

好处是它也可以通过网络运行,尽管您显然也可以在单台计算机上使用它。

至少可以说,在控制台应用程序中设置实际的 Windows 消息循环很复杂。如果您确实需要这样做,这里有一篇关于该主题的相当冗长的文章。如果您只想传输数据,我强烈建议您使用命名管道;如果您实际上不需要处理标准 Windows 消息,那么这就不值得付出努力。

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.

不如归去 2024-08-31 12:41:04

@tommieb75:XDMessaging 实际上是我的库:) WM_COPYDATA 不适用于控制台应用程序,因为它们没有消息泵。不过,该库确实包含一个基于 IOStream 的 IPC 实现,适用于控制台应用程序和服务。 http://thecodeking.github.com/XDMessaging.Net/

@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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文