Search code examples
c#wpfdllkeyboard-hookmouse-hook

How to send notification with mouse/key hook data from C# dll to wpf exe


I am creating a C# WPF exe and a C# DLL. There is a function called StartRecording in DLL. The exe calls this function. The function installs Low-level mouse and keyboard hooks on a third party application (e.g. MS Word). Inside the hook procedures, I collect details related to the mouse/keyboard message and also the details of the control (using UI Automation) with which the user interacts.

I want to send a notification from the DLL to the exe that data have been collected and in the notification itself, I want to send the data. What is the best way to do this? One option, I can think of is to send WM_COPY_DATA message from the dll to the exe. But is there any better way? Is there a way by which I can send a Dictionary from the dll to the exe?


Solution

  • Here are two of many solutions to this:

    First: You could implement an event handler and register your ViewModel at your dll-class. Check out this question - to implement your own custom event - if even needed.

    Second: Or you go with an interface if you only have one instance to listen.

    public interface IListen
    {
        void SendMessage(string msg);
    
        void SendDictonary(IDictionary<object, object> dic);
    }
    
    
    public class MyDllClass
    {
        public IListen Listener
        {
            get;
            set;
        }
    
        public void SomeMouseInputAppears()
        {
            if(Listener != null) Listener.SendMessage("sample");
        }
    
        public void SomeKeyboardInputAppears()
        {
            if (Listener != null) Listener.SendDictonary(new Dictionary<object,object>());
        }
    }
    

    Of course the interface is in your dll and the implementation of it has to be in the view library / application.