Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for quickeditmode.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using Microsoft.Win32;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	internal class QuickEditMode
  10. 	{
  11. 		static readonly string progver = "1.01";
  12.  
  13. 		public const uint STD_INPUT_HANDLE = unchecked((uint)-10);
  14. 		public const uint STD_OUTPUT_HANDLE = unchecked((uint)-11);
  15. 		public const uint STD_ERROR_HANDLE = unchecked((uint)-12);
  16.  
  17. 		public const uint ENABLE_MOUSE_INPUT = 0x0010;
  18. 		public const uint ENABLE_QUICK_EDIT_MODE = 0x0040;
  19. 		public const uint ENABLE_EXTENDED_FLAGS = 0x0080;
  20. 		public const uint ENABLE_ECHO_INPUT = 0x0004;
  21. 		public const uint ENABLE_WINDOW_INPUT = 0x0008;
  22. 		public const uint ENABLE_INSERT_MODE = 0x0020;
  23. 		public const uint ENABLE_LINE_INPUT = 0x0002;
  24. 		public const uint ENABLE_PROCESSED_INPUT = 0x0001;
  25. 		public const uint ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
  26.  
  27.  
  28. 		static int Main( string[] args )
  29. 		{
  30. 			#region Command Line Parsing
  31.  
  32. 			bool consoledefault = false;
  33. 			bool currentconsole = false;
  34. 			string quickedit = "get";
  35. 			foreach ( string arg in args )
  36. 			{
  37. 				if ( arg == "0" )
  38. 				{
  39. 					if ( quickedit != "get" )
  40. 					{
  41. 						return ShowHelp( "Duplicate new value on command line" );
  42. 					}
  43. 					quickedit = "off";
  44. 				}
  45. 				else if ( arg == "1" )
  46. 				{
  47. 					if ( quickedit != "get" )
  48. 					{
  49. 						return ShowHelp( "Duplicate new value on command line" );
  50. 					}
  51. 					quickedit = "on";
  52. 				}
  53. 				else if ( arg.ToUpper( ) == "/C" )
  54. 				{
  55. 					if ( currentconsole )
  56. 					{
  57. 						return ShowHelp( "Duplicate command line switch /C" );
  58. 					}
  59. 					currentconsole = true;
  60. 				}
  61. 				else if ( arg.ToUpper( ) == "/D" )
  62. 				{
  63. 					if ( consoledefault )
  64. 					{
  65. 						return ShowHelp( "Duplicate command line switch /D" );
  66. 					}
  67. 					consoledefault = true;
  68. 				}
  69. 				else if ( arg == "/?" )
  70. 				{
  71. 					return ShowHelp( );
  72. 				}
  73. 				else
  74. 				{
  75. 					return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  76. 				}
  77. 			}
  78. 			if ( !consoledefault )
  79. 			{
  80. 				currentconsole = true;
  81. 			}
  82. 			if ( quickedit == "get" && consoledefault && currentconsole )
  83. 			{
  84. 				return ShowHelp( "Use either /C or /D, not both, when reading the current status" );
  85. 			}
  86.  
  87. 			#endregion Command Line Parsing
  88.  
  89.  
  90. 			#region Get or Set Current Console
  91.  
  92. 			int currentstatus = 0;
  93. 			if ( currentconsole )
  94. 			{
  95. 				currentstatus = CurrentConsole( quickedit );
  96. 				if ( currentstatus == -1 )
  97. 				{
  98. 					if ( quickedit == "get" )
  99. 					{
  100. 						return ShowHelp( "Failed to get current console's Quick-Edit mode" );
  101. 					}
  102. 					else
  103. 					{
  104. 						return ShowHelp( "Failed to set current console's Quick-Edit mode" );
  105. 					}
  106. 				}
  107. 			}
  108.  
  109.  
  110. 			#endregion Get or Set Current Console
  111.  
  112.  
  113. 			#region Get or Set Console Default
  114.  
  115. 			int defaultstatus = 0;
  116. 			if ( consoledefault )
  117. 			{
  118. 				defaultstatus = ConsoleDefault( quickedit );
  119. 				if ( defaultstatus == -1 )
  120. 				{
  121. 					if ( quickedit == "get" )
  122. 					{
  123. 						return ShowHelp( "Failed to get default Quick-Edit mode" );
  124. 					}
  125. 					else
  126. 					{
  127. 						return ShowHelp( "Failed to set default Quick-Edit mode" );
  128. 					}
  129. 				}
  130. 			}
  131.  
  132. 			#endregion Get or Set Console Default
  133.  
  134.  
  135. 			return currentstatus + defaultstatus;
  136. 		}
  137.  
  138.  
  139. 		static int ConsoleDefault( string quickeditcommand )
  140. 		{
  141. 			if ( quickeditcommand == "get" )
  142. 			{
  143. 				return (int)Registry.GetValue( "HKEY_CURRENT_USER\\Console", "QuickEdit", -1 );
  144. 			}
  145. 			else
  146. 			{
  147. 				int newvalue = 0; // disable
  148. 				if ( quickeditcommand == "on" )
  149. 				{
  150. 					newvalue = 1; // enable
  151. 				}
  152. 				Registry.SetValue( "HKEY_CURRENT_USER\\Console", "QuickEdit", newvalue, RegistryValueKind.DWord );
  153. 				if ( (int)Registry.GetValue( "HKEY_CURRENT_USER\\Console", "QuickEdit", -1 ) == newvalue )
  154. 				{
  155. 					return 0; // success
  156. 				}
  157. 			}
  158. 			return -1; // failure
  159. 		}
  160.  
  161.  
  162. 		static int CurrentConsole( string quickeditcommand )
  163. 		{
  164. 			IntPtr handle = GetStdHandle( STD_INPUT_HANDLE );
  165. 			if ( handle != IntPtr.Zero )
  166. 			{
  167. 				uint mode = 0;
  168. 				if ( GetConsoleMode( handle, ref mode ) )
  169. 				{
  170. 					if ( quickeditcommand == "get" )
  171. 					{
  172. 						if ( ( mode & ENABLE_QUICK_EDIT_MODE ) == 0 )
  173. 						{
  174. 							return 0; // not set
  175. 						}
  176. 						else
  177. 						{
  178. 							return 1; // set
  179. 						}
  180. 					}
  181. 					else
  182. 					{
  183. 						mode |= ENABLE_EXTENDED_FLAGS; // enable, required to change Quick-Edit mode
  184. 						if ( quickeditcommand == "on" )
  185. 						{
  186. 							mode |= ENABLE_QUICK_EDIT_MODE; // enable
  187. 						}
  188. 						else
  189. 						{
  190. 							mode &= ~ENABLE_QUICK_EDIT_MODE; // disable
  191. 						}
  192. 						if ( SetConsoleMode( handle, mode ) )
  193. 						{
  194. 							return 0; // success
  195. 						}
  196. 					}
  197. 				}
  198. 			}
  199. 			return -1; // failure
  200. 		}
  201.  
  202. 		#region Error handling
  203.  
  204. 		static int ShowHelp( params string[] errmsg )
  205. 		{
  206. 			#region Error Message
  207.  
  208. 			if ( errmsg.Length > 0 )
  209. 			{
  210. 				List<string> errargs = new List<string>( errmsg );
  211. 				errargs.RemoveAt( 0 );
  212. 				Console.Error.WriteLine( );
  213. 				Console.ForegroundColor = ConsoleColor.Red;
  214. 				Console.Error.Write( "ERROR:\t" );
  215. 				Console.ForegroundColor = ConsoleColor.White;
  216. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  217. 				Console.ResetColor( );
  218. 			}
  219.  
  220. 			#endregion Error Message
  221.  
  222.  
  223. 			#region Help Text
  224.  
  225. 			/*
  226. 			QuickEditMode.exe,  Version 1.01
  227. 			Get or set Quick-Edit mode for consoles
  228.  
  229. 			Usage:    QuickEditMode.exe  [ /C | /D ]
  230.  
  231. 			   or:    QuickEditMode.exe  [ 0 | 1 ]  [ /C ]  [ /D ]
  232.  
  233. 			Where:    0    disables Quick-Edit mode
  234. 			          1    enables Quick-Edit mode
  235. 			          /C   for current console
  236. 			          /D   default for all consoles
  237.  
  238. 			Notes:    Without command line argument 0 or 1 (get mode), return code 0
  239. 			          indicates Quick-Edit mode was not set, return code 1 indicates
  240. 			          it was set.
  241. 			          With command line argument 0 or 1 (set mode), return code 0
  242. 			          indicates Quick-Edit mode was changed successfully.
  243. 			          Return code -1 always indicates an error occurred.
  244. 			          Without command line switches, only the setting for the current
  245. 			          console is read or changed.
  246. 			          With command line switch /D the default setting for all consoles
  247. 			          is read or changed.
  248. 			          Use both /C and /D if you want to change the default as well as the
  249. 			          current console; when reading the current status do not use both.
  250. 			          Besides this help, the program does not generate any screen output.
  251.  
  252. 			Written by Rob van der Woude
  253. 			https://www.robvanderwoude.com
  254. 			*/
  255.  
  256. 			#endregion Help Text
  257.  
  258.  
  259. 			#region Display Help Text
  260.  
  261. 			Console.Error.WriteLine( );
  262.  
  263. 			Console.Error.WriteLine( "QuickEditMode.exe,  Version {0}", progver );
  264.  
  265. 			Console.Error.WriteLine( "Get or set the current console's Quick-Edit mode" );
  266.  
  267. 			Console.Error.WriteLine( );
  268.  
  269. 			Console.Error.Write( "Usage:    " );
  270. 			Console.ForegroundColor = ConsoleColor.White;
  271. 			Console.Error.WriteLine( "QuickEditMode.exe  [ /C | /D ]" );
  272. 			Console.ResetColor( );
  273.  
  274. 			Console.Error.WriteLine( );
  275.  
  276. 			Console.Error.Write( "   or:    " );
  277. 			Console.ForegroundColor = ConsoleColor.White;
  278. 			Console.Error.WriteLine( "QuickEditMode.exe  [ 0 | 1 ]  [ /C ]  [ /D ]" );
  279. 			Console.ResetColor( );
  280.  
  281. 			Console.Error.WriteLine( );
  282.  
  283. 			Console.Error.Write( "Where:    " );
  284. 			Console.ForegroundColor = ConsoleColor.White;
  285. 			Console.Error.Write( "0" );
  286. 			Console.ResetColor( );
  287. 			Console.Error.WriteLine( "    disables Quick-Edit mode" );
  288.  
  289. 			Console.ForegroundColor = ConsoleColor.White;
  290. 			Console.Error.Write( "          1" );
  291. 			Console.ResetColor( );
  292. 			Console.Error.WriteLine( "    enables Quick-Edit mode" );
  293.  
  294.  
  295. 			Console.ForegroundColor = ConsoleColor.White;
  296. 			Console.Error.Write( "          /C" );
  297. 			Console.ResetColor( );
  298. 			Console.Error.WriteLine( "   for current console" );
  299.  
  300.  
  301. 			Console.ForegroundColor = ConsoleColor.White;
  302. 			Console.Error.Write( "          /D" );
  303. 			Console.ResetColor( );
  304. 			Console.Error.WriteLine( "   default for all consoles" );
  305.  
  306.  
  307. 			Console.Error.WriteLine( );
  308.  
  309. 			Console.Error.WriteLine( "Notes:    Without command line argument 0 or 1 (get mode), return code 0" );
  310.  
  311. 			Console.Error.WriteLine( "          indicates Quick-Edit mode was not set, return code 1 indicates" );
  312.  
  313. 			Console.Error.WriteLine( "          it was set." );
  314.  
  315. 			Console.Error.WriteLine( "          With command line argument 0 or 1 (set mode), return code 0" );
  316.  
  317. 			Console.Error.WriteLine( "          indicates Quick-Edit mode was changed successfully." );
  318.  
  319. 			Console.Error.WriteLine( "          Return code -1 always indicates an error occurred." );
  320.  
  321. 			Console.Error.WriteLine( "          Without command line switches, only the setting for the current" );
  322.  
  323. 			Console.Error.WriteLine( "          console is read or changed." );
  324.  
  325. 			Console.Error.WriteLine( "          With command line switch /D the default setting for all consoles" );
  326.  
  327. 			Console.Error.WriteLine( "          is read or changed." );
  328.  
  329. 			Console.Error.Write( "          Use both /C and /D if you want to " );
  330. 			Console.ForegroundColor = ConsoleColor.Yellow;
  331. 			Console.Error.Write( "change" );
  332. 			Console.ResetColor( );
  333. 			Console.Error.WriteLine( " the default as well as the" );
  334.  
  335. 			Console.Error.Write( "          current console; when " );
  336. 			Console.ForegroundColor = ConsoleColor.Yellow;
  337. 			Console.Error.Write( "reading" );
  338. 			Console.ResetColor( );
  339. 			Console.Error.Write( " the current status do " );
  340. 			Console.ForegroundColor = ConsoleColor.Yellow;
  341. 			Console.Error.Write( "not" );
  342. 			Console.ResetColor( );
  343. 			Console.Error.WriteLine( " use both." );
  344.  
  345. 			Console.Error.WriteLine( "          Besides this help, the program does not generate any screen output." );
  346.  
  347. 			Console.Error.WriteLine( );
  348.  
  349. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  350.  
  351. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  352.  
  353. 			#endregion Display Help Text
  354.  
  355.  
  356. 			return -1;
  357. 		}
  358.  
  359. 		#endregion Error handling
  360.  
  361.  
  362. 		#region DLL Imports
  363.  
  364. 		[DllImport( "kernel32.dll" )]
  365. 		public static extern IntPtr GetStdHandle( uint nStdHandle );
  366.  
  367.  
  368. 		[DllImportAttribute( "kernel32.dll" )]
  369. 		public static extern bool GetConsoleMode( IntPtr hConsoleInput, ref uint lpMode );
  370.  
  371.  
  372. 		[DllImportAttribute( "kernel32.dll" )]
  373. 		public static extern bool SetConsoleMode( IntPtr hConsoleInput, uint dwMode );
  374.  
  375.  
  376. 		#endregion DLL Imports
  377. 	}
  378. }

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