public List <string> GetDevicesList() { List <string> devices = new List <string>(); this.DevicePath = string.Empty; myUSB.CT_HidGuid(); myUSB.CT_SetupDiGetClassDevs(); bool?result = null; bool resultb = false; int device_count = 0; int size = 0; int requiredSize = 0; int numberOfDevices = 0; //search the device until you have found it or no more devices in list while (result.HasValue == false || result.Value == true) { //open the device result = myUSB.CT_SetupDiEnumDeviceInterfaces(device_count); //get size of device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceBuffer(ref requiredSize, 0); size = requiredSize; //get device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, size); //is this the device i want? string deviceID = this.VID; if (myUSB.DevicePathName.IndexOf(deviceID) > 0) { //create HID Device Handel resultb = myUSB.CT_CreateFile(myUSB.DevicePathName); // Check the serial Number myUSB.CT_HidD_GetHIDSerialNumber(out string device_sn); myUSB.CT_CloseFile(); if (device_sn != "") { devices.Add(device_sn); } numberOfDevices++; } device_count++; } myUSB.CT_SetupDiDestroyDeviceInfoList(); return(devices); }
bool SearchDevice() { this.DevicePath = string.Empty; myUSB.CT_HidGuid(); // Get the GUID of the HID device Class myUSB.CT_SetupDiGetClassDevs(); // Get the device information set bool?result = null; bool resultb = false; int device_count = 0; int size = 0; int requiredSize = 0; // Reset the IsConnected to false this.IsConnected = false; //search the device until you have found it or no more devices in list while (!result.HasValue || result.Value == true) { // //if (result == false) // break; //open the device result = myUSB.CT_SetupDiEnumDeviceInterfaces(device_count); //get size of device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceBuffer(ref requiredSize, 0); size = requiredSize; //get device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, size); if (resultb == false) { int err = Marshal.GetLastWin32Error(); } this.DevicePath = myUSB.DevicePathName; //is this the device i want? string deviceID = this.VID + "&" + this.PID; if (this.DevicePath.ToLower().IndexOf(deviceID.ToLower()) > 0) { //create HID Device Handel resultb = myUSB.CT_CreateFile(this.DevicePath); // Check the serial Number myUSB.CT_HidD_GetHIDSerialNumber(out string device_sn); if (this.SerialNumber == null || this.SerialNumber == string.Empty || device_sn == this.SerialNumber) { IntPtr myPtrToPreparsedData = default(IntPtr); if (myUSB.CT_HidD_GetPreparsedData(myUSB.hHidFile, ref myPtrToPreparsedData)) { bool code = myUSB.CT_HidP_GetCaps(myPtrToPreparsedData); int reportLength = myUSB.myHIDP_CAPS.InputReportByteLength; //we have found our device so stop searching this.IsConnected = true; this.SerialNumber = device_sn; } break; } else { myUSB.CT_CloseFile(); } } device_count++; } myUSB.CT_SetupDiDestroyDeviceInfoList(); //return state return(this.IsConnected); }