下面这一段时Delphi代码,用于通过USB接口与电话测试机建立连接。用C#代码应该怎么写,大家帮忙看看,谢谢~
begin try try aList:=TStringList.Create; Locator := CreateOleObject('WbemScripting.SWbemLocator'); WMI := Locator.ConnectServer('.', '', '',''); Ret := WMI.ExecQuery('SELECT * FROM Win32_USBHub');//这里Win32_USBHub为检测项,详见下表 Enum:= IUnknown(RET._NewEnum) as IEnumVariant; //Tmp为检测到返回的数据集合,其中PNPDeviceID为集合中的对象,包含了PID和VID码 while (Enum.Next(1, Tmp, Value) = S_OK) do aList.Add(Tmp.PNPDeviceID); //if aList.IndexOf('USB\VID_1267&PID_5AA5\5&16BBD8B4&0&1')>0 then //设备存在 if pos('USB\VID_1267',alist.CommaText)>0 then begin //BindWindow(form1.Handle);//绑定窗口 BindWindow(handle);//绑定窗口 StatusBar1.Panels[0].Text:='已连接'; button1.Tag :=100; button1.Caption :='解除绑定'; end else begin button1.Tag :=0; button1.Caption :='窗口绑定'; BindWindow(handle);//绑定窗口 StatusBar1.Panels[0].Text:='已断开'; end; finally alist.Destroy; end; except on e:exception do exit; end; end;
也许有点帮助:
http://www.developerfusion.com/article/84338/making-usb-c-friendly/