我想知道,如果特定介面(USB 除錯)的(Android)USB 設備連接到我的 Windows 計算機。
為此,我正在嘗試將 .Net 與以下代碼一起使用:
const string GUID_DEVINTERFACE_ANDROID = "f72fe0d4-cbcb-407d-8814-9ed673d0dd6b";
const string usbDeviceSelector = "System.Devices.InterfaceClassGuid:=\"{" GUID_DEVINTERFACE_ANDROID "}\" AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True";
_usbDeviceWatcher = DeviceInformation.CreateWatcher(
usbDeviceSelector,
new string[] { "System.Devices.InterfaceEnabled" },
DeviceInformationKind.AssociationEndpoint);
_usbDeviceWatcher.Updated = UsbDeviceWatcher_Updated;
不幸的是,該事件不會被拋出到我的UsbDeviceWatcher_Update函式中。
我不想通知特定設備,我想通知所有支持此介面的設備。
如果具有此特殊介面的設備將與我的計算機連接/斷開連接,我如何獲取事件?
如果有WinUsb解決方案,我也會很高興。
uj5u.com熱心網友回復:
使用介面類過濾 USB 設備,{f72fe0d4-cbcb-407d-8814-9ed673d0dd6b}然后訂閱Added和Removed事件:
using Windows.Devices.Enumeration;
using Windows.Devices.Usb;
var filter = UsbDevice.GetDeviceSelector(new Guid("f72fe0d4-cbcb-407d-8814-9ed673d0dd6b"));
var usbDeviceWatcher = DeviceInformation.CreateWatcher(filter);
usbDeviceWatcher.Added = UsbDeviceWatcher_Added;
usbDeviceWatcher.Removed = UsbDeviceWatcher_Removed;
usbDeviceWatcher.Start();
Console.WriteLine("Press key to exit...");
Console.ReadKey();
void UsbDeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
Console.WriteLine("Added");
}
void UsbDeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
{
Console.WriteLine("Removed");
}
或者,您也可以過濾介面類 0xff、子類 0x42 和協議 0x01:
var filter = UsbDevice.GetDeviceClassSelector(new UsbDeviceClass() {
ClassCode = 0xff,
SubclassCode = 0x42,
ProtocolCode = 0x01
});
您還可以實施 David Grayson 提出的方法。但這是相當多的努力。
uj5u.com熱心網友回復:
在 Windows 中獲得新的或洗掉的 USB 設備通知的標準方法是偵聽作業系統發送到您的視窗的 WM_DEVICECHANGE 訊息。這會給你帶來比你關心的更多的通知,所以每當你收到這條訊息時,你應該使用你最喜歡的庫或程式來列出你關心的設備,看看是否有任何變化。
您將需要覆寫您的 WindowsWndProc訊息。此處的檔案討論了如何在 C# 中執行此操作:
https ://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.wndproc?view=windowsdesktop-7.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/523961.html
上一篇:為什么windows上的curl命令會生成/bin/bash:line/:$'\r':commandnotfound錯誤?
