//internal bool RegisterForDeviceNotifications(string devicePathName, IntPtr formHandle, Guid classGuid, ref IntPtr deviceNotificationHandle)
        //{
        //    DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE broadcastDeviceinterface = new DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE();
        //    bool flag = false;
        //    try
        //    {
        //        int cb = Marshal.SizeOf((object)broadcastDeviceinterface);
        //        broadcastDeviceinterface.dbcc_size = cb;
        //        broadcastDeviceinterface.dbcc_devicetype = 5;
        //        broadcastDeviceinterface.dbcc_reserved = 0;
        //        broadcastDeviceinterface.dbcc_classguid = classGuid;
        //        IntPtr num = Marshal.AllocHGlobal(cb);
        //        Marshal.StructureToPtr((object)broadcastDeviceinterface, num, true);
        //        deviceNotificationHandle = DeviceManagementApiDeclarations.RegisterDeviceNotification(formHandle, num, 0);
        //        Marshal.PtrToStructure(num, (object)broadcastDeviceinterface);
        //        Marshal.FreeHGlobal(num);
        //        flag = deviceNotificationHandle.ToInt32() != IntPtr.Zero.ToInt32();
        //    }
        //    catch (Exception ex)
        //    {
        //        ProjectData.SetProjectError(ex);
        //        DeviceManagement.HandleException("DeviceManagement", ex);
        //        ProjectData.ClearProjectError();
        //    }
        //    return flag;
        //}

        internal bool RegisterForDeviceNotifications(string devicePathName, IntPtr formHandle, ref IntPtr deviceNotificationHandle)
        {
            DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE broadcastDeviceinterface = new DeviceManagementApiDeclarations.DEV_BROADCAST_DEVICEINTERFACE();
            bool flag = false;

            try
            {
                int cb = Marshal.SizeOf((object)broadcastDeviceinterface);
                broadcastDeviceinterface.dbcc_size       = cb;
                broadcastDeviceinterface.dbcc_devicetype = 5;
                broadcastDeviceinterface.dbcc_reserved   = 0;
                //broadcastDeviceinterface.dbcc_classguid = classGuid;
                IntPtr num = Marshal.AllocHGlobal(cb);
                Marshal.StructureToPtr((object)broadcastDeviceinterface, num, true);
                deviceNotificationHandle = DeviceManagementApiDeclarations.RegisterDeviceNotification(formHandle, num, 0);
                Marshal.PtrToStructure(num, (object)broadcastDeviceinterface);
                Marshal.FreeHGlobal(num);
                flag = deviceNotificationHandle.ToInt32() != IntPtr.Zero.ToInt32();
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                DeviceManagement.HandleException("DeviceManagement", ex);
                ProjectData.ClearProjectError();
            }
            return(flag);
        }
 internal void StopReceivingDeviceNotifications(IntPtr deviceNotificationHandle)
 {
     try
     {
         DeviceManagementApiDeclarations.UnregisterDeviceNotification(deviceNotificationHandle);
     }
     catch (Exception ex)
     {
         ProjectData.SetProjectError(ex);
         DeviceManagement.HandleException("DeviceManagement", ex);
         ProjectData.ClearProjectError();
     }
 }
        internal bool FindDeviceFromGuid(Guid myGuid, ref string[] devicePathName)
        {
            bool flag1 = false;

            try
            {
                IntPtr classDevs   = DeviceManagementApiDeclarations.SetupDiGetClassDevs(ref myGuid, (string)null, 0, 18);
                bool   flag2       = false;
                int    MemberIndex = 0;
                bool   flag3       = false;
                do
                {
                    DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = new DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA();
                    DeviceInterfaceData.cbSize = Marshal.SizeOf((object)DeviceInterfaceData);
                    if (!DeviceManagementApiDeclarations.SetupDiEnumDeviceInterfaces(classDevs, 0, ref myGuid, MemberIndex, ref DeviceInterfaceData))
                    {
                        flag3 = true;
                    }
                    else
                    {
                        int  RequiredSize          = 0;
                        bool deviceInterfaceDetail = DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(classDevs, ref DeviceInterfaceData, IntPtr.Zero, 0, ref RequiredSize, IntPtr.Zero);
                        DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData = new DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DETAIL_DATA();
                        interfaceDetailData.cbSize = Marshal.SizeOf((object)interfaceDetailData);
                        IntPtr num = Marshal.AllocHGlobal(RequiredSize);
                        Marshal.WriteInt32(num, checked (4 + Marshal.SystemDefaultCharSize));
                        deviceInterfaceDetail = DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(classDevs, ref DeviceInterfaceData, num, RequiredSize, ref RequiredSize, IntPtr.Zero);
                        string stringAuto = Marshal.PtrToStringAuto(new IntPtr(checked (num.ToInt32() + 4)));
                        devicePathName[MemberIndex] = stringAuto;
                        Marshal.FreeHGlobal(num);
                        flag2 = true;
                    }
                    checked { ++MemberIndex; }
                }while (!flag3);
                devicePathName = (string[])Utils.CopyArray((Array)devicePathName, (Array) new string[checked (MemberIndex - 1 + 1)]);
                DeviceManagementApiDeclarations.SetupDiDestroyDeviceInfoList(classDevs);
                flag1 = flag2;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                DeviceManagement.HandleException("DeviceManagement", ex);
                ProjectData.ClearProjectError();
            }
            return(flag1);
        }