WMI does not work after upgrading to Windows 10

I used the code below for a console application in Visual Studio on Windows 8 to return the description and device identifier of connected serial devices. I used a modified version of this in the application that I create to automatically detect the Arduino COM port. It does not return anything anymore since I did a new installation with Windows 10. I have a USB serial AVR programmer that still appears using this code. I checked the registry and Arduino is listed in SERIALCOMM, Arduino is displayed as β€œUSB Serial Port (COM6)” under β€œPorts (COM and LPT)” in Device Manager, and I can program Arduino using Arduino software. I have no idea why it is no longer working.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Management; using System.IO.Ports; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main() { ManagementScope connectionScope = new ManagementScope(); SelectQuery serialQuery = new SelectQuery("SELECT * FROM Win32_SerialPort"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(connectionScope, serialQuery); try { foreach (ManagementObject item in searcher.Get()) { string desc = item["Description"].ToString(); string deviceId = item["DeviceID"].ToString(); Console.WriteLine(desc); Console.WriteLine(deviceId); } } catch (ManagementException e) { Console.WriteLine(e.Message); } Console.ReadKey(); } } } 

It may also be important that when I tried to find a solution, I found the following implementation to find the port names using MSSerial_PortName and received an Access Denied error.

 using System; using System.Management; namespace ConsoleApplication1 { class Program { static void Main() { try { ManagementObjectSearcher MOSearcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName"); foreach (ManagementObject MOject in MOSearcher.Get()) { Console.WriteLine(MOject["PortName"]); } } catch (ManagementException me) { Console.WriteLine("An error occurred while querying for WMI data: " + me.Message); } Console.ReadKey(); } } } 
+4
source share
3 answers

Well, I think I see the problem now (I will add a new answer, but we will not replace the old one if someone finds useful information).

Win32_SerialPort only detects hardware serial ports (for example, RS232). Win32_PnPEntity identifies plug-and-play devices, including hardware and virtual serial ports (for example, the COM port created by the FTDI driver).

Using Win32_PnPEntity to identify the driver requires a little extra work. The following code identifies all COM ports in the system and creates a list of the specified ports. From this list you can determine the appropriate COM port number for creating the SerialPort object.

 // Class to contain the port info. public class PortInfo { public string Name; public string Description; } // Method to prepare the WMI query connection options. public static ConnectionOptions PrepareOptions ( ) { ConnectionOptions options = new ConnectionOptions ( ); options . Impersonation = ImpersonationLevel . Impersonate; options . Authentication = AuthenticationLevel . Default; options . EnablePrivileges = true; return options; } // Method to prepare WMI query management scope. public static ManagementScope PrepareScope ( string machineName , ConnectionOptions options , string path ) { ManagementScope scope = new ManagementScope ( ); scope . Path = new ManagementPath ( @"\\" + machineName + path ); scope . Options = options; scope . Connect ( ); return scope; } // Method to retrieve the list of all COM ports. public static List<PortInfo> FindComPorts ( ) { List<PortInfo> portList = new List<PortInfo> ( ); ConnectionOptions options = PrepareOptions ( ); ManagementScope scope = PrepareScope ( Environment . MachineName , options , @"\root\CIMV2" ); // Prepare the query and searcher objects. ObjectQuery objectQuery = new ObjectQuery ( "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0" ); ManagementObjectSearcher portSearcher = new ManagementObjectSearcher ( scope , objectQuery ); using ( portSearcher ) { string caption = null; // Invoke the searcher and search through each management object for a COM port. foreach ( ManagementObject currentObject in portSearcher . Get ( ) ) { if ( currentObject != null ) { object currentObjectCaption = currentObject [ "Caption" ]; if ( currentObjectCaption != null ) { caption = currentObjectCaption . ToString ( ); if ( caption . Contains ( "(COM" ) ) { PortInfo portInfo = new PortInfo ( ); portInfo . Name = caption . Substring ( caption . LastIndexOf ( "(COM" ) ) . Replace ( "(" , string . Empty ) . Replace ( ")" , string . Empty ); portInfo . Description = caption; portList . Add ( portInfo ); } } } } } return portList; } 
+4
source

A little smaller solution than Juderb

  public static List<string> FindComPorts() { using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0")) { return searcher.Get().OfType<ManagementBaseObject>() .Select(port => Regex.Match(port["Caption"].ToString(), @"\((COM\d*)\)")) .Where(match => match.Groups.Count >= 2) .Select(match => match.Groups[1].Value).ToList(); } } 

This is tested on win7 + win10. Btw, you can add additional search criteria to the regular expression if you want (or just add a new Where clause)

+2
source

Your Windows 8 user profile probably includes full administrator rights, while your Windows 10 user profile is a standard user. Since your Windows 10 user profile is a standard user, you need to set some permissions.

  • Launch Computer Management Utility.

  • Go to Computer Management > Services and Applications > WMI Control .

  • Go to Actions > WMI Control > More Actions > Properties to open the WMI Control Properties window.

  • On the Security tab, select Root , then click Security .

  • In the Security for Root dialog box, select your username or group to which you belong. Select Allow for Permissions > Execute Methods . Click OK to close the dialog box.

  • Repeat the previous step for CIMV2 .

  • Try again with these new permissions.

Computer management utility WMI management properties for the root WMI Management Properties for CIMV2 Security Settings for Root

+1
source

Source: https://habr.com/ru/post/1011780/


All Articles