1

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?

1
  • @GX: in the first link in my answer, there is an updated version of the code that caters for windows services and consoles... Commented Mar 4, 2010 at 1:32

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I have hear about Named Pipes, never actually tried to implement them. Thank you for the nice resource links
2

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

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
I didn't have enough rep at the time of posting to add a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.