コード例 #1
0
ファイル: hid.cs プロジェクト: AMV007/Common_CSharp
 /// <summary>
 /// Registers a window to receive windows messages when a device is inserted/removed. Need to call this
 /// from a form when its handle has been created, not in the form constructor. Use form's OnHandleCreated override.
 /// </summary>
 /// <param name="hWnd">Handle to window that will receive messages</param>
 /// <param name="gClass">Class of devices to get messages for</param>
 /// <returns>A handle used when unregistering</returns>
 public static IntPtr RegisterForUsbEvents(IntPtr hWnd, Guid gClass)
 {
     user32.DeviceBroadcastInterface oInterfaceIn = new user32.DeviceBroadcastInterface();
     oInterfaceIn.Size       = Marshal.SizeOf(oInterfaceIn);
     oInterfaceIn.ClassGuid  = gClass;
     oInterfaceIn.DeviceType = kernel32.DEVTYP_DEVICEINTERFACE;
     oInterfaceIn.Reserved   = 0;
     return(user32.RegisterDeviceNotification(hWnd, oInterfaceIn, kernel32.DEVICE_NOTIFY_WINDOW_HANDLE));
 }
コード例 #2
0
        public static bool DeviceNameMatch(Message m, string mydevicePathName)
        {
            // Purpose    : Compares two device path names. Used to find out if the device name
            //            : of a recently attached or removed device matches the name of a
            //            : device the application is communicating with.

            // Accepts    : m - a WM_DEVICECHANGE message. A call to RegisterDeviceNotification
            //            : causes WM_DEVICECHANGE messages to be passed to an OnDeviceChange routine.
            //            : mydevicePathName - a device pathname returned by SetupDiGetDeviceInterfaceDetail
            //            : in an SP_DEVICE_INTERFACE_DETAIL_DATA structure.

            // Returns    : True if the names match, False if not.

            try
            {
                user32.DeviceBroadcastInterface DevBroadcastDeviceInterface = new user32.DeviceBroadcastInterface();
                setupapi.DEV_BROADCAST_HDR      DevBroadcastHeader          = new setupapi.DEV_BROADCAST_HDR();

                // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure.
                Marshal.PtrToStructure(m.LParam, DevBroadcastHeader);

                if (DevBroadcastHeader.dbch_devicetype == setupapi.DBT_DEVTYP_DEVICEINTERFACE)
                {
                    // The dbch_devicetype parameter indicates that the event applies to a device interface.
                    // So the structure in LParam is actually a DEV_BROADCAST_INTERFACE structure,
                    // which begins with a DEV_BROADCAST_HDR.

                    // Obtain the number of characters in dbch_name by subtracting the 28 bytes
                    // in the other members of the structure and dividing by 2 because there are
                    // 2 bytes per character.
                    int StringSize = System.Convert.ToInt32((DevBroadcastHeader.dbch_size - 28) / 2);

                    // The dbcc_name parameter of DevBroadcastDeviceInterface contains the device name.
                    // Trim dbcc_name to match the size of the string.
                    DevBroadcastDeviceInterface.Name = (new char[StringSize + 1]).ToString();

                    // Marshal data from the unmanaged block pointed to by m.LParam
                    // to the managed object DevBroadcastDeviceInterface.
                    Marshal.PtrToStructure(m.LParam, DevBroadcastDeviceInterface);

                    // Store the device name in a String.
                    string DeviceNameString = DevBroadcastDeviceInterface.Name.Substring(0, StringSize);

                    Debug.WriteLine("Device Name = " + DeviceNameString);
                    Debug.WriteLine("");

                    // Compare the name of the newly attached device with the name of the device
                    // the application is accessing (mydevicePathName).
                    // Set ignorecase True.
                    if (string.Compare(DeviceNameString, mydevicePathName, true) == 0)
                    {
                        // The name matches.
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }

            // It's a different device.
            return(false);
        }
コード例 #3
0
        public static bool RegisterForDeviceNotifications(string devicePathName, IntPtr formHandle, Guid classGuid, ref IntPtr deviceNotificationHandle)
        {
            // Purpose    : Request to receive a notification when a device is attached or removed.

            // Accepts    : devicePathName - a handle to a device.
            //            : formHandle - a handle to the window that will receive device events.
            //            : classGuid - an interface class GUID.
            //

            // Returns    : True on success, False on failure.

            // A DEV_BROADCAST_DEVICEINTERFACE header holds information about the request.
            user32.DeviceBroadcastInterface DevBroadcastDeviceInterface = new user32.DeviceBroadcastInterface();

            int size;

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

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

                // Request to receive notifications about a class of devices.
                DevBroadcastDeviceInterface.DeviceType = setupapi.DBT_DEVTYP_DEVICEINTERFACE;

                DevBroadcastDeviceInterface.Reserved = 0;

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


                // ***
                // 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 = user32.RegisterDeviceNotification(formHandle, DevBroadcastDeviceInterface, setupapi.DEVICE_NOTIFY_WINDOW_HANDLE);


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

            return(true);
        }