Пример #1
0
        //[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
        //private void findDevice(uint pid, uint vid)
        //{
        //    Hid.HidD_GetHidGuid(out _hidGuid);
        //    hHidDeviceInfo = Setup.SetupDiGetClassDevs(ref _hidGuid, IntPtr.Zero, IntPtr.Zero, Setup.DIGCF_PRESENT | Setup.DIGCF_DEVICEINTERFACE);
        //    if (hHidDeviceInfo.ToInt32() > -1)
        //    {
        //        uint i = 0;
        //        while (!isValid && i < MAX_USB_DEVICES)
        //        {
        //            var hidDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
        //            hidDeviceInterfaceData.cbSize = Marshal.SizeOf(hidDeviceInterfaceData);
        //            if (Setup.SetupDiEnumDeviceInterfaces(hHidDeviceInfo, IntPtr.Zero, ref _hidGuid, (int)i, ref hidDeviceInterfaceData))
        //            {
        //                bool detailResult;
        //                int length=0, required=0;
        //                Setup.SetupDiGetDeviceInterfaceDetail(hHidDeviceInfo, ref hidDeviceInterfaceData, IntPtr.Zero, 0, ref length, IntPtr.Zero);
        //                var hidDeviceInterfaceDetailData = new PSP_DEVICE_INTERFACE_DETAIL_DATA();
        //                hidDeviceInterfaceDetailData.cbSize = 5; //DWORD cbSize (size 4) + Char[0] (size 1) for 32bit only!
        //                var hDeviceInterfaceDetailData = Marshal.AllocHGlobal(Marshal.SizeOf(hidDeviceInterfaceDetailData));
        //                Marshal.StructureToPtr(hidDeviceInterfaceDetailData, hDeviceInterfaceDetailData, true);
        //                detailResult = Setup.SetupDiGetDeviceInterfaceDetail(hHidDeviceInfo, ref hidDeviceInterfaceData, hDeviceInterfaceDetailData, (int)length, ref required, IntPtr.Zero);
        //                Marshal.PtrToStructure(hDeviceInterfaceDetailData, hidDeviceInterfaceDetailData);
        //                if (detailResult)
        //                {
        //                    SafeFileHandle aHandle = Kernel32.CreateFile(hidDeviceInterfaceDetailData.DevicePath,
        //                       Kernel32.GENERIC_READ |
        //                       Kernel32.GENERIC_WRITE,
        //                       Kernel32.FILE_SHARE_READ |
        //                       Kernel32.FILE_SHARE_WRITE,
        //                       IntPtr.Zero,
        //                       Kernel32.OPEN_EXISTING,
        //                       Kernel32.FILE_FLAG_OVERLAPPED,
        //                       0);
        //                    base.handle = aHandle.DangerousGetHandle();

        //                    if (base.handle.ToInt32() > -1)
        //                    {
        //                        HIDD_ATTRIBUTES hidDeviceAttributes = new HIDD_ATTRIBUTES();
        //                        if (Hid.HidD_GetAttributes(this, ref hidDeviceAttributes))
        //                        {
        //                            if ((hidDeviceAttributes.VendorID == vid) && (hidDeviceAttributes.ProductID == pid))
        //                            {
        //                                isValid = true;
        //                                ProductID = pid;
        //                                VendorID = vid;
        //                                VersionNumber = (uint)hidDeviceAttributes.VersionNumber;
        //                                IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string;
        //                                if (HidD_GetProductString(this.handle, buffer, 126)) Name = Marshal.PtrToStringAuto(buffer);
        //                                if (HidD_GetSerialNumberString(this.handle, buffer, 126)) SerialNumber = Marshal.PtrToStringAuto(buffer);
        //                                Marshal.FreeHGlobal(buffer);
        //                                var capabilities = new HIDP_CAPS();
        //                                IntPtr hPreparsedData = IntPtr.Zero;
        //                                if (Hid.HidD_GetPreparsedData(this, out hPreparsedData))
        //                                {
        //                                    if (Hid.HidP_GetCaps(hPreparsedData, out capabilities)>0) 
        //                                        FeatureReportLength = (ushort)capabilities.FeatureReportByteLength;
        //                                    Hid.HidD_FreePreparsedData(hPreparsedData);
        //                                }
        //                                break;
        //                            }
        //                        }
        //                        else
        //                        {
        //                            Kernel32.CloseHandle(base.handle);
        //                        }
        //                    }
        //                }
        //                Marshal.FreeHGlobal(hDeviceInterfaceDetailData);
        //            }
        //            i++;

        //        }
        //    }
        //}


        public static IntPtr RegisterForUsbEvents(IntPtr hWnd, Guid gClass)
        {
            DEV_BROADCAST_DEVICEINTERFACE oInterfaceIn = new DEV_BROADCAST_DEVICEINTERFACE();
            oInterfaceIn.dbcc_classguid = gClass;
            oInterfaceIn.dbcc_devicetype = TOAPI.Setup.Setup.DBT_DEVTYP_DEVICEINTERFACE;
            oInterfaceIn.dbcc_reserved = 0;
            
            return User32.RegisterDeviceNotification(hWnd, oInterfaceIn, Setup.DEVICE_NOTIFY_WINDOW_HANDLE);
        }