private void UpdateListButton_Click(object sender, EventArgs e) { this.Enabled = true; Cursor.Current = Cursors.WaitCursor; Application.DoEvents(); try { this.DeviceTypeComboBox.Items.Clear(); //////////////////////////////// ///// 获取设备列表存到数组中,最多存储127个设备。GBMSAPI_NET_MAX_PLUGGED_DEVICE_NUM=127常量 //////////////////////////////// GBMSAPI_NET_DeviceInfoStruct[] AttachedDeviceList = new GBMSAPI_NET_DeviceInfoStruct[ GBMSAPI_NET_DeviceInfoConstants.GBMSAPI_NET_MAX_PLUGGED_DEVICE_NUM]; Console.WriteLine(GBMSAPI_NET_DeviceInfoConstants.GBMSAPI_NET_MAX_PLUGGED_DEVICE_NUM); for (int i = 0; i < GBMSAPI_NET_DeviceInfoConstants.GBMSAPI_NET_MAX_PLUGGED_DEVICE_NUM; i++) { AttachedDeviceList[i] = new GBMSAPI_NET_DeviceInfoStruct(); } int AttachedDeviceNumber; uint USBErrorCode; int RetVal = GBMSAPI_NET_DeviceSettingRoutines.GBMSAPI_NET_GetAttachedDeviceList( AttachedDeviceList, out AttachedDeviceNumber, out USBErrorCode); Console.WriteLine(AttachedDeviceNumber); if (RetVal != GBMSAPI_NET_ErrorCodes.GBMSAPI_NET_ERROR_CODE_NO_ERROR || AttachedDeviceNumber <= 0) { if (RetVal != GBMSAPI_NET_ErrorCodes.GBMSAPI_NET_ERROR_CODE_NO_ERROR) { GBMSAPI_Example_Util.GBMSAPI_Example_ManageErrors(RetVal, USBErrorCode, "UpdateListButton_Click"); MessageBox.Show("Error in GBMSAPI_NET_GetAttachedDeviceList function: " + RetVal); } return; } // Console.WriteLine(RetVal); //////////////////////////////// //// Store device list 存储设备列表 //////////////////////////////// for (int i = 0; i < AttachedDeviceNumber; i++) { String StrToAdd = GBMSAPI_Example_Util.GBMSAPI_Example_GetDevNameFromDevID( AttachedDeviceList[i].DeviceID); StrToAdd += " " + (AttachedDeviceList[i].DeviceSerialNumber);//获取设备型号 this.DeviceTypeComboBox.Items.Add(StrToAdd); } if (AttachedDeviceNumber > 0) { this.DeviceTypeComboBox.SelectedIndex = 0;//指定索引号为0 } } catch (Exception ex) { MessageBox.Show("Error in UpdateListButton_Click function: " + ex.Message); this.Close(); } finally { Cursor.Current = Cursors.Default; this.Enabled = true; } }
/************************************** * GBMSAPI_GetAttachedDeviceList **************************************/ /// <summary> /// See GBMSAPI_GetAttachedDeviceList(...) function in GBMSAPI_Library.h file /// </summary> /// <param name="AttachedDeviceList"></param> /// <param name="AttachedDeviceNumber"></param> /// <param name="USBErrorCode"></param> /// <returns></returns> public static Int32 GBMSAPI_NET_GetAttachedDeviceList( GBMSAPI_NET_DeviceInfoStruct[] AttachedDeviceList, out Int32 AttachedDeviceNumber, out UInt32 USBErrorCode) { // check parameters if (AttachedDeviceList == null || AttachedDeviceList.Length < GBMSAPI_NET_DeviceInfoConstants.GBMSAPI_NET_MAX_PLUGGED_DEVICE_NUM) { USBErrorCode = 0; AttachedDeviceNumber = 0; return GBMSAPI_NET_ErrorCodes.GBMSAPI_NET_ERROR_CODE_PARAMETER; } try { GBMSAPI_NET_DLL_WRAPPER.GBMSAPI_DEVICE_INFO_STRUCT[] WrapAttDevList = new GBMSAPI_NET_DLL_WRAPPER.GBMSAPI_DEVICE_INFO_STRUCT [GBMSAPI_NET_DeviceInfoConstants.GBMSAPI_NET_MAX_PLUGGED_DEVICE_NUM]; Int32 RetVal = GBMSAPI_NET_DLL_WRAPPER.GBMSAPI_GetAttachedDeviceList( WrapAttDevList, out AttachedDeviceNumber, out USBErrorCode); for (int i = 0; i < AttachedDeviceNumber; i++) { AttachedDeviceList[i].DeviceID = WrapAttDevList[i].DeviceID; AttachedDeviceList[i].DeviceSerialNumber = "" + WrapAttDevList[i].DeviceSerialNumber; } return RetVal; } catch (Exception ex) { USBErrorCode = 0; AttachedDeviceNumber = 0; GBMSAPI_NET_ErrorCodes.GBMSAPI_NET_ERROR_CODE_EXCEPTION_STRING = "GBMSAPI_NET_GetAttachedDeviceList: " + ex.Message; return GBMSAPI_NET_ErrorCodes.GBMSAPI_NET_ERROR_CODE_EXCEPTION; } }