0

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

user1400716
  • 1,126
  • 5
  • 13
  • 32
  • I think you'd find using WCF Named Pipes to be much more flexible, secure, and reliable... Check out http://stackoverflow.com/questions/7353670/wcf-named-pipe-minimal-example – Dan Field Aug 25 '15 at 16:38
  • Thanks. WCF seems to be aimed at loosely coupled applications such as web services. Basically my COM application is instantiating and running my WPF application so I was hoping there would be a simple way to receive messages from the WPF application by referencing windows handlers. – user1400716 Aug 25 '15 at 16:50
  • WCF NamedPipes is specifically for interprocess communication on the same machine. Using WinHooks is opening a challenging can of worms once you want to do anything but send standard WM_ messages to a window that you know the name of (and good luck finding all those calls once you decide to rename a window...) – Dan Field Aug 25 '15 at 17:40
  • Very unclear why you are doing this, you are not bridging an api, language or process boundary here. Simply use normal C# patterns, methods, properties, events. – Hans Passant Aug 25 '15 at 18:19
  • The goal is to call my WPF application from a Python script. – user1400716 Aug 25 '15 at 19:13
  • That's fine, just use that [ComVisible] MyComClass in a class library as you planned. But there is no need at all to use COM or the winapi to get your class library to talk to your WPF code. It is all C# code. – Hans Passant Aug 26 '15 at 09:10
  • I use COM so that the python script can run my C# application. The interface is defined in the COM class and is lanquage independant. The COM class instantiates and runs the WPF application. – user1400716 Aug 26 '15 at 14:30

0 Answers0