2009-10-27 158 views
6

我写需要写在本地C.到目前为止,我已经找到了如何从我的C#应用​​程序将消息发送到与还原C应用其他应用程序进行通信的C#应用​​程序User32.dll SendMessage。但我无法弄清楚如何让C#应用从C应用程序接收消息。接收WM_COPYDATA结构在WPF或控制台C#应用程序

我看到重写WndProc方法的的WinForms的例子,但没有WndProc方法在WPF或控制台应用程序重写。至少可以在控制台应用程序中执行。对?

回答

8

您可以使用WPF做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   
} 

不幸的是,对于一个控制台应用程序没有真正equivelent。 Windows消息,根据定义,发送,并通过窗口句柄(HWND)接受,所以他们真的是为了与GUI应用程序中使用。

还有很多其他的,少奇,意味着做inter-process communication on Windows,但是。我个人比较喜欢使用管道 - 建立命名管道的作品相当不错,在本地和托管代码,并且是两个程序之间的通信效率非常高。