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?
C# Windows Messages in Console Application?
1.1k views Asked by piort At
2
There are 2 answers
0
On
The SendMessage
and PostThreadMessage
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.
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: