Example #1
0
        // Token: 0x0600002B RID: 43 RVA: 0x00003FA4 File Offset: 0x000021A4
        public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
        {
            string empty   = string.Empty;
            string value   = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid);
            Guid   hidguid = Win32Usb.HIDGuid;
            IntPtr intPtr  = Win32Usb.SetupDiGetClassDevs(ref hidguid, null, IntPtr.Zero, 18u);

            try
            {
                Win32Usb.DeviceInterfaceData deviceInterfaceData = default(Win32Usb.DeviceInterfaceData);
                deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData);
                int num = 0;
                while (Win32Usb.SetupDiEnumDeviceInterfaces(intPtr, 0u, ref hidguid, (uint)num, ref deviceInterfaceData))
                {
                    string devicePath = HIDDevice.GetDevicePath(intPtr, ref deviceInterfaceData);
                    if (devicePath.IndexOf(value) >= 0)
                    {
                        HIDDevice hiddevice = (HIDDevice)Activator.CreateInstance(oType);
                        hiddevice.Initialise(devicePath);
                        return(hiddevice);
                    }
                    num++;
                }
            }
            catch (Exception ex)
            {
                throw HIDDeviceException.GenerateError(ex.ToString());
            }
            finally
            {
                Win32Usb.SetupDiDestroyDeviceInfoList(intPtr);
            }
            return(null);
        }
Example #2
0
 /// <summary>
 /// Registers this application, so it will be notified for usb events.
 /// </summary>
 /// <param name="Handle">a IntPtr, that is a handle to the application.</param>
 /// <example> This sample shows how to implement this method in your form.
 /// <code>
 ///protected override void OnHandleCreated(EventArgs e)
 ///{
 ///    base.OnHandleCreated(e);
 ///    usb.RegisterHandle(Handle);
 ///}
 ///</code>
 ///</example>
 public void RegisterHandle(IntPtr Handle)
 {
     usb_event_handle = Win32Usb.RegisterForUsbEvents(Handle, device_class);
     this.handle      = Handle;
     //Check if the device is already present.
     CheckDevicePresent();
 }
Example #3
0
        public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
        {
            string value   = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid);
            Guid   hIDGuid = Win32Usb.HIDGuid;
            IntPtr intPtr  = Win32Usb.SetupDiGetClassDevs(ref hIDGuid, null, IntPtr.Zero, 18u);

            try
            {
                DeviceInterfaceData deviceInterfaceData = default(DeviceInterfaceData);
                deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData);
                int num = 0;
                while (Win32Usb.SetupDiEnumDeviceInterfaces(intPtr, 0u, ref hIDGuid, (uint)num, ref deviceInterfaceData))
                {
                    string text = HIDDevice.smethod_0(intPtr, ref deviceInterfaceData);
                    if (text.IndexOf(value) < 0)
                    {
                        num++;
                        continue;
                    }
                    HIDDevice hIDDevice = (HIDDevice)Activator.CreateInstance(oType);
                    hIDDevice.method_0(text);
                    return(hIDDevice);
                }
            }
            catch (Exception ex)
            {
                throw GException0.GenerateError(ex.ToString());
            }
            finally
            {
                Marshal.GetLastWin32Error();
                Win32Usb.SetupDiDestroyDeviceInfoList(intPtr);
            }
            return(null);
        }
Example #4
0
 private void Initialise(string strPath)
 {
     this.m_hHandle = Win32Usb.CreateFile(strPath, 0xc0000000, 3, IntPtr.Zero, 3, 0x40000000, IntPtr.Zero);
     if (this.m_hHandle != Win32Usb.InvalidHandleValue)
     {
         IntPtr ptr;
         if (Win32Usb.HidD_GetPreparsedData(this.m_hHandle, out ptr))
         {
             try
             {
                 try
                 {
                     Win32Usb.HidCaps caps;
                     Win32Usb.HidP_GetCaps(ptr, out caps);
                     this.m_nInputReportLength  = caps.InputReportByteLength;
                     this.m_nOutputReportLength = caps.OutputReportByteLength;
                     this.m_oFile = new FileStream(new SafeFileHandle(this.m_hHandle, false), FileAccess.ReadWrite, this.m_nInputReportLength, true);
                     this.BeginAsyncRead();
                 }
                 catch (Exception)
                 {
                     throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid.");
                 }
                 return;
             }
             finally
             {
                 Win32Usb.HidD_FreePreparsedData(ref ptr);
             }
         }
         throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
     }
     this.m_hHandle = IntPtr.Zero;
     throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
 }
Example #5
0
        public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
        {
            string str             = $"vid_{nVid:x4}&pid_{nPid:x4}";
            Guid   hIDGuid         = Win32Usb.HIDGuid;
            IntPtr lpDeviceInfoSet = Win32Usb.SetupDiGetClassDevs(ref hIDGuid, null, IntPtr.Zero, 0x12);

            try
            {
                Win32Usb.DeviceInterfaceData structure = new Win32Usb.DeviceInterfaceData();
                structure.Size = Marshal.SizeOf(structure);
                for (int i = 0; Win32Usb.SetupDiEnumDeviceInterfaces(lpDeviceInfoSet, 0, ref hIDGuid, (uint)i, ref structure); i++)
                {
                    string devicePath = GetDevicePath(lpDeviceInfoSet, ref structure);
                    if (devicePath.IndexOf(str) >= 0)
                    {
                        HIDDevice device = (HIDDevice)Activator.CreateInstance(oType);
                        device.Initialise(devicePath);
                        return(device);
                    }
                }
            }
            catch (Exception exception)
            {
                throw HIDDeviceException.GenerateError(exception.ToString());
            }
            finally
            {
                Win32Usb.SetupDiDestroyDeviceInfoList(lpDeviceInfoSet);
            }
            return(null);
        }
Example #6
0
 private void method_0(string string_0)
 {
     this.m_hHandle = Win32Usb.CreateFile(string_0, 3221225472u, 3u, IntPtr.Zero, 3u, 1073741824u, IntPtr.Zero);
     if (this.m_hHandle != Win32Usb.InvalidHandleValue)
     {
         IntPtr lpData = default(IntPtr);
         if (Win32Usb.HidD_GetPreparsedData(this.m_hHandle, out lpData))
         {
             try
             {
                 HidCaps hidCaps = default(HidCaps);
                 Win32Usb.HidP_GetCaps(lpData, out hidCaps);
                 this.m_nInputReportLength  = hidCaps.InputReportByteLength;
                 this.m_nOutputReportLength = hidCaps.OutputReportByteLength;
                 this.m_oFile = new FileStream(new SafeFileHandle(this.m_hHandle, false), FileAccess.ReadWrite, this.m_nInputReportLength, true);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
                 throw GException0.GenerateWithWinError("Failed to get the detailed data from the hid.");
             }
             finally
             {
                 Win32Usb.HidD_FreePreparsedData(ref lpData);
             }
             return;
         }
         throw GException0.GenerateWithWinError("GetPreparsedData failed");
     }
     this.m_hHandle = IntPtr.Zero;
     throw GException0.GenerateWithWinError("Failed to create device file");
 }
Example #7
0
        // Token: 0x06000024 RID: 36 RVA: 0x00003CB0 File Offset: 0x00001EB0
        private void Initialise(string strPath)
        {
            this.m_hHandle = Win32Usb.CreateFile(strPath, 3221225472u, 0u, IntPtr.Zero, 3u, 1073741824u, IntPtr.Zero);
            if (!(this.m_hHandle != Win32Usb.InvalidHandleValue))
            {
                this.m_hHandle = IntPtr.Zero;
                throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
            }
            IntPtr lpData;

            if (Win32Usb.HidD_GetPreparsedData(this.m_hHandle, out lpData))
            {
                try
                {
                    Win32Usb.HidCaps hidCaps;
                    Win32Usb.HidP_GetCaps(lpData, out hidCaps);
                    this.m_nInputReportLength  = (int)hidCaps.InputReportByteLength;
                    this.m_nOutputReportLength = (int)hidCaps.OutputReportByteLength;
                    HIDDevice.OutputLength     = this.m_nOutputReportLength;
                    HIDDevice.InputLength      = this.m_nInputReportLength;
                    this.m_oFile = new FileStream(new SafeFileHandle(this.m_hHandle, false), FileAccess.ReadWrite, this.m_nInputReportLength, true);
                    this.BeginAsyncRead();
                }
                catch (Exception ex)
                {
                    throw HIDDeviceException.GenerateWithWinError("Failed to get the detailed data from the hid." + ex.ToString());
                }
                finally
                {
                    Win32Usb.HidD_FreePreparsedData(ref lpData);
                }
                return;
            }
            throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
        }
Example #8
0
 // Token: 0x0600001C RID: 28 RVA: 0x00003B78 File Offset: 0x00001D78
 public static IntPtr RegisterForUsbEvents(IntPtr hWnd, Guid gClass)
 {
     Win32Usb.DeviceBroadcastInterface deviceBroadcastInterface = new Win32Usb.DeviceBroadcastInterface();
     deviceBroadcastInterface.Size       = Marshal.SizeOf(deviceBroadcastInterface);
     deviceBroadcastInterface.ClassGuid  = gClass;
     deviceBroadcastInterface.DeviceType = 5;
     deviceBroadcastInterface.Reserved   = 0;
     return(Win32Usb.RegisterDeviceNotification(hWnd, deviceBroadcastInterface, 0u));
 }
Example #9
0
        /// <summary>
        /// Unregisters this application, so it won't be notified for usb events.
        /// </summary>
        /// <returns>Returns if it wass succesfull to unregister.</returns>
        public bool UnregisterHandle()
        {
            if (this.handle != null)
            {
                return(Win32Usb.UnregisterForUsbEvents(this.handle));
            }

            return(false);
        }
Example #10
0
        // Token: 0x0600002A RID: 42 RVA: 0x00003F40 File Offset: 0x00002140
        private static string GetDevicePath(IntPtr hInfoSet, ref Win32Usb.DeviceInterfaceData oInterface)
        {
            uint nDeviceInterfaceDetailDataSize = 0u;

            if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0u, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero))
            {
                Win32Usb.DeviceInterfaceDetailData deviceInterfaceDetailData = default(Win32Usb.DeviceInterfaceDetailData);
                deviceInterfaceDetailData.Size = 5;
                if (Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref deviceInterfaceDetailData, nDeviceInterfaceDetailDataSize, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero))
                {
                    return(deviceInterfaceDetailData.DevicePath);
                }
            }
            return(null);
        }
Example #11
0
        private static string GetDevicePath(IntPtr hInfoSet, ref Win32Usb.DeviceInterfaceData oInterface)
        {
            uint nRequiredSize = 0;

            if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
            {
                Win32Usb.DeviceInterfaceDetailData oDetailData = new Win32Usb.DeviceInterfaceDetailData {
                    Size = 5
                };
                if (Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetailData, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
                {
                    return(oDetailData.DevicePath);
                }
            }
            return(null);
        }
Example #12
0
 protected virtual void Dispose(bool bDisposing)
 {
     try
     {
         if (bDisposing && (this.m_oFile != null))
         {
             this.m_oFile.Close();
             this.m_oFile = null;
         }
         if (this.m_hHandle != IntPtr.Zero)
         {
             Win32Usb.CloseHandle(this.m_hHandle);
         }
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception.ToString());
     }
 }
Example #13
0
        private static string smethod_0(IntPtr intptr_0, ref DeviceInterfaceData deviceInterfaceData_0)
        {
            uint nDeviceInterfaceDetailDataSize = 0u;

            if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(intptr_0, ref deviceInterfaceData_0, IntPtr.Zero, 0u, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero))
            {
                DeviceInterfaceDetailData deviceInterfaceDetailData = default(DeviceInterfaceDetailData);
                if (IntPtr.Size == 4)
                {
                    deviceInterfaceDetailData.Size = 5;
                }
                else
                {
                    deviceInterfaceDetailData.Size = 8;
                }
                if (Win32Usb.SetupDiGetDeviceInterfaceDetail(intptr_0, ref deviceInterfaceData_0, ref deviceInterfaceDetailData, nDeviceInterfaceDetailDataSize, ref nDeviceInterfaceDetailDataSize, IntPtr.Zero))
                {
                    return(deviceInterfaceDetailData.DevicePath);
                }
            }
            return(null);
        }
Example #14
0
        // Token: 0x06000137 RID: 311 RVA: 0x000153FC File Offset: 0x000135FC
        public bool UnregisterHandle()
        {
            bool flag = 1 == 0;

            return(Win32Usb.UnregisterForUsbEvents(this.handle));
        }
Example #15
0
 public bool UnregisterHandle() =>
 Win32Usb.UnregisterForUsbEvents(this.handle);
Example #16
0
 /// <summary>
 /// Регистрация для получения событий USB-шины.
 /// </summary>
 /// <param name="Handle">= IntPtr, путь до приложения.</param>
 public void RegisterHandle(IntPtr Handle)
 {
     usb_event_handle = Win32Usb.RegisterForUsbEvents(Handle, device_class);
     this.handle      = Handle;
     CheckDevicePresent();
 }
Example #17
0
 // Token: 0x0600001D RID: 29 RVA: 0x00003BBC File Offset: 0x00001DBC
 public static bool UnregisterForUsbEvents(IntPtr hHandle)
 {
     return(Win32Usb.UnregisterDeviceNotification(hHandle));
 }