Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for hasmouse.cs

(view source code of hasmouse.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management;
  5. using System.Threading;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	internal class HasMouse
  11. 	{
  12. 		static readonly string progver = "4.00";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			Thread.Sleep( 10000 );
  18.  
  19. 			#region Initialize Variables
  20.  
  21. 			string mousetype = string.Empty;
  22. 			string wminamespace = @"root\WMI";
  23. 			string wmiquery = "SELECT * FROM MSMouse_PortInformation";
  24. 			bool ps2 = false;
  25. 			bool serial = false;
  26. 			bool usb = false;
  27. 			bool quiet = false;
  28. 			bool verbose = false;
  29.  
  30. 			SortedList<uint, string> ConnectorType = new SortedList<uint, string>
  31. 			{
  32. 				{ 0, "PS/2" },
  33. 				{ 1, "Serial" },
  34. 				{ 2, "USB" },
  35. 			};
  36.  
  37. 			SortedList<uint, string> HardwareType = new SortedList<uint, string>
  38. 			{
  39. 				{ 0, "Standard Mouse" },
  40. 				{ 1, "Standard Pointer" },
  41. 				{ 2, "Standard Absolute Pointer" },
  42. 				{ 3, "Tablet" },
  43. 				{ 4, "Touch Screen" },
  44. 				{ 5, "Pen" },
  45. 				{ 6, "Track Ball" },
  46. 				{ 256, "Other" }
  47. 			};
  48.  
  49. 			#endregion Initialize Variables
  50.  
  51.  
  52. 			#region Parse Command Line
  53.  
  54. 			foreach ( string arg in args )
  55. 			{
  56. 				if ( arg[0] == '/' && arg.Length > 1 )
  57. 				{
  58. 					switch ( arg.ToUpper( )[1] )
  59. 					{
  60. 						case 'P':
  61. 							if ( ps2 )
  62. 							{
  63. 								return ShowHelp( "Duplicate command line switch /P" );
  64. 							}
  65. 							ps2 = true;
  66. 							if ( wmiquery.Contains( "WHERE" ) )
  67. 							{
  68. 								wmiquery += " OR ConnectorType='0'";
  69. 							}
  70. 							else
  71. 							{
  72. 								wmiquery += " WHERE ConnectorType='0'";
  73. 							}
  74. 							if ( string.IsNullOrWhiteSpace( mousetype ) )
  75. 							{
  76. 								mousetype += " PS/2";
  77. 							}
  78. 							else
  79. 							{
  80. 								mousetype += " or PS/2";
  81. 							}
  82. 							break;
  83. 						case 'Q':
  84. 							if ( quiet )
  85. 							{
  86. 								return ShowHelp( "Duplicate command line switch /Q" );
  87. 							}
  88. 							if ( verbose )
  89. 							{
  90. 								return ShowHelp( "Command line switches /Q and /V are mutually exclusive" );
  91. 							}
  92. 							quiet = true;
  93. 							break;
  94. 						case 'S':
  95. 							if ( serial )
  96. 							{
  97. 								return ShowHelp( "Duplicate command line switch /S" );
  98. 							}
  99. 							serial = true;
  100. 							if ( wmiquery.Contains( "WHERE" ) )
  101. 							{
  102. 								wmiquery += " OR ConnectorType='1'";
  103. 							}
  104. 							else
  105. 							{
  106. 								wmiquery += " WHERE ConnectorType='1'";
  107. 							}
  108. 							if ( string.IsNullOrWhiteSpace( mousetype ) )
  109. 							{
  110. 								mousetype += " serial";
  111. 							}
  112. 							else
  113. 							{
  114. 								mousetype += " or serial";
  115. 							}
  116. 							break;
  117. 						case 'U':
  118. 							if ( usb )
  119. 							{
  120. 								return ShowHelp( "Duplicate command line switch /U" );
  121. 							}
  122. 							usb = true;
  123. 							if ( wmiquery.Contains( "WHERE" ) )
  124. 							{
  125. 								wmiquery += " OR ConnectorType='2'";
  126. 							}
  127. 							else
  128. 							{
  129. 								wmiquery += " WHERE ConnectorType='2'";
  130. 							}
  131. 							if ( string.IsNullOrWhiteSpace( mousetype ) )
  132. 							{
  133. 								mousetype += " USB";
  134. 							}
  135. 							else
  136. 							{
  137. 								mousetype += " or USB";
  138. 							}
  139. 							break;
  140. 						case 'V':
  141. 							if ( verbose )
  142. 							{
  143. 								return ShowHelp( "Duplicate command line switch /V" );
  144. 							}
  145. 							if ( quiet )
  146. 							{
  147. 								return ShowHelp( "Command line switches /Q and /V are mutually exclusive" );
  148. 							}
  149. 							verbose = true;
  150. 							break;
  151. 						default:
  152. 							return ShowHelp( "Invalid command line switch {0}", arg );
  153. 					}
  154. 				}
  155. 				else
  156. 				{
  157. 					return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  158. 				}
  159. 			}
  160.  
  161. 			#endregion Parse Command Line
  162.  
  163.  
  164. 			#region Count Mice
  165.  
  166. 			ManagementObjectSearcher searcher = new ManagementObjectSearcher( wminamespace, wmiquery );
  167. 			ManagementObjectCollection colItems = searcher.Get( );
  168. 			int count = colItems.Count;
  169. 			if ( !quiet )
  170. 			{
  171. 				if ( count == 0 )
  172. 				{
  173. 					Console.ForegroundColor = ConsoleColor.Red;
  174. 					Console.Write( "No" );
  175. 					Console.ResetColor( );
  176. 					Console.WriteLine( "{0} mouse detected", mousetype );
  177. 				}
  178. 				else if ( count == 1 )
  179. 				{
  180. 					Console.ForegroundColor = ConsoleColor.Green;
  181. 					Console.Write( "1" );
  182. 					Console.ResetColor( );
  183. 					Console.Write( "{0} mouse detected", mousetype );
  184. 					if ( verbose )
  185. 					{
  186. 						Console.Write( ":" );
  187. 					}
  188. 					Console.WriteLine( );
  189. 				}
  190. 				else
  191. 				{
  192. 					Console.ForegroundColor = ConsoleColor.Green;
  193. 					Console.Write( count );
  194. 					Console.ResetColor( );
  195. 					Console.Write( "{0} mice detected", mousetype );
  196. 					if ( verbose )
  197. 					{
  198. 						Console.Write( ":" );
  199. 					}
  200. 					Console.WriteLine( );
  201. 				}
  202. 			}
  203.  
  204. 			#endregion Count Mice
  205.  
  206.  
  207. 			#region Show Mice Details
  208.  
  209. 			if ( verbose && count > 0 )
  210. 			{
  211. 				foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
  212. 				{
  213. 					Console.WriteLine( "\tActive        : {0}", queryObj["Active"] );
  214. 					Console.WriteLine( "\tButtons       : {0}", queryObj["Buttons"] );
  215. 					uint connectortype = uint.Parse( queryObj["ConnectorType"].ToString( ) );
  216. 					Console.WriteLine( "\tConnectorType : {0}", ConnectorType[connectortype] );
  217. 					Console.WriteLine( "\tDataQueueSize : {0}", queryObj["DataQueueSize"] );
  218. 					Console.WriteLine( "\tErrorCount    : {0}", queryObj["ErrorCount"] );
  219. 					uint hardwaretype = uint.Parse( queryObj["HardwareType"].ToString( ) );
  220. 					Console.WriteLine( "\tHardwareType  : {0}", HardwareType[hardwaretype] );
  221. 					Console.WriteLine( "\tInstanceName  : {0}", queryObj["InstanceName"] );
  222. 					Console.WriteLine( );
  223. 					queryObj.Dispose();
  224. 				}
  225. 			}
  226.  
  227. 			#endregion Show Mice Details
  228.  
  229.  
  230. 			return count;
  231. 		}
  232.  
  233.  
  234. 		public static int ShowHelp( params string[] errmsg )
  235. 		{
  236. 			#region Error Message
  237.  
  238. 			if ( errmsg.Length > 0 )
  239. 			{
  240. 				List<string> errargs = new List<string>( errmsg );
  241. 				errargs.RemoveAt( 0 );
  242. 				Console.Error.WriteLine( );
  243. 				Console.ForegroundColor = ConsoleColor.Red;
  244. 				Console.Error.Write( "ERROR:\t" );
  245. 				Console.ForegroundColor = ConsoleColor.White;
  246. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  247. 				Console.ResetColor( );
  248. 			}
  249.  
  250. 			#endregion Error Message
  251.  
  252.  
  253. 			#region Help Text
  254.  
  255. 			/*
  256. 			HasMouse.exe,  Version 4.00
  257. 			Check the actual number of mice available at this very moment
  258.  
  259. 			Usage:   HasMouse.exe  [ type ]  [ output ]
  260.  
  261. 			Type:    /P   count PS/2 mice only   (default: all types)
  262. 			         /S   count Serial mice only (default: all types)
  263. 			         /U   count USB mice only    (default: all types)
  264.  
  265. 			Output:  /Q   Quiet mode: no screen output       (default: count only)
  266. 			         /V   Verbose output: display properties (default: count only)
  267.  
  268. 			Notes:   This program requires elevated privileges; use version 3.00
  269. 			         if you cannot guarantee elevated privileges.
  270. 			         Mouse types can be combined, e.g. /P /U for PS/2 and USB mice;
  271. 			         if no type is specified, all types will be listed.
  272. 			         Command line switches may be abbreviated to their first letter,
  273. 			         e.g. /P for /PS2, /U for /USB, etc.
  274. 			         Command line switches /Q and /V are mutually exclusive.
  275. 			         The program's return code equals the number of matching mice
  276. 			         detected, or -1 in case of (command line) errors.
  277.  
  278. 			Written by Rob van der Woude
  279. 			https://www.robvanderwoude.com
  280. 			*/
  281.  
  282. 			#endregion Help Text
  283.  
  284.  
  285. 			#region Display Help Text
  286.  
  287. 			Console.Error.WriteLine( );
  288.  
  289. 			Console.Error.WriteLine( "HasMouse.exe,  Version {0}", progver );
  290.  
  291. 			Console.Error.WriteLine( "Check the actual number of mice available at this very moment" );
  292.  
  293. 			Console.Error.WriteLine( );
  294.  
  295. 			Console.Error.Write( "Usage:   " );
  296. 			Console.ForegroundColor = ConsoleColor.White;
  297. 			Console.Error.WriteLine( "HasMouse.exe  [ type ]  [ output ]" );
  298. 			Console.ResetColor( );
  299.  
  300. 			Console.Error.WriteLine( );
  301.  
  302. 			Console.Error.Write( "Type:    " );
  303. 			Console.ForegroundColor = ConsoleColor.White;
  304. 			Console.Error.Write( "/P" );
  305. 			Console.ResetColor( );
  306. 			Console.Error.Write( "   count " );
  307. 			Console.ForegroundColor = ConsoleColor.White;
  308. 			Console.Error.Write( "P" );
  309. 			Console.ResetColor( );
  310. 			Console.Error.WriteLine( "S/2 mice only   (default: all types)" );
  311.  
  312. 			Console.ForegroundColor = ConsoleColor.White;
  313. 			Console.Write( "         /S" );
  314. 			Console.ResetColor( );
  315. 			Console.Write( "   count " );
  316. 			Console.ForegroundColor = ConsoleColor.White;
  317. 			Console.Write( "S" );
  318. 			Console.ResetColor( );
  319. 			Console.WriteLine( "erial mice only (default: all types)" );
  320.  
  321. 			Console.ForegroundColor = ConsoleColor.White;
  322. 			Console.Write( "         /U" );
  323. 			Console.ResetColor( );
  324. 			Console.Write( "   count " );
  325. 			Console.ForegroundColor = ConsoleColor.White;
  326. 			Console.Write( "U" );
  327. 			Console.ResetColor( );
  328. 			Console.WriteLine( "SB mice only    (default: all types)" );
  329.  
  330. 			Console.Error.WriteLine( );
  331.  
  332. 			Console.Error.Write( "Output:  " );
  333. 			Console.ForegroundColor = ConsoleColor.White;
  334. 			Console.Error.Write( "/Q   Q" );
  335. 			Console.ResetColor( );
  336. 			Console.Error.WriteLine( "uiet mode: no screen output    (default: count only)" );
  337.  
  338. 			Console.ForegroundColor = ConsoleColor.White;
  339. 			Console.Error.Write( "         /V   V" );
  340. 			Console.ResetColor( );
  341. 			Console.Error.WriteLine( "erbose output: list properties (default: count only)" );
  342.  
  343. 			Console.Error.WriteLine( );
  344.  
  345. 			Console.Error.WriteLine( "Notes:   This program requires elevated privileges; use version 3.00" );
  346.  
  347. 			Console.Error.WriteLine( "         if you cannot guarantee elevated privileges." );
  348.  
  349. 			Console.Error.Write( "         Mouse types can be combined, e.g. " );
  350. 			Console.ForegroundColor = ConsoleColor.White;
  351. 			Console.Error.Write( "/P /U" );
  352. 			Console.ResetColor( );
  353. 			Console.Error.WriteLine( " for PS/2 and USB mice;" );
  354.  
  355. 			Console.Error.WriteLine( "         if no type is specified, all types will be listed." );
  356.  
  357. 			Console.Error.Write( "         Command line switches " );
  358. 			Console.ForegroundColor = ConsoleColor.White;
  359. 			Console.Error.Write( "/Q" );
  360. 			Console.ResetColor( );
  361. 			Console.Error.Write( " and " );
  362. 			Console.ForegroundColor = ConsoleColor.White;
  363. 			Console.Error.Write( "/V" );
  364. 			Console.ResetColor( );
  365. 			Console.Error.WriteLine( " are mutually exclusive." );
  366.  
  367. 			Console.Error.WriteLine( "         The program's return code equals the number of matching mice" );
  368.  
  369. 			Console.Error.WriteLine( "         detected, or -1 in case of (command line) errors." );
  370.  
  371. 			Console.Error.WriteLine( );
  372.  
  373. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  374.  
  375. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  376.  
  377. 			#endregion Display Help Text
  378.  
  379.  
  380. 			return -1;
  381. 		}
  382. 	}
  383. }

page last modified: 2024-04-16; loaded in 0.0104 seconds