1

In device manager of windows7 in com port's branch I choose menu "properties" one of port. In tab of "details" I chose property "parent" and see the string:

enter image description here

How I can obtain this string from vb .net or another language in visual studio in cmd also will be good?

I tried to use win32_ clases: pnpentity, serialPort etc but this no solved to my problem even output of Get-WMIObject Win32_SerialPort in PS had not property "parent".

 Dim objService = GetObject("winmgmts://./root/cimv2")

        For Each objPort In objService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE ClassGuid='{4d36e978-e325-11ce-bfc1-08002be10318}'")

            Console.WriteLine(objPort.Caption & vbCrLf)

            Console.Write(objPort.DeviceID & vbCrLf)
            Console.ReadLine()

        Next

Except Device ID I try Caption and all syntax that available in List. Do you have any idea, please?

ardila
  • 1,277
  • 1
  • 13
  • 24
bdrum
  • 31
  • 5
  • 3
    which language are you wrigint in C# or Vb? Also what have you tried so far? – Simon Price Oct 28 '16 at 08:05
  • See if [this answer](http://stackoverflow.com/a/19826972/579895) helps you – Pikoh Oct 28 '16 at 08:16
  • I use Vb, but it does not matter, because I sure that in my case solution on c# will be similar. Did you read my text? I wrote that i use Win32_SerialPort and etc for a trying to obtaind parent of device. See the Code in main message. – bdrum Oct 28 '16 at 08:45
  • Pikoh, thank you! I will try to use this. – bdrum Oct 28 '16 at 08:51

1 Answers1

0

My solution was the following:

  1. Get all active ports with their PnpDeviceIds:

    private static List<PortInfo> GetActivePorts()
    {
        var ports = new List<PortInfo>();
    
        using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort"))
        using (var collection = searcher.Get())
        {
            foreach (var device in collection)
            {
                var portInfo = new PortInfo
                {
                    Port = (string)device.GetPropertyValue("DeviceID"),
                    PnPDeviceId = (string)device.GetPropertyValue("PNPDeviceID")
                };
                if (!string.IsNullOrEmpty(portInfo.Port) && !string.IsNullOrEmpty(portInfo.PnPDeviceId))
                {
                    ports.Add(portInfo);
                }
            }
        }
    
        return ports;
    }
    

where PortInfo is

    private class PortInfo
    {
        public string Port { get; set; }
        public string PnPDeviceId { get; set; }
        public string ParentDeviceId { get; set; }
    }
  1. fill ParentDeviceIds :

    private static async void FillParentIds(IReadOnlyCollection<PortInfo> ports)
    {
        var propertiesToQuery = new List<string> {
            "System.Devices.DeviceInstanceId",
            "System.Devices.Parent"
        };
    
        var aqs = string.Join(" OR ", ports.Select(p => $"System.Devices.DeviceInstanceId:={p.PnPDeviceId}"));
        var pnpDevices = await PnpObject.FindAllAsync(PnpObjectType.Device, propertiesToQuery, aqs);
        foreach (var pnpDevice in pnpDevices)
        {
            var port = ports.FirstOrDefault(p => string.Compare(p.PnPDeviceId, pnpDevice.Id, StringComparison.InvariantCultureIgnoreCase) == 0);
            if (port != null && pnpDevice.Properties.TryGetValue("System.Devices.Parent", out var parentId))
            {
                port.ParentDeviceId = parentId?.ToString();
            }
        }
    }
    

ParentDeviceId will be that string you are looking for.

Mihass
  • 1
  • 1
  • Mihass, many thanks for the response. I'll try it as soon as possible and share result with you. – bdrum Aug 23 '19 at 11:18
  • where is Windows.Devices.Enumeration.pnp.dll , Is there any alternative solution to create PnpObject? – Milind Morey Sep 13 '19 at 07:52
  • you could find it in Windows.Foundation.UniversalApiContract.winmd which is a part of Windows 10 SDK (https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk) – Mihass Sep 13 '19 at 12:12