Beispiel #1
0
        /// <summary>
        /// USB HIDデバイスをオープンする
        /// </summary>
        /// <param name="vid">デバイスのVID</param>
        /// <param name="pid">デバイスのPID</param>
        /// <returns>成功した場合true、存在しなかった場合false</returns>
        /// <exception cref="InvalidOperationException">デバイスをオープンできなかった場合</exception>
        public bool Open(uint vid, uint pid)
        {
            string path = string.Format(@"\\?\hid#vid_{0,0:x4}&pid_{1,0:x4}", vid, pid);

            Guid guid = new Guid();

            Native.HidD_GetHidGuid(ref guid);

            IntPtr hDeviceInfoSet = Native.SetupDiGetClassDevs(ref guid, 0, IntPtr.Zero, Native.DIGCF_PRESENT | Native.DIGCF_DEVICEINTERFACE);

            Native.SP_DEVICE_INTERFACE_DATA spid = new Native.SP_DEVICE_INTERFACE_DATA();
            spid.cbSize = Marshal.SizeOf(spid);
            int i = 0;

            while (Native.SetupDiEnumDeviceInterfaces(hDeviceInfoSet, null, ref guid, i, spid))
            {
                i++;

                Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
                devData.cbSize = Marshal.SizeOf(devData);
                int size = 0;
                Native.SetupDiGetDeviceInterfaceDetail(hDeviceInfoSet, spid, IntPtr.Zero, 0, ref size, devData);

                IntPtr buffer = Marshal.AllocHGlobal(size);
                Native.SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new Native.SP_DEVICE_INTERFACE_DETAIL_DATA();
                detailData.cbSize = Marshal.SizeOf(typeof(Native.SP_DEVICE_INTERFACE_DETAIL_DATA));
                Marshal.StructureToPtr(detailData, buffer, false);
                Native.SetupDiGetDeviceInterfaceDetail(hDeviceInfoSet, spid, buffer, size, ref size, devData);
                IntPtr pDevicePath = (IntPtr)((int)buffer + Marshal.SizeOf(typeof(int)));
                string devicePath  = Marshal.PtrToStringAuto(pDevicePath);
                Marshal.FreeHGlobal(buffer);

                if (devicePath.IndexOf(path) == 0)
                {
                    hDev = Native.CreateFile(
                        devicePath,
                        Native.GENERIC_READ | Native.GENERIC_WRITE,
                        0,
                        IntPtr.Zero,
                        Native.OPEN_EXISTING,
                        0,
                        IntPtr.Zero
                        );
                    break;
                }
            }

            Native.SetupDiDestroyDeviceInfoList(hDeviceInfoSet);

            if (hDev.ToInt32() <= -1)
            {
                hDev = IntPtr.Zero;
                throw new InvalidOperationException(GetErrorMessage());
            }
            else if (DeviceReady)
            {
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// USB HIDデバイスをオープンする
        /// </summary>
        /// <param name="vid">デバイスのVID</param>
        /// <param name="pid">デバイスのPID</param>
        /// <returns>成功した場合true、存在しなかった場合false</returns>
        /// <exception cref="InvalidOperationException">デバイスをオープンできなかった場合</exception>
        public bool Open( uint vid, uint pid )
        {
            string path = string.Format( @"\\?\hid#vid_{0,0:x4}&pid_{1,0:x4}", vid, pid );

            Guid guid = new Guid();
            Native.HidD_GetHidGuid( ref guid );

            IntPtr hDeviceInfoSet = Native.SetupDiGetClassDevs( ref guid, 0, IntPtr.Zero, Native.DIGCF_PRESENT | Native.DIGCF_DEVICEINTERFACE );

            Native.SP_DEVICE_INTERFACE_DATA spid = new Native.SP_DEVICE_INTERFACE_DATA();
            spid.cbSize = Marshal.SizeOf( spid );
            int i = 0;
            while ( Native.SetupDiEnumDeviceInterfaces( hDeviceInfoSet, null, ref guid, i, spid ) ) {
                i++;

                Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
                devData.cbSize = Marshal.SizeOf( devData );
                int size = 0;
                Native.SetupDiGetDeviceInterfaceDetail( hDeviceInfoSet, spid, IntPtr.Zero, 0, ref size, devData );

                IntPtr buffer = Marshal.AllocHGlobal( size );
                Native.SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new Native.SP_DEVICE_INTERFACE_DETAIL_DATA();
                detailData.cbSize = Marshal.SizeOf( typeof( Native.SP_DEVICE_INTERFACE_DETAIL_DATA ) );
                Marshal.StructureToPtr( detailData, buffer, false );
                Native.SetupDiGetDeviceInterfaceDetail( hDeviceInfoSet, spid, buffer, size, ref size, devData );
                IntPtr pDevicePath = (IntPtr)( (int)buffer + Marshal.SizeOf( typeof( int ) ) );
                string devicePath = Marshal.PtrToStringAuto( pDevicePath );
                Marshal.FreeHGlobal( buffer );

                if ( devicePath.IndexOf( path ) == 0 ) {
                    hDev = Native.CreateFile(
                        devicePath,
                        Native.GENERIC_READ | Native.GENERIC_WRITE,
                        0,
                        IntPtr.Zero,
                        Native.OPEN_EXISTING,
                        0,
                        IntPtr.Zero
                    );
                    break;
                }
            }

            Native.SetupDiDestroyDeviceInfoList( hDeviceInfoSet );

            if ( hDev.ToInt32() <= -1 ) {
                hDev = IntPtr.Zero;
                throw new InvalidOperationException( GetErrorMessage() );
            } else if ( DeviceReady ) {
                return true;
            }
            return false;
        }