(view source code of hasmouse.cs as plain text)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Threading;
namespace RobvanderWoude
{
internal class HasMouse
{
static readonly string progver = "4.00";
static int Main( string[] args )
{
Thread.Sleep( 10000 );
#region Initialize Variables
string mousetype = string.Empty;
string wminamespace = @"root\WMI";
string wmiquery = "SELECT * FROM MSMouse_PortInformation";
bool ps2 = false;
bool serial = false;
bool usb = false;
bool quiet = false;
bool verbose = false;
SortedList<uint, string> ConnectorType = new SortedList<uint, string>
{
{ 0, "PS/2" },
{ 1, "Serial" },
{ 2, "USB" },
};
SortedList<uint, string> HardwareType = new SortedList<uint, string>
{
{ 0, "Standard Mouse" },
{ 1, "Standard Pointer" },
{ 2, "Standard Absolute Pointer" },
{ 3, "Tablet" },
{ 4, "Touch Screen" },
{ 5, "Pen" },
{ 6, "Track Ball" },
{ 256, "Other" }
};
#endregion Initialize Variables
#region Parse Command Line
foreach ( string arg in args )
{
if ( arg[0] == '/' && arg.Length > 1 )
{
switch ( arg.ToUpper( )[1] )
{
case 'P':
if ( ps2 )
{
return ShowHelp( "Duplicate command line switch /P" );
}
ps2 = true;
if ( wmiquery.Contains( "WHERE" ) )
{
wmiquery += " OR ConnectorType='0'";
}
else
{
wmiquery += " WHERE ConnectorType='0'";
}
if ( string.IsNullOrWhiteSpace( mousetype ) )
{
mousetype += " PS/2";
}
else
{
mousetype += " or PS/2";
}
break;
case 'Q':
if ( quiet )
{
return ShowHelp( "Duplicate command line switch /Q" );
}
if ( verbose )
{
return ShowHelp( "Command line switches /Q and /V are mutually exclusive" );
}
quiet = true;
break;
case 'S':
if ( serial )
{
return ShowHelp( "Duplicate command line switch /S" );
}
serial = true;
if ( wmiquery.Contains( "WHERE" ) )
{
wmiquery += " OR ConnectorType='1'";
}
else
{
wmiquery += " WHERE ConnectorType='1'";
}
if ( string.IsNullOrWhiteSpace( mousetype ) )
{
mousetype += " serial";
}
else
{
mousetype += " or serial";
}
break;
case 'U':
if ( usb )
{
return ShowHelp( "Duplicate command line switch /U" );
}
usb = true;
if ( wmiquery.Contains( "WHERE" ) )
{
wmiquery += " OR ConnectorType='2'";
}
else
{
wmiquery += " WHERE ConnectorType='2'";
}
if ( string.IsNullOrWhiteSpace( mousetype ) )
{
mousetype += " USB";
}
else
{
mousetype += " or USB";
}
break;
case 'V':
if ( verbose )
{
return ShowHelp( "Duplicate command line switch /V" );
}
if ( quiet )
{
return ShowHelp( "Command line switches /Q and /V are mutually exclusive" );
}
verbose = true;
break;
default:
return ShowHelp( "Invalid command line switch {0}", arg );
}
}
else
{
return ShowHelp( "Invalid command line argument \"{0}\"", arg );
}
}
#endregion Parse Command Line
#region Count Mice
ManagementObjectSearcher searcher = new ManagementObjectSearcher( wminamespace, wmiquery );
ManagementObjectCollection colItems = searcher.Get( );
int count = colItems.Count;
if ( !quiet )
{
if ( count == 0 )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write( "No" );
Console.ResetColor( );
Console.WriteLine( "{0} mouse detected", mousetype );
}
else if ( count == 1 )
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write( "1" );
Console.ResetColor( );
Console.Write( "{0} mouse detected", mousetype );
if ( verbose )
{
Console.Write( ":" );
}
Console.WriteLine( );
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write( count );
Console.ResetColor( );
Console.Write( "{0} mice detected", mousetype );
if ( verbose )
{
Console.Write( ":" );
}
Console.WriteLine( );
}
}
#endregion Count Mice
#region Show Mice Details
if ( verbose && count > 0 )
{
foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
{
Console.WriteLine( "\tActive : {0}", queryObj["Active"] );
Console.WriteLine( "\tButtons : {0}", queryObj["Buttons"] );
uint connectortype = uint.Parse( queryObj["ConnectorType"].ToString( ) );
Console.WriteLine( "\tConnectorType : {0}", ConnectorType[connectortype] );
Console.WriteLine( "\tDataQueueSize : {0}", queryObj["DataQueueSize"] );
Console.WriteLine( "\tErrorCount : {0}", queryObj["ErrorCount"] );
uint hardwaretype = uint.Parse( queryObj["HardwareType"].ToString( ) );
Console.WriteLine( "\tHardwareType : {0}", HardwareType[hardwaretype] );
Console.WriteLine( "\tInstanceName : {0}", queryObj["InstanceName"] );
Console.WriteLine( );
queryObj.Dispose();
}
}
#endregion Show Mice Details
return count;
}
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
/*
HasMouse.exe, Version 4.00
Check the actual number of mice available at this very moment
Usage: HasMouse.exe [ type ] [ output ]
Type: /P count PS/2 mice only (default: all types)
/S count Serial mice only (default: all types)
/U count USB mice only (default: all types)
Output: /Q Quiet mode: no screen output (default: count only)
/V Verbose output: display properties (default: count only)
Notes: This program requires elevated privileges; use version 3.00
if you cannot guarantee elevated privileges.
Mouse types can be combined, e.g. /P /U for PS/2 and USB mice;
if no type is specified, all types will be listed.
Command line switches may be abbreviated to their first letter,
e.g. /P for /PS2, /U for /USB, etc.
Command line switches /Q and /V are mutually exclusive.
The program's return code equals the number of matching mice
detected, or -1 in case of (command line) errors.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "HasMouse.exe, Version {0}", progver );
Console.Error.WriteLine( "Check the actual number of mice available at this very moment" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "HasMouse.exe [ type ] [ output ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Type: " );
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 only (default: all types)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Write( " /S" );
Console.ResetColor( );
Console.Write( " count " );
Console.ForegroundColor = ConsoleColor.White;
Console.Write( "S" );
Console.ResetColor( );
Console.WriteLine( "erial mice only (default: all types)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Write( " /U" );
Console.ResetColor( );
Console.Write( " count " );
Console.ForegroundColor = ConsoleColor.White;
Console.Write( "U" );
Console.ResetColor( );
Console.WriteLine( "SB mice only (default: all types)" );
Console.Error.WriteLine( );
Console.Error.Write( "Output: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/Q Q" );
Console.ResetColor( );
Console.Error.WriteLine( "uiet mode: no screen output (default: count only)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /V V" );
Console.ResetColor( );
Console.Error.WriteLine( "erbose output: list properties (default: count only)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: This program requires elevated privileges; use version 3.00" );
Console.Error.WriteLine( " if you cannot guarantee elevated privileges." );
Console.Error.Write( " Mouse types can be combined, e.g. " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/P /U" );
Console.ResetColor( );
Console.Error.WriteLine( " for PS/2 and USB mice;" );
Console.Error.WriteLine( " if no type is specified, all types will be 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.WriteLine( " The program's return code equals the number of matching mice" );
Console.Error.WriteLine( " detected, or -1 in case of (command line) errors." );
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;
}
}
}
page last modified: 2024-04-16; loaded in 0.0104 seconds