Receiving Windows messages in Unity

I need to react to the event of a USB device being unplugged and plugged in.
I found a solution for Windows Forms reacting to the WM_DEVICECHANGE message sent by the system. In plain C# it works really well, but I can’t get it to work with unity. Are there any ideas?

Following is the code I used for testing with Windows Forms:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace HardwareServiceTest
{
internal static class UsbNotification
    {
        public const int DbtDevicearrival = 0x8000; // system detected a new device        
        public const int DbtDeviceremovecomplete = 0x8004; // device is gone      
        public const int WmDevicechange = 0x0219; // device change event
        public const int DbtConfigChanged = 0x0018;
        public const int DbtDeviceSpecific = 0x8005;
        public const int DbtDevnodesChanged = 0x0007;
        private const int DbtDevtypDeviceinterface = 5;
        private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB 
        private static IntPtr notificationHandle;
        
        /// <summary>
        /// Registers a window to receive notifications when USB devices are plugged or unplugged.
        /// </summary>
        /// <param name="windowHandle">Handle to the window receiving notifications.</param>
        public static void RegisterUsbDeviceNotification(IntPtr windowHandle)
        {
            DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
            {
                DeviceType = DbtDevtypDeviceinterface,
                Reserved = 0,
                ClassGuid = GuidDevinterfaceUSBDevice,
                Name = 0
            };

            dbi.Size = Marshal.SizeOf(dbi);
            IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
            Marshal.StructureToPtr(dbi, buffer, true);

            notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
        }

        /// <summary>
        /// Unregisters the window for USB device notifications
        /// </summary>
        public static void UnregisterUsbDeviceNotification()
        {
            UnregisterDeviceNotification(notificationHandle);
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags);

        [DllImport("user32.dll")]
        private static extern bool UnregisterDeviceNotification(IntPtr handle);

        [StructLayout(LayoutKind.Sequential)]
        private struct DevBroadcastDeviceinterface
        {
            internal int Size;
            internal int DeviceType;
            internal int Reserved;
            internal Guid ClassGuid;
            internal short Name;
        }
    }
    

    public partial class Form1 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        
        public Form1()
        {
            InitializeComponent();
            UsbNotification.RegisterUsbDeviceNotification(this.Handle);

        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == UsbNotification.WmDevicechange)
            {
                switch ((int)m.WParam)
                {
                    case UsbNotification.DbtDeviceremovecomplete:
                        Usb_DeviceRemoved(); // this is where you do your magic
                        break;
                    case UsbNotification.DbtDevicearrival:
                        Usb_DeviceAdded(); // this is where you do your magic
                        break;
                    case UsbNotification.DbtConfigChanged:
                        Usb_ConfigChanged();
                        break;
                    case UsbNotification.DbtDevnodesChanged:
                        Usb_NodesChanged();
                        break;
                }
            }
        }
        public void Usb_DeviceRemoved()
        {
            label1.Text = "A USB device was removed!";
        }

        public void Usb_DeviceAdded()
        {
            label1.Text = "A USB device was added!";
        }
        public void Usb_ConfigChanged()
        {
            label1.Text = "Config changed!";
        }
        public void Usb_NodesChanged()
        {
            label1.Text = "Nodes changed!";
        }
    }
   
}

Solutions that work with Standard C# won’t work with Unity commonly, as they nearly alwys rely on the presence of Windows Forms which don’t exist in Unity or Mono in general (they are MS.NET on Windows only) or assume / require that your code has access to the event handling loop which unluckily is not the case.

To my knowledge, you can not even hook into the default event pipeline as Unity does not expose it.

What we’ve done this far in such a situation is having a 2nd process that intercepts relevant events and forwards them, through TCP in our case (you could use pipes, UDP, TCP), so we can react to the events as required.