Get WM_COPYDATA structure in WPF or Console C # application

I am writing a C # application that should communicate with another application written in my native C. So far I have figured out how to send messages from my C # application to C application using User32.dll SendMessage. However, I cannot figure out how to get a C # application to receive messages from application C.

I have seen WinForms examples for overriding the WndProc method, but there is no WndProc method for overriding in a WPF application or console. Of course, this can be done in a console application. Correctly?

+6
c # interop console wpf interprocess
source share
1 answer

You can do this in WPF using HwndSource.AddHook :

private HwndSource hwndSource; void MyWindowClass_Loaded(object sender, RoutedEventArgs e) { hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); hwndSource.AddHook(new HwndSourceHook(WndProc)); } private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // Process your windows proc message here } 

Unfortunately, there is no real equivalence for the console application. Windows messages are, by definition, transmitted and received by a window descriptor (HWND), so they are really intended for use with graphics applications.

There are many other, less bizarre ways to do interprocess communication in Windows . I personally like to use pipes - setting up named pipes works very well in both native and managed code, and is very effective for exchanging data between two programs.

+8
source share

All Articles