/// <summary>
    /// Request to receive a notification when a device is attached or removed.
    /// </summary>
    /// <param name="devicePathName">a handle to a device.</param>
    /// <param name="formHandle">a handle to the window that will receive device events.</param>
    /// <param name="classGuid">an interface class GUID.</param>
    /// <param name="deviceNotificationHandle"></param>
    /// <returns>True on success, False on failure.</returns>
    internal static bool RegisterForDeviceNotifications(string devicePathName, IntPtr formHandle, Guid classGuid,
                                                        ref IntPtr deviceNotificationHandle)
    {
      // Returns    : 
      // A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
      DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface =
        new DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE();

      try
      {
        // Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.

        // Set the size.
        int size = Marshal.SizeOf(DevBroadcastDeviceInterface);
        DevBroadcastDeviceInterface.dbcc_size = size;

        // Request to receive notifications about a class of devices.
        DevBroadcastDeviceInterface.dbcc_devicetype = DeviceManagementApiDeclarations.DBT_DEVTYP_DEVICEINTERFACE;

        DevBroadcastDeviceInterface.dbcc_reserved = 0;

        // Specify the interface class to receive notifications about.
        DevBroadcastDeviceInterface.dbcc_classguid = classGuid;

        // Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.
        IntPtr DevBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);

        // Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
        // Set fDeleteOld True to prevent memory leaks.
        Marshal.StructureToPtr(DevBroadcastDeviceInterface, DevBroadcastDeviceInterfaceBuffer, true);

        // ***
        // API function:
        // RegisterDeviceNotification
        // Purpose:
        // Request to receive notification messages when a device in an interface class
        // is attached or removed.
        // Accepts:
        // Aa handle to the window that will receive device events
        // A pointer to a DEV_BROADCAST_DEVICEINTERFACE to specify the type of
        // device to send notifications for,
        // DEVICE_NOTIFY_WINDOW_HANDLE to indicate that Handle is a window handle.
        // Returns:
        // A device notification handle or NULL on failure.
        // ***

        deviceNotificationHandle =
          DeviceManagementApiDeclarations.RegisterDeviceNotification(formHandle, DevBroadcastDeviceInterfaceBuffer,
                                                                     DeviceManagementApiDeclarations.
                                                                       DEVICE_NOTIFY_WINDOW_HANDLE);

        // Marshal data from the unmanaged block DevBroadcastDeviceInterfaceBuffer to
        // the managed object DevBroadcastDeviceInterface
        Marshal.PtrToStructure(DevBroadcastDeviceInterfaceBuffer, DevBroadcastDeviceInterface);

        // Free the memory allocated previously by AllocHGlobal.
        Marshal.FreeHGlobal(DevBroadcastDeviceInterfaceBuffer);

        // Find out if RegisterDeviceNotification was successful.
        if (deviceNotificationHandle.ToInt32() == IntPtr.Zero.ToInt32())
        {
          Debug.WriteLine("RegisterDeviceNotification error");
          return false;
        }
      }
      catch (Exception ex)
      {
        HandleException(ModuleName + ":" + MethodBase.GetCurrentMethod(), ex);
      }

      return true;
    }
Exemple #2
0
        /// <summary>
        /// Request to receive a notification when a device is attached or removed.
        /// </summary>
        /// <param name="devicePathName">a handle to a device.</param>
        /// <param name="formHandle">a handle to the window that will receive device events.</param>
        /// <param name="classGuid">an interface class GUID.</param>
        /// <param name="deviceNotificationHandle"></param>
        /// <returns>True on success, False on failure.</returns>
        internal static bool RegisterForDeviceNotifications(string devicePathName, IntPtr formHandle, Guid classGuid,
                                                            ref IntPtr deviceNotificationHandle)
        {
            // Returns    :
            // A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
            DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface =
                new DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE();

            try
            {
                // Set the parameters in the DEV_BROADCAST_DEVICEINTERFACE structure.

                // Set the size.
                int size = Marshal.SizeOf(DevBroadcastDeviceInterface);
                DevBroadcastDeviceInterface.dbcc_size = size;

                // Request to receive notifications about a class of devices.
                DevBroadcastDeviceInterface.dbcc_devicetype = DeviceManagementApiDeclarations.DBT_DEVTYP_DEVICEINTERFACE;

                DevBroadcastDeviceInterface.dbcc_reserved = 0;

                // Specify the interface class to receive notifications about.
                DevBroadcastDeviceInterface.dbcc_classguid = classGuid;

                // Allocate memory for the buffer that holds the DEV_BROADCAST_DEVICEINTERFACE structure.
                IntPtr DevBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);

                // Copy the DEV_BROADCAST_DEVICEINTERFACE structure to the buffer.
                // Set fDeleteOld True to prevent memory leaks.
                Marshal.StructureToPtr(DevBroadcastDeviceInterface, DevBroadcastDeviceInterfaceBuffer, true);

                // ***
                // API function:
                // RegisterDeviceNotification
                // Purpose:
                // Request to receive notification messages when a device in an interface class
                // is attached or removed.
                // Accepts:
                // Aa handle to the window that will receive device events
                // A pointer to a DEV_BROADCAST_DEVICEINTERFACE to specify the type of
                // device to send notifications for,
                // DEVICE_NOTIFY_WINDOW_HANDLE to indicate that Handle is a window handle.
                // Returns:
                // A device notification handle or NULL on failure.
                // ***

                deviceNotificationHandle =
                    DeviceManagementApiDeclarations.RegisterDeviceNotification(formHandle, DevBroadcastDeviceInterfaceBuffer,
                                                                               DeviceManagementApiDeclarations.
                                                                               DEVICE_NOTIFY_WINDOW_HANDLE);

                // Marshal data from the unmanaged block DevBroadcastDeviceInterfaceBuffer to
                // the managed object DevBroadcastDeviceInterface
                Marshal.PtrToStructure(DevBroadcastDeviceInterfaceBuffer, DevBroadcastDeviceInterface);

                // Free the memory allocated previously by AllocHGlobal.
                Marshal.FreeHGlobal(DevBroadcastDeviceInterfaceBuffer);

                // Find out if RegisterDeviceNotification was successful.
                if (deviceNotificationHandle.ToInt32() == IntPtr.Zero.ToInt32())
                {
                    Debug.WriteLine("RegisterDeviceNotification error");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                HandleException(ModuleName + ":" + MethodBase.GetCurrentMethod(), ex);
            }

            return(true);
        }