public string[] DevClassList() { List <string> HWList = new List <string>(); try { UInt32 RequiredSize = 0; Guid guid = Guid.Empty; Guid[] guids = new Guid[1]; bool res = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize); if (res == false) { guids = new Guid[RequiredSize]; res = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize); if (!res || RequiredSize == 0) { Debug.WriteLine("类型不正确"); } } if (res == true) { List <DeviceEntity> list = new List <Hardware.DeviceEntity>(); Guid myGUID = System.Guid.Empty; IntPtr hDevInfo = SetupDiGetClassDevs(guids[0], 0, IntPtr.Zero, DIGCF_NOSET); Debug.WriteLine("枚举设备 : " + Externs.GetLastError() + "---" + Marshal.GetLastWin32Error() + hDevInfo.ToInt64()); if (hDevInfo.ToInt64() == Externs.INVALID_HANDLE_VALUE) { throw new Exception("Invalid Handle"); } else { Externs.SP_DEVINFO_DATA DeviceInfoData; DeviceInfoData = new Externs.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); DeviceInfoData.devInst = 0; DeviceInfoData.classGuid = System.Guid.Empty; DeviceInfoData.reserved = 0; StringBuilder DeviceID = new StringBuilder("", 254); for (uint i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) { if (CM_Get_Device_IDA(DeviceInfoData.devInst, DeviceID, 254, 0) == CR_SUCCESS) { HWList.Add(DeviceID.ToString()); } } } Externs.SetupDiDestroyDeviceInfoList(hDevInfo); } } catch (Exception ex) { throw new Exception("枚举设备列表出错", ex); } return(HWList.ToArray()); }
/// <summary> /// 设置指定设备的状态 /// </summary> /// <param name="match">设备名称</param> /// <param name="bEnable">是否启用</param> /// <returns></returns> public bool SetState(string[] match, bool bEnable) { try { UInt32 RequiredSize = 0; Guid guid = Guid.Empty; Guid[] guids = new Guid[1]; bool res = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize); if (res == false) { guids = new Guid[RequiredSize]; res = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize); if (!res || RequiredSize == 0) { Debug.WriteLine("类型不正确"); } } if (res == true) { List <DeviceEntity> list = new List <Hardware.DeviceEntity>(); Guid myGUID = System.Guid.Empty; IntPtr hDevInfo = SetupDiGetClassDevs(guids[0], 0, IntPtr.Zero, DIGCF_NOSET); Debug.WriteLine("枚举设备 : " + Externs.GetLastError() + "---" + Marshal.GetLastWin32Error() + hDevInfo.ToInt64()); if (hDevInfo.ToInt64() == Externs.INVALID_HANDLE_VALUE) { throw new Exception("Invalid Handle"); } else { Externs.SP_DEVINFO_DATA DeviceInfoData; DeviceInfoData = new Externs.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); DeviceInfoData.devInst = 0; DeviceInfoData.classGuid = System.Guid.Empty; DeviceInfoData.reserved = 0; UInt32 i; StringBuilder DeviceID = new StringBuilder("", 256); DeviceID.Capacity = Externs.MAX_DEV_LEN; for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) { //while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero)) //{ //} if (CM_Get_Device_IDA(DeviceInfoData.devInst, DeviceID, 254, 0) == CR_SUCCESS) { bool bMatch = true; foreach (string search in match) { if (!DeviceID.ToString().ToLower().Contains(search.ToLower())) { bMatch = false; break; } } if (bMatch) { OpenClose(hDevInfo, DeviceInfoData, bEnable, true); } } } Externs.SetupDiDestroyDeviceInfoList(hDevInfo); } } } catch (Exception ex) { throw new Exception("枚举设备信息出错!", ex); return(false); } return(true); }