Skip to content

Commit

Permalink
SerialPortStream: Completely remove WMI searches
Browse files Browse the repository at this point in the history
A test on my T61p shows that inbuilt modems should also work with the CfgMgr, as it also exposes the port name.

Issue: DOTNET-698, #136
  • Loading branch information
jcurl committed Jan 5, 2023
1 parent d9d8203 commit c167e01
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
30 changes: 7 additions & 23 deletions code/Native/WinNativeSerial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ namespace RJCP.IO.Ports.Native
using Trace;
using Windows;

#if !NETSTANDARD1_5
using System.Management;
using System.Xml.Linq;
using System.Linq;
#else
#if NETSTANDARD1_5
using System.Reflection;
#endif

Expand Down Expand Up @@ -135,18 +131,6 @@ public PortDescription[] GetPortDescriptions()
// works since Windows XP.
QueryDevices(list);

#if !NETSTANDARD1_5
// Look for any modems that are attached to COM ports that aren't listed above
using (ManagementObjectSearcher q = new ManagementObjectSearcher("select * from Win32_POTSModem")) {
foreach (ManagementObject mObj in q.Get().Cast<ManagementObject>()) {
string k = mObj["AttachedTo"].ToString();
if (list.ContainsKey(k)) {
list[k].Description = mObj["Name"].ToString();
}
}
}
#endif

// Get the array and return it
int i = 0;
PortDescription[] ports = new PortDescription[list.Count];
Expand Down Expand Up @@ -183,7 +167,7 @@ private static string GetDeviceProperty(CfgMgr32.SafeDevInst devInst, CfgMgr32.C
{
CfgMgr32.CONFIGRET ret = CfgMgr32.CM_Get_DevNode_Registry_Property(
devInst, deviceProperty, out int _, out string buffer);
if (ret != CfgMgr32.CONFIGRET.CR_SUCCESS)
if (ret != CfgMgr32.CONFIGRET.CR_SUCCESS)
return string.Empty;

return buffer;
Expand All @@ -194,7 +178,7 @@ private static RegistryKey GetDeviceKey(CfgMgr32.SafeDevInst devInst)
CfgMgr32.CONFIGRET ret = CfgMgr32.CM_Open_DevNode_Key(
devInst, Kernel32.REGSAM.KEY_READ, 0, CfgMgr32.RegDisposition.OpenExisting,
out SafeRegistryHandle key, 0);
if (ret != CfgMgr32.CONFIGRET.CR_SUCCESS)
if (ret != CfgMgr32.CONFIGRET.CR_SUCCESS)
return null;
if (key.IsInvalid || key.IsClosed)
return null;
Expand Down Expand Up @@ -1002,7 +986,7 @@ private void WinIOError()
throw new IOException(string.Format("Unknown error 0x{0}: {1}", e.ToString("X"), PortName), e);
}

#region Event Handling
#region Event Handling
private void RegisterEvents()
{
m_CommOverlappedIo.CommEvent += CommOverlappedIo_CommEvent;
Expand Down Expand Up @@ -1101,9 +1085,9 @@ protected virtual void OnPinChanged(object sender, SerialPinChangedEventArgs arg
handler(sender, args);
}
}
#endregion
#endregion

#region IDisposable Support
#region IDisposable Support
private bool m_IsDisposed;

/// <summary>
Expand Down Expand Up @@ -1133,6 +1117,6 @@ protected virtual void Dispose(bool disposing)
// with an IntPtr however.
m_IsDisposed = true;
}
#endregion
#endregion
}
}
1 change: 0 additions & 1 deletion code/Native/Windows/CfgMgr32.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class CfgMgr32
{
Expand Down

0 comments on commit c167e01

Please sign in to comment.