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

纵然是瞬间 提交于 2019-11-27 03:20:54

问题


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.


回答1:


Related to Programmatically uninstall a device in windows device manager

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.




回答2:


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.

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):

This is the wrapper class for the P/Invoke functions

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.




回答3:


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

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

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

Which can also be done in code.




回答4:


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



来源:https://stackoverflow.com/questions/2181525/is-there-an-api-call-to-start-a-scan-for-hardware-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!