/// <summary> /// The register for device notifications. /// </summary> /// <param name="windowHandleToReceiveNotifications"> /// The window handle to receive notifications. /// </param> /// <returns> /// The <see cref="ProcessHandleResult"/>. /// </returns> public IProcessHandleResult Register(IntPtr windowHandleToReceiveNotifications) { var devBroadcastDeviceInterface = _broadcastDeviceInterfaceFactory.CreateBroadcastDeviceInterface(); var devBroadcastDeviceInterfaceBuffer = IntPtr.Zero; try { devBroadcastDeviceInterfaceBuffer = _marshallWrapper.AllocHGlobal(devBroadcastDeviceInterface.Size); _marshallWrapper.StructureToPointer(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, false); var deviceNotificationHandle = _unsafeNativeMethodsWrapper.RegisterDeviceNotification( windowHandleToReceiveNotifications, devBroadcastDeviceInterfaceBuffer, DeviceNotifyWindowHandle); return(_processHandleResultFactory.Create(deviceNotificationHandle)); } finally { if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero) { _marshallWrapper.FreeHGlobal(devBroadcastDeviceInterfaceBuffer); } } }