(view source code of hashardware.cs as plain text)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
namespace RobvanderWoude
{
internal class HasHardware
{
static readonly string progver = "1.05";
#region Global Variables
static bool analog = false;
static bool continueonerrors = false;
static bool digital = false;
static bool errors = false;
static bool hideerrors = false;
static bool includekeyboards = false;
static bool includemice = false;
static bool includemonitors = false;
static bool ps2 = false;
static bool quiet = false;
static bool serial = false;
static bool usb = false;
static bool verbose = false;
static bool yesno = false;
static int keyboardcount = 0;
static int monitorcount = 0;
static int mousecount = 0;
static string keyboardquery;
static string keyboardtype;
static string mousequery;
static string mousetype;
static List<SortedList<string, string>> monitors = new List<SortedList<string, string>>( );
static SortedList<string, string> monitor;
// Monitor/mouse/keyboard availability (root\CIMV2)
static readonly Dictionary<int, string> Availability = new Dictionary<int, string>
{
{ 1, "Other" },
{ 2, "Unknown" },
{ 3, "Running/Full Power" },
{ 4, "Warning" },
{ 5, "In Test" },
{ 6, "Not Applicable" },
{ 7, "Power Off" },
{ 8, "Off Line" },
{ 9, "Off Duty" },
{ 10, "Degraded" },
{ 11, "Not Installed" },
{ 12, "Install Error" },
{ 13, "Power Save - Unknown" },
{ 14, "Power Save - Low Power Mode" },
{ 15, "Power Save - Standby" },
{ 16, "Power Cycle" },
{ 17, "Power Save - Warning" },
{ 18, "Paused" },
{ 19, "Not Ready" },
{ 20, "Not Configured" },
{ 21, "Quiesced" }
};
// Keyboard and mouse connector type (root\WMI)
static readonly Dictionary<int, string> ConnectorType = new Dictionary<int, string>
{
{ 0, "PS/2" },
{ 1, "Serial" },
{ 2, "USB" },
};
// Mouse device interface (root\CIMV2)
static readonly Dictionary<int, string> DeviceInterface = new Dictionary<int, string>
{
{ 1,"Other" },
{ 2, "Unknown" },
{ 3, "Serial" },
{ 4, "PS/2" },
{ 5, "Infrared" },
{ 6, "HP-HIL" },
{ 7, "Bus mouse" },
{ 8, "ADB (Apple Desktop Bus)" },
{ 160, "Bus mouse DB-9" },
{ 161, "Bus mouse micro-DIN" },
{ 162, "USB" }
};
// Mouse hardware type (root\WMI)
static readonly Dictionary<int, string> HardwareType = new Dictionary<int, string>
{
{ 0, "Standard Mouse" },
{ 1, "Standard Pointer" },
{ 2, "Standard Absolute Pointer" },
{ 3, "Tablet" },
{ 4, "Touch Screen" },
{ 5, "Pen" },
{ 6, "Track Ball" },
{ 256, "Other" }
};
#endregion Global Variables
static int Main( string[] args )
{
#region Initialize Variables
keyboardtype = string.Empty;
keyboardquery = "SELECT * FROM MsKeyboard_PortInformation";
string monitortype = string.Empty;
string monitorquery = "SELECT * FROM Win32_DesktopMonitor WHERE NOT MonitorType='Default Monitor' AND MonitorManufacturer IS NOT NULL"; // common PNPDeviceID
string monitordetails = "SELECT * FROM WmiMonitorBasicDisplayParams"; // common InstanceName
mousetype = string.Empty;
mousequery = "SELECT * FROM MSMouse_PortInformation";
#endregion Initialize Variables
#region Parse Command Line
foreach ( string arg in args )
{
switch ( arg.ToUpper( ) )
{
case "/?":
return ShowHelp( );
case "/A":
CheckArg( ref analog, arg );
break;
case "/C":
CheckArg( ref continueonerrors, arg );
break;
case "/D":
CheckArg( ref digital, arg );
break;
case "/H":
CheckArg( ref hideerrors, arg );
break;
case "/I":
CheckArg( ref includemonitors, arg );
break;
case "/K":
CheckArg( ref includekeyboards, arg );
break;
case "/M":
CheckArg( ref includemice, arg );
break;
case "/P":
CheckArg( ref ps2, arg );
break;
case "/Q":
CheckArg( ref quiet, arg );
break;
case "/S":
CheckArg( ref serial, arg );
break;
case "/U":
CheckArg( ref usb, arg );
break;
case "/V":
CheckArg( ref verbose, arg );
break;
case "/Y":
CheckArg( ref yesno, arg );
break;
default:
return ShowHelp( "Invalid command line argument \"{0}\"", arg );
}
}
continueonerrors = continueonerrors || hideerrors;
if ( quiet && verbose )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "Command line switches /Q and /V are mutually exclusive" );
Console.ResetColor( );
errors = true;
}
if ( errors )
{
return ShowHelp( );
}
if ( !includekeyboards && !includemice && !includemonitors )
{
includekeyboards = true;
includemice = true;
includemonitors = true;
}
if ( includekeyboards || includemice )
{
if ( !ps2 && !serial && !usb )
{
ps2 = true;
serial = true;
usb = true;
}
}
#endregion Parse Command Line
if ( includekeyboards || includemice )
{
ConnectorTypeQuery( );
}
if ( includekeyboards )
{
#region Count Keyboards
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\WMI", keyboardquery );
ManagementObjectCollection colItems = searcher.Get( );
keyboardcount = colItems.Count;
if ( !quiet )
{
ShowCount( keyboardcount, "keyboard", keyboardtype, "keyboards" );
}
#region Show Keyboards Details
if ( verbose && keyboardcount > 0 )
{
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
int connectortype = int.Parse( queryObj["ConnectorType"].ToString( ) );
Console.WriteLine( "\tConnector Type : {0}", ConnectorType[connectortype] );
Console.WriteLine( "\tFunction Keys : {0}", queryObj["FunctionKeys"] );
Console.WriteLine( "\tIndicators : {0}", queryObj["Indicators"] );
Console.WriteLine( );
queryObj.Dispose( );
}
}
#endregion Show Keyboards Details
}
catch ( ManagementException e )
{
if ( hideerrors )
{
errors = true;
// Plan B: use root\CIMV2 if access to root\WMI is denied
Win32_Keyboard( );
}
else if ( continueonerrors )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "\nERROR:\t" );
Console.ResetColor( );
Console.Error.WriteLine( "{0} (namespace: root/WMI; class: MsKeyboard_PortInformation)", e.Message );
Console.WriteLine( " \tTrying alternative query (namespace: root/CIMV2; class: Win32_Keyboard)\n" );
errors = true;
// Plan B: use root\CIMV2 if access to root\WMI is denied
Win32_Keyboard( );
}
else
{
return ShowHelp( "{0} (namespace: root/WMI; class: MsKeyboard_PortInformation)", e.Message );
}
}
#endregion Count Keyboards
}
if ( includemonitors )
{
#region Prepare Monitor Query
if ( analog != digital )
{
if ( analog )
{
monitordetails += " WHERE VideoInputType='0'";
monitortype += " analog";
}
else
{
monitordetails += " WHERE VideoInputType='1'";
monitortype += " digital";
}
}
#endregion Prepare Monitor Query
#region Count Monitors
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\WMI", monitordetails );
ManagementObjectCollection colItems = searcher.Get( );
monitorcount = colItems.Count;
if ( !quiet )
{
ShowCount( monitorcount, "monitor", monitortype, "monitors" );
}
#endregion Count Monitors
#region Prepare Monitor Details Queries
searcher = new ManagementObjectSearcher( @"root\CIMV2", monitorquery );
colItems = searcher.Get( );
if ( verbose )
{
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
monitor = new SortedList<string, string>( );
int availability = int.Parse( queryObj["Availability"].ToString( ) );
monitor.Add( "Availability", Availability[availability] );
monitor.Add( "Manufacturer", queryObj["MonitorManufacturer"].ToString( ) );
monitor.Add( "Description", queryObj["MonitorType"].ToString( ) );
string deviceid = queryObj["PNPDeviceID"].ToString( ).Replace( "&", "&" );
monitor.Add( "PnP Device ID", deviceid );
monitor.Add( "Screen Height (px)", queryObj["ScreenHeight"].ToString( ) );
monitor.Add( "Screen Width (px)", queryObj["ScreenWidth"].ToString( ) );
monitor.Add( "Status", queryObj["Status"].ToString( ) );
queryObj.Dispose( );
WMIMonitorID( deviceid );
string inputtype = monitor["Video Input (Monitor)"].ToLower( );
if ( ( analog == digital ) || ( inputtype == "analog" && analog ) || ( inputtype == "digital" && digital ) )
{
monitors.Add( monitor );
}
//monitor.Clear( );
}
// determine left column width
int columnwidth = 0;
foreach ( var mon in monitors )
{
foreach ( string key in mon.Keys )
{
columnwidth = Math.Max( columnwidth, key.Length );
}
}
// list a selection of all properties and values
foreach ( var mon in monitors )
{
foreach ( string key in mon.Keys )
{
Console.WriteLine( "\t{0,-" + columnwidth + "} : {1}", key, mon[key] );
}
Console.WriteLine( );
}
}
#endregion Prepare Monitor Details Queries
}
if ( includemice )
{
#region Count Mice
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\WMI", mousequery );
ManagementObjectCollection colItems = searcher.Get( );
mousecount = colItems.Count;
if ( !quiet )
{
ShowCount( mousecount, "mouse", mousetype, "mice" );
}
#region Show Mice Details
if ( verbose && mousecount > 0 )
{
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
Console.WriteLine( "\tButtons : {0}", queryObj["Buttons"] );
int connectortype = int.Parse( queryObj["ConnectorType"].ToString( ) );
Console.WriteLine( "\tConnector Type : {0}", ConnectorType[connectortype] );
int hardwaretype = int.Parse( queryObj["HardwareType"].ToString( ) );
Console.WriteLine( "\tHardware Type : {0}", HardwareType[hardwaretype] );
Console.WriteLine( );
queryObj.Dispose( );
}
}
#endregion Show Mice Details
}
catch ( ManagementException e )
{
if ( hideerrors )
{
errors = true;
Win32_PointingDevice( );
}
else if ( continueonerrors )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "\nERROR:\t" );
Console.ResetColor( );
Console.Error.WriteLine( "{0} (namespace: root/WMI; class: MSMouse_PortInformation)", e.Message );
Console.WriteLine( " \tTrying alternative query (namespace: root/CIMV2; class: Win32_PointingDevice)\n" );
errors = true;
Win32_PointingDevice( );
}
else
{
return ShowHelp( "{0} (namespace: root/WMI; class: MSMouse_PortInformation)", e.Message );
}
}
#endregion Count Mice
}
// calculate return code
if ( errors && !hideerrors )
{
return -1;
}
else if ( yesno )
{
int rc = 0;
if ( keyboardcount > 0 )
{
rc += 100;
}
if ( monitorcount > 0 )
{
rc += 10;
}
if ( mousecount > 0 )
{
rc += 1;
}
return rc;
}
else
{
return keyboardcount + monitorcount + mousecount;
}
}
static string Chain( UInt16[] chararray )
{
string charstring = null;
foreach ( UInt16 chr in chararray )
{
if ( chr == 0 )
{
return charstring;
}
else
{
charstring += ( (char)chr ).ToString( );
}
}
return charstring;
}
public static void CheckArg( ref bool variable, string argument )
{
if ( variable )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "Duplicate command line switch {0}", argument.ToUpper( ) );
Console.ResetColor( );
errors = true;
}
else
{
variable = true;
}
}
public static void ConnectorTypeQuery( )
{
string query = string.Empty;
string type = string.Empty;
if ( ps2 && serial && usb )
{
ps2 = false;
serial = false;
usb = false;
}
if ( ps2 || serial || usb )
{
if ( ps2 )
{
if ( query.Contains( "WHERE" ) )
{
query += " OR ConnectorType='0'";
}
else
{
query += " WHERE ConnectorType='0'";
}
if ( string.IsNullOrWhiteSpace( type ) )
{
type += " PS/2";
}
else
{
type += " or PS/2";
}
}
if ( serial )
{
if ( query.Contains( "WHERE" ) )
{
query += " OR ConnectorType='1'";
}
else
{
query += " WHERE ConnectorType='1'";
}
if ( string.IsNullOrWhiteSpace( type ) )
{
type += " serial";
}
else
{
type += " or serial";
}
}
if ( usb )
{
if ( query.Contains( "WHERE" ) )
{
query += " OR ConnectorType='2'";
}
else
{
query += " WHERE ConnectorType='2'";
}
if ( string.IsNullOrWhiteSpace( type ) )
{
type += " USB";
}
else
{
type += " or USB";
}
}
}
keyboardquery += query;
keyboardtype += type;
mousequery += query;
mousetype += type;
}
public static void ShowCount( int count, string deviceclass, string devicetype, string plural )
{
if ( !quiet )
{
if ( count == 0 )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write( "No" );
Console.ResetColor( );
Console.WriteLine( "{0} {1} detected", devicetype, deviceclass );
if ( verbose )
{
Console.WriteLine( );
}
}
else if ( count == 1 )
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write( "1" );
Console.ResetColor( );
Console.Write( "{0} {1} detected", devicetype, deviceclass );
if ( verbose )
{
Console.Write( ":" );
}
Console.WriteLine( );
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write( count );
Console.ResetColor( );
Console.Write( "{0} {1} detected", devicetype, plural );
if ( verbose )
{
Console.Write( ":" );
}
Console.WriteLine( );
}
}
}
public static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Help Text
/*
HasHardware.exe, Version 1.03
Check the actual number of connected input and video devices available
Usage: HasHardware.exe [ hardware [ type ] ] [ output ]
Hardware: /I check dIsplay (monitors) only (default: keyboard, monitor, mouse)
/K check Keyboards only (default: keyboard, monitor, mouse)
/M check Mice only (default: keyboard, monitor, mouse)
Type: /A count Analog (VGA) monitors only (default: all types)
/D count Digital (DVI/HDMI/DP) monitors only (default: all types)
/P count PS/2 mice and/or keyboards only (default: all types)
/S count Serial mice and/or keyboards only (default: all types)
/U count USB mice and/or keyboards only (default: all types)
Output: /C Continue on (access denied) errors (default: terminate on error)
/H Hide (access denied) errors (default: terminate on error)
/Q Quiet mode: no screen output (default: # per device class)
/V Verbose output: display properties (default: # per device class)
/Y YesNo mode: available or not? (see Notes)
Notes: This program requires elevated privileges.
Hardware and types can be combined, e.g. /K /M /P /U for PS/2 and
USB keyboards and mice; if no type is specified, all types will be
listed.
Command line switches /Q and /V are mutually exclusive.
In case of access denied errors, switches /C and /H will use
alternative WMI queries to count keyboards and/or mice; with /C an
error message will be displayed, followed by the "plan B count", and
return code will be -1; with /H the "plan B count" will be shown
without error message, and return code will not be affected; the
alternative WMI queries may return less details, e.g. there is no way
to determine the keyboard connector type without elevated privileges.
Return code equals the sum of detected matching devices, or with /Y
100 x K + 10 x V + M (K = keyboard count, V = monitors, M = mice),
or -1 in case of command line or access denied errors.
Examples: HasHardware
Show keyboards, monitors and mice (all types)
Return code equals total number of matching devices
HasHardware /Y
Show keyboards, monitors and mice
Return code 111 if at least 1 keyboard and at least 1 monitor and
at least 1 mouse were detected
HasHardware /M /K /U /Y
Show USB keyboards and mice, ignore monitors
Return code 101 if at least 1 USB keyboard and at least 1 USB mouse
were detected
HasHardware /U /D
Show USB keyboards and mice and digital moitors
Return code equals total number of matching devices
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "HasHardware.exe, Version {0}", progver );
Console.Error.WriteLine( "Check the actual number of connected input and video devices available" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "HasHardware.exe [ hardware [ type ] ] [ output ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Hardware: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/I" );
Console.ResetColor( );
Console.Error.Write( " check d" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "I" );
Console.ResetColor( );
Console.Error.WriteLine( "splay (monitors) only (default: keyboard, monitor, mouse)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /K" );
Console.ResetColor( );
Console.Error.Write( " check " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "K" );
Console.ResetColor( );
Console.Error.WriteLine( "eyboards only (default: keyboard, monitor, mouse)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /M" );
Console.ResetColor( );
Console.Error.Write( " check " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "M" );
Console.ResetColor( );
Console.Error.WriteLine( "ice only (default: keyboard, monitor, mouse)" );
Console.Error.WriteLine( );
Console.Error.Write( "Type: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/A" );
Console.ResetColor( );
Console.Error.Write( " count " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "A" );
Console.ResetColor( );
Console.Error.WriteLine( "nalog (VGA) monitors only (default: all types)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /D" );
Console.ResetColor( );
Console.Error.Write( " count " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "D" );
Console.ResetColor( );
Console.Error.WriteLine( "igital (DVI/HDMI/DP) monitors only (default: all types)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /P" );
Console.ResetColor( );
Console.Error.Write( " count " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "P" );
Console.ResetColor( );
Console.Error.WriteLine( "S/2 mice and/or keyboards only (default: all types)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /S" );
Console.ResetColor( );
Console.Error.Write( " count " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "S" );
Console.ResetColor( );
Console.Error.WriteLine( "erial mice and/or keyboards only (default: all types)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /U" );
Console.ResetColor( );
Console.Error.Write( " count " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "U" );
Console.ResetColor( );
Console.Error.WriteLine( "SB mice and/or keyboards only (default: all types)" );
Console.Error.WriteLine( );
Console.Error.Write( "Output: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/C C" );
Console.ResetColor( );
Console.Error.WriteLine( "ontinue on (access denied) errors (default: terminate on error)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /H H" );
Console.ResetColor( );
Console.Error.WriteLine( "ide (access denied) errors (default: terminate on error)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /Q Q" );
Console.ResetColor( );
Console.Error.WriteLine( "uiet mode: no screen output (default: # per device class)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /V V" );
Console.ResetColor( );
Console.Error.WriteLine( "erbose output: display properties (default: # per device class)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /Y Y" );
Console.ResetColor( );
Console.Error.WriteLine( "esNo mode: available or not? (see Notes)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: This program requires elevated privileges." );
Console.Error.Write( " Hardware and types can be combined, e.g. " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/K /M /P /U" );
Console.ResetColor( );
Console.Error.WriteLine( " for PS/2 and" );
Console.Error.WriteLine( " USB keyboards and mice; if no type is specified, all types will be" );
Console.Error.WriteLine( " listed." );
Console.Error.Write( " Command line switches " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/Q" );
Console.ResetColor( );
Console.Error.Write( " and " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/V" );
Console.ResetColor( );
Console.Error.WriteLine( " are mutually exclusive." );
Console.Error.Write( " In case of access denied errors, switches " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/C" );
Console.ResetColor( );
Console.Error.Write( " and " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/H" );
Console.ResetColor( );
Console.Error.WriteLine( " will use" );
Console.Error.Write( " alternative WMI queries to count keyboards and/or mice; with " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/C" );
Console.ResetColor( );
Console.Error.WriteLine( " an" );
Console.Error.WriteLine( " error message will be displayed, followed by the \"plan B count\", and" );
Console.Error.Write( " return code will equal -1; with " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/H" );
Console.ResetColor( );
Console.Error.WriteLine( " the \"plan B count\" will be shown" );
Console.Error.WriteLine( " without error message, and return code will not be affected; the" );
Console.Error.WriteLine( " alternative WMI queries may return less details, e.g. there is no way" );
Console.Error.WriteLine( " to determine the keyboard connector type without elevated privileges." );
Console.Error.Write( " Return code equals the sum of detected matching devices, or with " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "/Y" );
Console.Error.Write( " 100 x K + 10 x V + M" );
Console.ResetColor( );
Console.Error.WriteLine( " (K = keyboards, V = monitors, M = mice)," );
Console.Error.WriteLine( " or -1 in case of command line or access denied errors." );
Console.Error.WriteLine( );
Console.Error.Write( "Examples: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "HasHardware" );
Console.ResetColor( );
Console.Error.WriteLine( " Show keyboards, monitors and mice (all types)" );
Console.Error.WriteLine( " Return code equals total number of matching devices" );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( " HasHardware /Y" );
Console.ResetColor( );
Console.Error.WriteLine( " Show keyboards, monitors and mice" );
Console.Error.WriteLine( " Return code 111 if at least 1 keyboard and at least 1 monitor and" );
Console.Error.WriteLine( " at least 1 mouse were detected" );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( " HasHardware /M /K /U /Y" );
Console.ResetColor( );
Console.Error.WriteLine( " Show USB keyboards and mice, ignore monitors" );
Console.Error.WriteLine( " Return code 101 if at least 1 USB keyboard and at least 1 USB mouse" );
Console.Error.WriteLine( " were detected" );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( " HasHardware /U /D" );
Console.ResetColor( );
Console.Error.WriteLine( " Show USB keyboards and mice and digital moitors" );
Console.Error.WriteLine( " Return code equals total number of matching devices" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return -1;
}
static string VideoOutputTechnology( string id )
{
string result = null;
Dictionary<long, string> VideoOutputTechnology = new Dictionary<long, string>
{
{ -2, "unknown connector(s)" },
{ -1, "other connector(s)" },
{ 0, "HD15 (VGA) connector" },
{ 1, "S-video connector" },
{ 2, "composite video connectors" },
{ 3, "component video connectors" },
{ 4, "Digital Video Interface (DVI) connector" },
{ 5, "High-Definition Multimedia Interface (HDMI) connector" },
{ 6, "Low Voltage Differential Swing (LVDS) or Mobile Industry Processor Interface (MIPI) Digital Serial Interface (DSI) connector" },
{ 8, "D-Jpn connector" },
{ 9, "SDI connector" },
{ 10, "external display port" },
{ 11, "embedded display port" },
{ 12, "external Unified Display Interface (UDI)" },
{ 13, "embedded Unified Display Interface (UDI)" },
{ 14, "dongle cable that supports SDTV" },
{ 15, "wireless Miracast" },
{ 16, "wired indirect" },
{ 0x80000000, "internal connection (e.g. laptop)" }
};
// This query gets the monitor connector type ON THE VIDEO CARD SIDE
string query = string.Format( "SELECT * FROM WmiMonitorConnectionParams WHERE InstanceName='{0}'", id ).Replace( @"\", @"\\" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\WMI", query );
ManagementObjectCollection colItems = searcher.Get( );
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
long videooutputtechnology = long.Parse( queryObj["VideoOutputTechnology"].ToString( ) );
result = VideoOutputTechnology[videooutputtechnology];
queryObj.Dispose( );
}
return result;
}
static void Win32_Keyboard( )
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\CIMV2", "SELECT * FROM Win32_Keyboard" );
ManagementObjectCollection colItems = searcher.Get( );
keyboardcount = colItems.Count;
ShowCount( keyboardcount, "keyboard", string.Empty, "keyboards" ); // unable to determine connector type without elevated privileges
if ( verbose && keyboardcount > 0 )
{
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
int availability = 2; // default: Unknown
if ( !( queryObj["Availability"] is null ) )
{
availability = int.Parse( queryObj["Availability"].ToString( ) );
}
Console.WriteLine( "\tAvailability : {0}", Availability[availability] );
Console.WriteLine( "\tCaption : {0}", queryObj["Caption"] );
Console.WriteLine( "\tDescription : {0}", queryObj["Description"] );
Console.WriteLine( "\tLayout : {0}", queryObj["Layout"] );
Console.WriteLine( "\tName : {0}", queryObj["Name"] );
Console.WriteLine( "\tNumber of Function Keys : {0}", queryObj["NumberOfFunctionKeys"] );
Console.WriteLine( "\tPnP Device ID : {0}", queryObj["PNPDeviceID"] );
Console.WriteLine( "\tStatus : {0}", queryObj["Status"] );
Console.WriteLine( );
queryObj.Dispose( );
}
Console.WriteLine( );
}
}
static void Win32_PointingDevice( )
{
string query = "SELECT * FROM Win32_PointingDevice";
if ( !( ps2 && serial && usb ) || ps2 || serial || usb )
{
if ( ps2 )
{
if ( query.Contains( "WHERE" ) )
{
query += " OR DeviceInterface='4'";
}
else
{
query += " WHERE DeviceInterface='4'";
}
}
if ( serial )
{
if ( query.Contains( "WHERE" ) )
{
query += " OR DeviceInterface='3'";
}
else
{
query += " WHERE DeviceInterface='3'";
}
}
if ( usb )
{
if ( query.Contains( "WHERE" ) )
{
query += " OR DeviceInterface='162'";
}
else
{
query += " WHERE DeviceInterface='162'";
}
}
}
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\CIMV2", query );
ManagementObjectCollection colItems = searcher.Get( );
mousecount = colItems.Count;
ShowCount( mousecount, "mouse", mousetype, "mice" );
if ( verbose && mousecount > 0 )
{
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
int availability = 2; // default: Unknown
if ( !( queryObj["Availability"] is null ) )
{
availability = int.Parse( queryObj["Availability"].ToString( ) );
}
Console.WriteLine( "\tAvailability : {0}", Availability[availability] );
int deviceinterface = int.Parse( queryObj["DeviceInterface"].ToString( ) );
Console.WriteLine( "\tDevice Interface : {0}", DeviceInterface[deviceinterface] );
Console.WriteLine( "\tHardware Type : {0}", queryObj["HardwareType"] );
Console.WriteLine( "\tManufacturer : {0}", queryObj["Manufacturer"] );
Console.WriteLine( "\tNumber of Buttons : {0}", queryObj["NumberOfButtons"] );
Console.WriteLine( "\tPnP Device ID : {0}", queryObj["PNPDeviceID"] );
Console.WriteLine( "\tPointing Type : {0}", queryObj["PointingType"] );
Console.WriteLine( "\tStatus : {0}", queryObj["Status"] );
queryObj.Dispose( );
}
Console.WriteLine( );
}
}
static void WmiMonitorBasicDisplayParams( string id )
{
SortedList<int, string> VideoInputType = new SortedList<int, string>
{
{ 0, "Analog" },
{ 1, "Digital" }
};
int videoinputtype;
string query = string.Format( "SELECT * FROM WmiMonitorBasicDisplayParams WHERE InstanceName='{0}'", id ).Replace( @"\", @"\\" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\WMI", query );
ManagementObjectCollection colItems = searcher.Get( );
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
monitor.Add( "Screen Width (cm)", queryObj["MaxHorizontalImageSize"].ToString( ) );
monitor.Add( "Screen Height (cm)", queryObj["MaxVerticalImageSize"].ToString( ) );
videoinputtype = int.Parse( queryObj["VideoInputType"].ToString( ) );
monitor.Add( "Video Input (Monitor)", VideoInputType[videoinputtype] );
}
}
static void WMIMonitorID( string id )
{
string query = string.Format( "SELECT * FROM WmiMonitorID WHERE InstanceName LIKE '{0}%'", id ).Replace( @"\", @"\\" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\WMI", query );
ManagementObjectCollection colItems = searcher.Get( );
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
monitor.Add( "Active", queryObj["Active"].ToString( ) );
string instancename = queryObj["InstanceName"].ToString( );
monitor.Add( "Product Code", Chain( (UInt16[])queryObj["ProductCodeID"] ) );
monitor.Add( "Serial Number", Chain( (UInt16[])queryObj["SerialNumberID"] ) );
monitor.Add( "Monitor Type", Chain( (UInt16[])queryObj["UserFriendlyName"] ) );
monitor.Add( "Video Output (Display Adapter)", VideoOutputTechnology( instancename ) );
monitor.Add( "Week of Manufacture", queryObj["WeekOfManufacture"].ToString( ) );
monitor.Add( "Year of Manufacture", queryObj["YearOfManufacture"].ToString( ) );
queryObj.Dispose( );
WmiMonitorBasicDisplayParams( instancename );
}
}
}
}
page last modified: 2024-04-16; loaded in 0.0195 seconds