接收WM_COPYDATA结构在WPF或控制台C#应用程序 [英] Receive WM_COPYDATA struct in WPF or Console C# app

查看:815
本文介绍了接收WM_COPYDATA结构在WPF或控制台C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,需要写在本地C.其他应用程序到目前为止,我已经找到了如何从我的C#应用​​程序发送消息与User32.dll中SendMessage函数的C程序进行通信的C#应用​​程序。但我无法弄清楚如何让C#应用程序来接收C应用程序的消息。

I am writing a C# application that needs to communicate with another application written in native C. So far I have figured out how to send messages from my C# app to the C app with the User32.dll SendMessage. However I am unable to figure out how to get the C# app to RECEIVE messages from the C app.

我见过重写WndProc方法的WinForms的例子,但是有没有WndProc方法在一个WPF或控制台应用程序重写。当然,这是可能的,至少在一个控制台应用程序做的。对吧?

I have seen WinForms examples of overriding the WndProc method, but there is no WndProc method to override in a WPF or Console application. Surely it's possible to do in a Console application at least. Right?

推荐答案

您可以使用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应用程序中使用。

Unfortunately, there is no real equivelent for a Console Application. Windows messages, by definition, are sent and received by a window handle (HWND), so they really are meant to be used with GUI applications.

有许多其他然而少奇,意味着在Windows 做进程间的通信, 。我个人比较喜欢使用管道 - 设置命名管道的作品相当不错,在本地和托管代码,并且是两个程序之间的通信效率非常高。

There are many other, less odd, means of doing inter-process communication on Windows, however. I personally like using pipes - setting up named pipes works very well in both native and managed code, and is very efficient for communicating between the two programs.

这篇关于接收WM_COPYDATA结构在WPF或控制台C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆