Beispiel #1
0
        internal void Start(string Path)
        {
            if (StartProgress != null)
            {
                StartProgress(this, null);
            }

            string strPath = string.Empty;
//            string strSearch = string.Format( "vid_{0:x4}&pid_{1:x4}", nVid, nPid ); // first, build the path search string
            //Guid gHid;
            //CWin32.HidD_GetHidGuid( out gHid );	// next, get the GUID from Windows that it uses to represent the HID USB interface

            Guid gHid = new Guid("86e0d1e0808911d09ce408003e301f73");

            IntPtr hInfoSet = CWin32.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, CWin32.DIGCF_DEVICEINTERFACE | CWin32.DIGCF_PRESENT);     // this gets a list of all HID devices currently connected to the computer (InfoSet)

            try
            {
                CWin32.DeviceInterfaceData oInterface = new CWin32.DeviceInterfaceData();       // build up a device interface data block
                oInterface.Size = Marshal.SizeOf(oInterface);
                // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
                // to get device details for each device connected
                int nIndex = 0;
                while (CWin32.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) // this gets the device interface information for a device at index 'nIndex' in the memory block
                {
                    string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);                             // get the device path (see helper method 'GetDevicePath')
                    //if ( strDevicePath.IndexOf( strSearch ) >= 0 )	// do a string search, if we find the VID/PID string then we found our device!
                    //{
                    //    HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance( oType );	// create an instance of the class for this device
                    //    oNewDevice.Initialise( strDevicePath );	// initialise it with the device path
                    //    return oNewDevice;	// and return it
                    //}
                    nIndex++;   // if we get here, we didn't find our device. So move on to the next one.
                }
            }
            finally
            {
                // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
                CWin32.SetupDiDestroyDeviceInfoList(hInfoSet);
            }
//            return null;	// oops, didn't find our device



            if (EndProgress != null)
            {
                EndProgress(this, null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Helper method to return the device path given a DeviceInterfaceData structure and an InfoSet handle.
        /// Used in 'FindDevice' so check that method out to see how to get an InfoSet handle and a DeviceInterfaceData.
        /// </summary>
        /// <param name="hInfoSet">Handle to the InfoSet</param>
        /// <param name="oInterface">DeviceInterfaceData structure</param>
        /// <returns>The device path or null if there was some problem</returns>
        private static string GetDevicePath(IntPtr hInfoSet, ref CWin32.DeviceInterfaceData oInterface)
        {
            uint nRequiredSize = 0;

            // Get the device interface details
            if (!CWin32.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
            {
                CWin32.DeviceInterfaceDetailData oDetail = new CWin32.DeviceInterfaceDetailData();
                oDetail.Size = 5;       // hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
                if (CWin32.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
                {
                    return(oDetail.DevicePath);
                }
            }
            return(null);
        }
        internal void Start ( string Path )
        {
            if ( StartProgress != null )
                StartProgress( this, null );

            string strPath = string.Empty;
//            string strSearch = string.Format( "vid_{0:x4}&pid_{1:x4}", nVid, nPid ); // first, build the path search string
            //Guid gHid;
            //CWin32.HidD_GetHidGuid( out gHid );	// next, get the GUID from Windows that it uses to represent the HID USB interface

            Guid gHid = new Guid( "86e0d1e0808911d09ce408003e301f73" );

            IntPtr hInfoSet = CWin32.SetupDiGetClassDevs( ref gHid, null, IntPtr.Zero, CWin32.DIGCF_DEVICEINTERFACE | CWin32.DIGCF_PRESENT );	// this gets a list of all HID devices currently connected to the computer (InfoSet)
            try
            {
                CWin32.DeviceInterfaceData oInterface = new CWin32.DeviceInterfaceData();	// build up a device interface data block
                oInterface.Size = Marshal.SizeOf( oInterface );
                // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
                // to get device details for each device connected
                int nIndex = 0;
                while ( CWin32.SetupDiEnumDeviceInterfaces( hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface ) )	// this gets the device interface information for a device at index 'nIndex' in the memory block
                {
                    string strDevicePath = GetDevicePath( hInfoSet, ref oInterface );	// get the device path (see helper method 'GetDevicePath')
                    //if ( strDevicePath.IndexOf( strSearch ) >= 0 )	// do a string search, if we find the VID/PID string then we found our device!
                    //{
                    //    HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance( oType );	// create an instance of the class for this device
                    //    oNewDevice.Initialise( strDevicePath );	// initialise it with the device path
                    //    return oNewDevice;	// and return it
                    //}
                    nIndex++;	// if we get here, we didn't find our device. So move on to the next one.
                }
            }
            finally
            {
                // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
                CWin32.SetupDiDestroyDeviceInfoList( hInfoSet );
            }
//            return null;	// oops, didn't find our device



            if ( EndProgress != null )
                EndProgress( this, null );
        }