Skip to content

Instantly share code, notes, and snippets.

@danieleli
Last active May 9, 2024 21:44
Show Gist options
  • Save danieleli/16e7afb18f3f5bf76553bcb1b0897d85 to your computer and use it in GitHub Desktop.
Save danieleli/16e7afb18f3f5bf76553bcb1b0897d85 to your computer and use it in GitHub Desktop.
Registry reader debugger
function Get-ComPortNames {
param (
[string]$VendorID,
[string]$ProductID
)
$pattern = "^VID_$VendorID.PID_$ProductID"
$rx = New-Object System.Text.RegularExpressions.Regex($pattern, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
$comports = @()
$rk1 = [Microsoft.Win32.Registry]::LocalMachine
$rootKey = "SYSTEM\CurrentControlSet\Enum"
"Root: $rootKey" | Out-File output.txt -Append
$rk2 = $rk1.OpenSubKey($rootKey)
if ($rk2 -ne $null) {
foreach ($childKey in $rk2.GetSubKeyNames()) {
"`t$childKey" | Out-File output.txt -Append
$rk3 = $rk2.OpenSubKey($childKey)
if ($rk3 -ne $null) {
foreach ($grandChildKey in $rk3.GetSubKeyNames()) {
"`t`t$grandChildKey" | Out-File output.txt -Append
if ($rx.IsMatch($grandChildKey)) {
$rk4 = $rk3.OpenSubKey($grandChildKey)
if ($rk4 -ne $null) {
foreach ($greatGrandChildKey in $rk4.GetSubKeyNames()) {
"`t`t`t$greatGrandChildKey" | Out-File output.txt -Append
$rk5 = $rk4.OpenSubKey($greatGrandChildKey)
if ($rk5 -ne $null) {
"`t`t`t`tDevice Parameters" | Out-File output.txt -Append
$rk6 = $rk5.OpenSubKey("Device Parameters")
if ($rk6 -ne $null) {
"`t`t`t`t`tPortName" | Out-File output.txt -Append
$portName = $rk6.GetValue("PortName")
if ($portName -ne $null) {
"`t`t`t`t`t`t$portName" | Out-File output.txt -Append
$comports += $portName
}
}
}
}
}
}
}
}
}
}
return $comports
}
$ports = Get-ComPortNames -VendorID "0B00" -ProductID "0084"
foreach ($port in $ports) {
"Port: $port" | Out-File output.txt -Append
}
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace ComPortDebugger
{
internal class Program
{
static void Main(string[] args)
{
var ports = GetComPortNames("0B00", "0084");
foreach (var port in ports)
{
Console.WriteLine("Port: " + port);
}
Console.Read();
}
private static List<string> GetComPortNames(string VID, string PID)
{
string pattern = string.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
List<string> comports = new List<string>();
RegistryKey rk1 = Registry.LocalMachine;
var rootKey = "SYSTEM\\CurrentControlSet\\Enum";
Console.WriteLine("Root: " + rootKey);
RegistryKey rk2 = rk1.OpenSubKey(rootKey);
foreach (string childKey in rk2.GetSubKeyNames())
{ Console.WriteLine("\t" + childKey);
RegistryKey rk3 = rk2.OpenSubKey(childKey);
foreach (string grandChildKey in rk3.GetSubKeyNames())
{
Console.WriteLine("\t\t" + grandChildKey);
if (_rx.Match(grandChildKey).Success)
{
RegistryKey rk4 = rk3.OpenSubKey(grandChildKey);
foreach (string greatGrandChildKey in rk4.GetSubKeyNames())
{
Console.WriteLine("\t\t\t" + greatGrandChildKey);
RegistryKey rk5 = rk4.OpenSubKey(greatGrandChildKey);
Console.WriteLine("\t\t\t\tDevice Parameters");
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
Console.WriteLine("\t\t\t\t\tPortName");
var portName = rk6.GetValue("PortName");
Console.WriteLine("\t\t\t\t\t\t" + portName);
comports.Add((string)portName);
}
}
}
}
return comports;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment