和this question有关但是..。是否可以通过API触发新的硬件设备扫描?我有一个串口蓝牙设备,我正在通过32feet.net .NET Bluetooth的API调用自动配对,它工作得很好。虽然我可以在扫描设备时查询串行服务,但COM端口没有显示在Bluetooth Devices对话框的COM端口选项卡中。
发布于 2010-02-03 01:10:13
与Programmatically uninstall a device in windows device manager相关
我的回答是:
强制“扫描硬件更改”签出"How To Force Reenumeration of a Device Tree From an Application“该示例显示如何强制重新枚举整个树。
发布于 2010-05-15 01:24:40
我不确定这是否会对你的整体问题有所帮助,但这应该会回答你第一句话中的问题。
几年前,我最初使用Nullsoft的NSIS安装程序做过类似的事情。
如果你只想触发一个普通的硬件扫描,你可以使用下面的代码(根据这个问题中的.net标签在C#中提供):
Invoke这是P/Invoke函数的包装类
public static class Win32Api
{
public const int CM_LOCATE_DEVNODE_NORMAL = 0x00000000;
public const int CM_REENUMERATE_NORMAL = 0x00000000;
public const int CR_SUCCESS = 0x00000000;
[DllImport("CfgMgr32.dll", SetLastError=true)]
public static extern int CM_Locate_DevNodeA(ref int pdnDevInst, string pDeviceID, int ulFlags);
[DllImport("CfgMgr32.dll", SetLastError=true)]
public static extern int CM_Reenumerate_DevNode(int dnDevInst, int ulFlags);
}
这是一个如何使用的示例
int pdnDevInst = 0;
if (Win32Api.CM_Locate_DevNodeA(ref pdnDevInst, null, Win32Api.CM_LOCATE_DEVNODE_NORMAL) != Win32Api.CR_SUCCESS)
throw new Exception("something...");
if (Win32Api.CM_Reenumerate_DevNode(pdnDevInst, Win32Api.CM_REENUMERATE_NORMAL) != Win32Api.CR_SUCCESS)
throw new Exception("something else...");
我只是快速地从MSDN C++文档中翻译出来,并在一个峰值中测试了它,所以我知道它可以工作,但它不是生产质量。此外,如果您关心特定的返回码,可以在cfgmgr32.h中查找它们。
发布于 2010-02-03 21:49:23
下面的答案有帮助吗?How can I find out a COM port number of a bluetooth device in c#?
简而言之,使用System.IO.Ports.SerialPort.GetPortNames()
或WMI列出串行端口,例如PowerShell命令:
C:\> Get-WmiObject -query "select DeviceID,PNPDeviceID from Win32_SerialPort"
这也可以在代码中完成。
https://stackoverflow.com/questions/2181525
复制相似问题