I have a WPF application which I build as a dll and run from a COM class (MyComClass), developed in c#, as follows.
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;
}
}
I then pass messages from the COM class to the WPF application as follows
[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);
}
}
}
In my WPF application, I create a message handler as follows
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;
}
I can successfully post messages to the application from my COM class by obtaining a handler to the WPF window and passing it into the SendMessage function.
I would also like for the WPF application to obtain a handler to the COM class that created it and post messages to it. Can someone please advise how to do this.
Thanks