windows 是否有 API 调用来启动硬件设备扫描

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2181525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:52:17  来源:igfitidea点击:

Is there an API call to start a scan for hardware devices

.netwindowswinapibluetooth

提问by kenny

Related to this question, but... is it possible to trigger via API a new hardware device scan? I have a serial port Bluetooth device that I'm pairing automatically via API calls with 32feet.net .NET Bluetooth, which works quite nicely. While i can query for the serial services in the scanning of the device, the COM ports don't show up in the Bluetooth Devices dialog's COM Ports tab.

此问题相关,但是...是否可以通过 API 触发新的硬件设备扫描?我有一个串行端口蓝牙设备,我通过 API 调用与32feet.net .NET Bluetooth自动配对,它工作得很好。虽然我可以在扫描设备时查询串行服务,但 COM 端口没有显示在蓝牙设备对话框的 COM 端口选项卡中。

采纳答案by Ruddy

Related to Programmatically uninstall a device in windows device manager

在 Windows 设备管理器中编程方式卸载设备相关

My answer from there:

我从那里回答:

To force the 'scan for hardware changes' checkout "How To Force Reenumeration of a Device Tree From an Application" the sample there shows how to force the entire tree to be re-enumerated.

要强制“扫描硬件更改”,请查看“如何从应用程序强制重新枚举设备树”,那里的示例显示了如何强制重新枚举整个树。

回答by Jeronimo Colon III

Not sure if this will help your overall problem but this should answer the question in your first sentence.

不确定这是否会帮助您解决整体问题,但这应该可以回答您第一句话中的问题。

I originally did something like this using Nullsoft's NSIS installer a few years ago.

几年前,我最初使用 Nullsoft 的 NSIS 安装程序做了类似的事情。

If you just want to trigger a vanilla hardware scan you can use the following code (provided in C# per the .net tag in this question):

如果您只想触发 vanilla 硬件扫描,您可以使用以下代码(在此问题中的 .net 标记中以 C# 提供):

This is the wrapper class for the P/Invoke functions

这是 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);
}

This is a sample of how to use them

这是如何使用它们的示例

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...");

I just quickly translated this from the MSDN C++ docs and tested it in a spike so I know it works but it's not production quality. Also, if you care about the specific return codes you can look them up in cfgmgr32.h.

我只是从 MSDN C++ 文档中快速翻译了它并在一个尖峰中对其进行了测试,所以我知道它可以工作,但它不是生产质量。此外,如果您关心特定的返回代码,您可以在 cfgmgr32.h 中查找它们。

回答by alanjmcf

Does my answer at the following help? How can I find out a COM port number of a bluetooth device in c#?

我在下面的回答有帮助吗?如何在 C# 中找出蓝牙设备的 COM 端口号?

In brief use System.IO.Ports.SerialPort.GetPortNames()or WMI to list the serial ports, e.g. PowerShell command:

简要使用System.IO.Ports.SerialPort.GetPortNames()或 WMI 列出串行端口,例如 PowerShell 命令:

C:\> Get-WmiObject -query "select DeviceID,PNPDeviceID from Win32_SerialPort" 

Which can also be done in code.

这也可以在代码中完成。

回答by kenny

Just found this SO postthat also may solve my issue or others that find this question.

刚刚发现这个 SO post也可以解决我的问题或其他发现这个问题的问题。