C#Windows消息在控制台应用程序中?
我需要使用IPC从另一个过程接收消息。目前,我正在使用WPF应用程序使用WindowsMessages
接收消息,但是我想知道该通信是否在ConsoleApp中起作用?乍一看,我注意到hwndsource
在consoleapp中找不到,所以问题是是否有一种方法是在consoleapp(首选)中接收windowsmessages
创建WPF应用程序会更容易吗?
I need to use IPC to receive messages from another process. Currently I am using WPF application to receive messages using WindowsMessages
but I am wondering if that communication would work in a ConsoleApp instead? At first glance I've noticed that HwndSource
cannot be found in ConsoleApp so the question is if there is a way to receive WindowsMessages
in a ConsoleApp (preferred way) or if it would be easier to create a WPF app instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用步行路线(同意,不是最优质,最干净的方式,但对我有用)。
将引用添加到System.Windows.Forms,并创建一个从表单中衍生的类,如下所示:
上面的代码将创建一个无形的表单窗口,您将可以通过名称找到并将窗口消息发送到。
在控制台应用程序的主要方法中,创建一个消息循环并初始化您的表单:
最后,在发件人应用程序中,按名称查找窗口,一旦找到该窗口,您将能够发送窗口消息。
声明该方法:
并将其使用如下:
You could use a walkaround (agree, not the nicest and cleanest way but worked for me).
Add a reference to System.Windows.Forms and create a class that derives from Form as follows:
Above code will create an invisible form window which you will be able to find by name and send you window messages to.
In the Main method of your console app Create a message loop and initialize your form:
Lastly, within your sender app find the window by name and once it is found you will be able to send your window messages.
Declare the method:
And use it as follows:
sendmessage
和post threadMessage
API只有在接收应用程序中运行一个活动的消息循环时才能使用。如果您想与同一台计算机上的控制台应用程序进行通信,则可以使用名为管道。在文档这应该有用。
The
SendMessage
andPostThreadMessage
APIs will only work when there is an active message loop running in the receiving application.If you want to communicate with a console app on the same machine, you could use named pipes. There is an example available in the documentation that should be helpful.