예제 #1
0
        private bool IsDesiredDevice(IntPtr lParam)
        {
            var hdr = (Native.DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(Native.DEV_BROADCAST_HDR));

            if (hdr.dbcc_devicetype == (uint)_deviceType)
            {
                if (_deviceType == Native.DeviceType.DBT_DEVTYP_DEVICEINTERFACE)
                {
                    Native.DEV_BROADCAST_DEVICEINTERFACE deviceInterface =
                        (Native.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(lParam, typeof(Native.DEV_BROADCAST_DEVICEINTERFACE));

                    var str  = Encoding.Default.GetString(deviceInterface.dbcc_classguid);
                    var guid = new Guid(deviceInterface.dbcc_classguid);
                    if (guid.ToString() == DEVICE_INTERFACE_GUID)
                    {
                        return(true);
                    }
                }
                else
                {
                    //TODO: other device type
                }
            }
            return(false);
        }
예제 #2
0
        public void RegisterNotificationFor(IntPtr recipientHandle, Native.DeviceType deviceType)
        {
            _deviceType = deviceType;

            IntPtr notificationFilter = IntPtr.Zero;

            switch (_deviceType)
            {
            case Native.DeviceType.DBT_DEVTYP_DEVICEINTERFACE:
            {
                Native.DEV_BROADCAST_DEVICEINTERFACE di =
                    new Native.DEV_BROADCAST_DEVICEINTERFACE();

                var size = Marshal.SizeOf(di);

                di.dbcc_size       = (uint)size;
                di.dbcc_devicetype = (int)Native.DeviceType.DBT_DEVTYP_DEVICEINTERFACE;
                di.dbcc_reserved   = 0;
                di.dbcc_classguid  = new Guid(DEVICE_INTERFACE_GUID).ToByteArray();

                notificationFilter = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(di, notificationFilter, true);
                break;
            }

            case Native.DeviceType.DBT_DEVTYP_HANDLE:
            {
                Native.DEV_BROADCAST_HANDLE di =
                    new Native.DEV_BROADCAST_HANDLE();

                var size = Marshal.SizeOf(di);

                di.dbch_size       = (uint)size;
                di.dbch_devicetype = (int)Native.DeviceType.DBT_DEVTYP_HANDLE;
                di.dbch_reserved   = 0;

                //https://msdn.microsoft.com/en-us/library/windows/desktop/aa363217(v=vs.85).aspx
                di.dbch_handle     = IntPtr.Zero;      //Be sure to set the dbch_handle member to the device handle obtained from the CreateFile function.
                di.dbch_hdevnotify = IntPtr.Zero;

                //TODO
                //di.dbch_eventguid =
                //di.dbch_nameoffset =
                //di.dbch_data =

                break;
            }

            case Native.DeviceType.DBT_DEVTYP_OEM:
            {
                //TODO

                break;
            }

            case Native.DeviceType.DBT_DEVTYP_PORT:
            {
                //TODO
                break;
            }

            case Native.DeviceType.DBT_DEVTYP_VOLUME:
            {
                //TODO
                break;
            }

            default:
                break;
            }

            if (notificationFilter != IntPtr.Zero)
            {
                _hdNotify = Native.RegisterDeviceNotification(recipientHandle, notificationFilter, (uint)Native.DEVICE_NOTIFY.DEVICE_NOTIFY_WINDOW_HANDLE);
            }
        }