Powered by GeSHi

Source code for getmypid.cs

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

  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	class GetMyPID
  7. 	{
  8. 		static int Main( string[] args )
  9. 		{
  10. 			#region Command Line Parsing
  11.  
  12. 			bool doCheck = false;
  13. 			bool verbose = false;
  14. 			Int64 check = -1;
  15.  
  16. 			// Check command line arguments
  17. 			foreach ( string arg in args )
  18. 			{
  19. 				switch ( arg.ToLower( ) )
  20. 				{
  21. 					case "/v":
  22. 					case "/verbose":
  23. 					case "-v":
  24. 					case "--verbose":
  25. 						if ( verbose )
  26. 						{
  27. 							return WriteError( "Duplicate command line switch /Verbose" );
  28. 						}
  29. 						verbose = true;
  30. 						break;
  31. 					case "?":
  32. 					case "/?":
  33. 					case "-?":
  34. 					case "/h":
  35. 					case "-h":
  36. 					case "/help":
  37. 					case "--help":
  38. 						// Display help text without error message
  39. 						return WriteError( string.Empty );
  40. 					default:
  41. 						if ( doCheck )
  42. 						{
  43. 							return WriteError( "Invalid or duplicate command line argument " + arg.ToUpper( ) );
  44. 						}
  45. 						if ( !Int64.TryParse( arg, out check ) )
  46. 						{
  47. 							return WriteError( "Invalid command line argument " + arg.ToUpper( ) );
  48. 						}
  49. 						if ( check < 1 )
  50. 						{
  51. 							return WriteError( "Invalid process ID " + arg );
  52. 						}
  53. 						doCheck = true;
  54. 						break;
  55. 				}
  56. 			}
  57. 			if ( args.Length > 2 )
  58. 			{
  59. 				return WriteError( "Invalid command line argument(s)" );
  60. 			}
  61.  
  62. 			#endregion Command Line Parsing
  63.  
  64. 			try
  65. 			{
  66. 				// Use Jared Barneck's ParentProcess class to retrieve the requested parent process info
  67. 				int pid = ParentProcess.ParentProcess.ProcessId;
  68. 				if ( verbose )
  69. 				{
  70. 					Console.WriteLine( "Parent name  : {0}", ParentProcess.ParentProcess.ProcessName );
  71. 					Console.WriteLine( "Parent path  : {0}", ParentProcess.ParentProcess.FullPath );
  72. 					Console.WriteLine( "Parent PID   : {0}", pid );
  73. 					if ( doCheck )
  74. 					{
  75. 						Console.WriteLine( "Expected PID : {0}", check );
  76. 						Console.WriteLine( "Match        : {0}", ( pid == check ? "Yes" : "No" ) );
  77. 					}
  78. 				}
  79. 				else
  80. 				{
  81. 					Console.WriteLine( pid.ToString( ) );
  82. 				}
  83. 				if ( doCheck )
  84. 				{
  85. 					return ( pid == check ? 0 : 2 );
  86. 				}
  87. 				else
  88. 				{
  89. 					return pid;
  90. 				}
  91. 			}
  92. 			catch ( Exception e )
  93. 			{
  94. 				return WriteError( e );
  95. 			}
  96. 		}
  97.  
  98. 		#region Error Handling
  99.  
  100. 		// Code to display help and optional error message, by Bas van der Woude
  101. 		public static int WriteError( Exception e )
  102. 		{
  103. 			return WriteError( e == null ? null : e.Message );
  104. 		}
  105.  
  106. 		public static int WriteError( string errorMessage )
  107. 		{
  108. 			string exeName = Process.GetCurrentProcess( ).ProcessName;
  109.  
  110. 			/*
  111. 			GetMyPID,  Version 1.00
  112. 			Return or check the parent's (caller's) Process ID
  113.  
  114. 			Usage:    GETMYPID  [ pid ]  [ /V ]
  115.  
  116. 			Where:    pid   is the expected PID to check the actual PID against
  117. 			          /V    display verbose information on the parent process
  118.  
  119. 			Notes:    With an expected PID specified, this program returns 'errorlevel'
  120. 			          0 if the actual PID matches the expected value, or 2 if not;
  121. 			          otherwise, it returns an 'errorlevel' equal to the PID.
  122. 			          So to get a batch file's PID, use the following commands:
  123.  
  124. 			              GETMYPID.EXE
  125. 			              SET PID=%ErrorLevel%
  126.  
  127. 			Credits:  ParentProcess class by Jared Barneck www.rhyous.com/2010/04/30
  128. 			          /how-to-get-the-parent-process-that-launched-a-c-application/
  129.  
  130.  
  131. 			Written by Rob van der Woude
  132. 			http://www.robvanderwoude.com
  133. 			*/
  134.  
  135. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  136. 			{
  137. 				Console.Error.WriteLine( );
  138. 				Console.ForegroundColor = ConsoleColor.Red;
  139. 				Console.Error.Write( "ERROR: " );
  140. 				Console.ForegroundColor = ConsoleColor.White;
  141. 				Console.Error.WriteLine( errorMessage );
  142. 				Console.ResetColor( );
  143. 			}
  144.  
  145. 			Console.Error.WriteLine( );
  146. 			Console.Error.WriteLine( "GetMyPID,  Version 1.00" );
  147. 			Console.Error.WriteLine( "Return or check the parent's (caller's) Process ID" );
  148. 			Console.Error.WriteLine( );
  149. 			Console.Error.Write( "Usage:    " );
  150. 			Console.ForegroundColor = ConsoleColor.White;
  151. 			Console.Error.WriteLine( "{0}  [ pid ]  [ /v ]", exeName.ToUpper( ) );
  152. 			Console.ResetColor( );
  153. 			Console.Error.WriteLine( );
  154. 			Console.Error.Write( "Where:    " );
  155. 			Console.ForegroundColor = ConsoleColor.White;
  156. 			Console.Error.Write( "pid" );
  157. 			Console.ResetColor( );
  158. 			Console.Error.WriteLine( "   is the expected PID to check the actual PID against" );
  159. 			Console.ForegroundColor = ConsoleColor.White;
  160. 			Console.Error.Write( "          /V" );
  161. 			Console.ResetColor( );
  162. 			Console.Error.WriteLine( "    display verbose information on the parent process" );
  163. 			Console.Error.WriteLine( );
  164. 			Console.Error.WriteLine( "Notes:    With an expected PID specified, this program returns 'errorlevel'" );
  165. 			Console.Error.WriteLine( "          0 if the actual PID matches the expected value, or 2 if not;" );
  166. 			Console.Error.WriteLine( "          otherwise, it returns an 'errorlevel' equal to the PID." );
  167. 			Console.Error.WriteLine( "          So to get a batch file's PID, use the following commands:" );
  168. 			Console.Error.WriteLine( );
  169. 			Console.ForegroundColor = ConsoleColor.White;
  170. 			Console.Error.WriteLine( "              GETMYPID.EXE" );
  171. 			Console.Error.WriteLine( "              SET PID=%ErrorLevel%" );
  172. 			Console.ResetColor( );
  173. 			Console.Error.WriteLine( );
  174. 			Console.Error.Write( "Credits:  ParentProcess class by Jared Barneck " );
  175. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  176. 			Console.Error.WriteLine( "www.rhyous.com/2010/04/30" );
  177. 			Console.Error.WriteLine( "          /how-to-get-the-parent-process-that-launched-a-c-application/" );
  178. 			Console.ResetColor( );
  179. 			Console.Error.WriteLine( );
  180. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  181. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  182. 			return 1;
  183. 		}
  184.  
  185. 		#endregion Error Handling
  186. 	}
  187. }
  188.