下面是我在網上找到的搜索本機上的USB設備後再根據PNPDeceiveId打開USB設備,可是不知道為什麼在自己的電腦上找不到相應設備,句柄的返回值總是-1,麻煩大家幫忙看一下哪裡錯了,
public static IntPtr openDeceive(String PNPDeviceID)
{
//if (String.IsNullOrEmpty(PNPDeviceID)) return null;
// 打開設備
IntPtr hHCDev = Kernel32.CreateFile(
"\\\\.\\" + PNPDeviceID.Replace('\\', '#') + "#{3ABF6F2D-71C4-462A-8A92-1E6861E6AF27}",
NativeFileAccess.GENERIC_WRITE,
NativeFileShare.FILE_SHARE_WRITE,
IntPtr.Zero,
NativeFileMode.OPEN_EXISTING,
IntPtr.Zero,
IntPtr.Zero);
// if (hHCDev == Kernel32.INVALID_HANDLE_VALUE) return null;
return hHCDev;
}
Kernel32.CreateFile方法如下:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CreateFile(
String fileName,
[MarshalAs(UnmanagedType.U4)] NativeFileAccess fileAccess,
[MarshalAs(UnmanagedType.U4)] NativeFileShare fileShare,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] NativeFileMode creationDisposition,
IntPtr flags,
IntPtr template
);
PNPDeceiveId獲得方法如下:
public static PnPEntityInfo[] AllUsbDevices
{
get
{
return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
}
}
/// <summary>
/// 查詢USB設備實體(設備要求有VID和PID)
/// </summary>
/// <param name="VendorID">供應商標識,MinValue忽視</param>
/// <param name="ProductID">產品編號,MinValue忽視</param>
/// <param name="ClassGuid">設備安裝類Guid,Empty忽視</param>
/// <returns>設備列表</returns>
public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid)
{
List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();
// 獲取USB控制器及其相關聯的設備實體
ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
if (USBControllerDeviceCollection != null)
{
foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
{ // 獲取設備實體的DeviceID
String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];
// 過濾掉沒有VID和PID的USB設備
Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
if (match.Success)
{
UInt16 theVendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16); // 供應商標識
if (VendorID != UInt16.MinValue && VendorID != theVendorID) continue;
UInt16 theProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 產品編號
if (ProductID != UInt16.MinValue && ProductID != theProductID) continue;
ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get();
if (PnPEntityCollection != null)
{
foreach (ManagementObject Entity in PnPEntityCollection)
{
Guid theClassGuid = new Guid(Entity["ClassGuid"] as String); // 設備安裝類GUID
if (ClassGuid != Guid.Empty && ClassGuid != theClassGuid) continue;
PnPEntityInfo Element;
Element.PNPDeviceID = Entity["PNPDeviceID"] as String; // 設備ID
Element.Name = Entity["Name"] as String; // 設備名稱
Element.Description = Entity["Description"] as String; // 設備描述
Element.Service = Entity["Service"] as String; // 服務
Element.Status = Entity["Status"] as String; // 設備狀態
Element.VendorID = theVendorID; // 供應商標識
Element.ProductID = theProductID; // 產品編號
Element.ClassGuid = theClassGuid; // 設備安裝類GUID
UsbDevices.Add(Element);
}
}
}
}
}
if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
}
通過 AllUsbDevices得到USB設備列表,之後獲得其中一個設備的PNPDeceiveId,然後調用openDeceive方法,可是返回值總是-1,是一個無效值,麻煩大家幫忙找一下錯誤
我曾經試驗過,.net不能訪問設備驅動程序,也就是說"\.\xxxx"的文件是打不開的。