C# COM类和WPF应用程序之间的双向消息
我有一个WPF应用程序,我将其构建为dll,并从用c开发的COM类MyComClass运行,如下所示C# COM类和WPF应用程序之间的双向消息,c#,wpf,com,interop,C#,Wpf,Com,Interop,我有一个WPF应用程序,我将其构建为dll,并从用c开发的COM类MyComClass运行,如下所示 private void runDlg() { m_app = new Application(); m_app.ShutdownMode = ShutdownMode.OnExplicitShutdown; m_appRunning = true; m_app.Run(); } public vo
private void runDlg()
{
m_app = new Application();
m_app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
m_appRunning = true;
m_app.Run();
}
public void Open()
{
if(m_appRunning && !m_windowOpen)
{
m_app.Dispatcher.Invoke(new Action( () => new EwokApp().Show() ) );
Thread.Sleep(cWaitPeriod1000_ms);
m_windowOpen = true;
}
}
然后,我将消息从COM类传递到WPF应用程序,如下所示
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
public void Start()
{
if(m_windowOpen)
{
int hWnd = FindWindow(null, "MY-WPF-APP");
if(hWnd != 0)
{
m_msgHelper.sendMessage(hWnd, WM_START, 0, 0);
Thread.Sleep(cWaitPeriod2000_ms);
}
}
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private static IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// address the messages you are receiving using msg, wParam, lParam
if (msg == WM_START)
{
MyApp window = (MyApp)HwndSource.FromHwnd(hWnd).RootVisual;
window.start();
}
return IntPtr.Zero;
}
在我的WPF应用程序中,我创建了一个消息处理程序,如下所示
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
public void Start()
{
if(m_windowOpen)
{
int hWnd = FindWindow(null, "MY-WPF-APP");
if(hWnd != 0)
{
m_msgHelper.sendMessage(hWnd, WM_START, 0, 0);
Thread.Sleep(cWaitPeriod2000_ms);
}
}
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private static IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// address the messages you are receiving using msg, wParam, lParam
if (msg == WM_START)
{
MyApp window = (MyApp)HwndSource.FromHwnd(hWnd).RootVisual;
window.start();
}
return IntPtr.Zero;
}
通过获取WPF窗口的处理程序并将其传递到SendMessage函数中,我可以从COM类成功地将消息发布到应用程序
我还希望WPF应用程序获得创建它的COM类的处理程序,并向它发布消息。有人能告诉我怎么做吗
谢谢我想您会发现使用WCF命名管道更加灵活、安全和可靠。。。退房,谢谢。WCF似乎针对松散耦合的应用程序,如web服务。基本上,我的COM应用程序正在实例化和运行我的WPF应用程序,因此我希望有一种简单的方法通过引用windows处理程序从WPF应用程序接收消息。WCF NamedPipes专门用于同一台机器上的进程间通信。一旦你想做任何事情,除了向一个你知道名字的窗口发送标准WM_uu消息外,使用WinHooks就是打开一个具有挑战性的蠕虫罐头。如果你决定重命名一个窗口,那么很幸运地找到所有这些调用……很不清楚你为什么要这样做,你没有在这里桥接api、语言或进程边界。只需使用普通的C模式、方法、属性和事件,目标是从Python脚本调用我的WPF应用程序。