(view source code of getmypid.cs as plain text)
using System;
using System.Diagnostics;
namespace RobvanderWoude{ class GetMyPID {static int Main( string[] args )
{ #region Command Line Parsingbool doCheck = false;
bool verbose = false;
Int64 check = -1;
// Check command line argumentsforeach ( string arg in args )
{switch ( arg.ToLower( ) )
{case "/v":
case "/verbose":
case "-v":
case "--verbose":
if ( verbose )
{return WriteError( "Duplicate command line switch /Verbose" );
}verbose = true;
break;
case "?":
case "/?":
case "-?":
case "/h":
case "-h":
case "/help":
case "--help":
// Display help text without error messagereturn WriteError( string.Empty );
default:
if ( doCheck )
{return WriteError( "Invalid or duplicate command line argument " + arg.ToUpper( ) );
}if ( !Int64.TryParse( arg, out check ) )
{return WriteError( "Invalid command line argument " + arg.ToUpper( ) );
}if ( check < 1 )
{return WriteError( "Invalid process ID " + arg );
}doCheck = true;
break;
} }if ( args.Length > 2 )
{return WriteError( "Invalid command line argument(s)" );
} #endregion Command Line Parsing try { // Use Jared Barneck's ParentProcess class to retrieve the requested parent process infoint pid = ParentProcess.ParentProcess.ProcessId;
if ( verbose )
{Console.WriteLine( "Parent name : {0}", ParentProcess.ParentProcess.ProcessName );
Console.WriteLine( "Parent path : {0}", ParentProcess.ParentProcess.FullPath );
Console.WriteLine( "Parent PID : {0}", pid );
if ( doCheck )
{Console.WriteLine( "Expected PID : {0}", check );
Console.WriteLine( "Match : {0}", ( pid == check ? "Yes" : "No" ) );
} } else {Console.WriteLine( pid.ToString( ) );
}if ( doCheck )
{return ( pid == check ? 0 : 2 );
} else {return pid;
} }catch ( Exception e )
{return WriteError( e );
} } #region Error Handling // Code to display help and optional error message, by Bas van der Woudepublic static int WriteError( Exception e )
{return WriteError( e == null ? null : e.Message );
}public static int WriteError( string errorMessage )
{string exeName = Process.GetCurrentProcess( ).ProcessName;
/* GetMyPID, Version 1.00 Return or check the parent's (caller's) Process ID Usage: GETMYPID [ pid ] [ /V ] Where: pid is the expected PID to check the actual PID against /V display verbose information on the parent process Notes: With an expected PID specified, this program returns 'errorlevel' 0 if the actual PID matches the expected value, or 2 if not; otherwise, it returns an 'errorlevel' equal to the PID. So to get a batch file's PID, use the following commands: GETMYPID.EXE SET PID=%ErrorLevel% Credits: ParentProcess class by Jared Barneck www.rhyous.com/2010/04/30 /how-to-get-the-parent-process-that-launched-a-c-application/ Written by Rob van der Woude http://www.robvanderwoude.com */if ( string.IsNullOrEmpty( errorMessage ) == false )
{Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errorMessage );
Console.ResetColor( );
}Console.Error.WriteLine( );
Console.Error.WriteLine( "GetMyPID, Version 1.00" );
Console.Error.WriteLine( "Return or check the parent's (caller's) Process ID" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "{0} [ pid ] [ /v ]", exeName.ToUpper( ) );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "pid" );
Console.ResetColor( );
Console.Error.WriteLine( " is the expected PID to check the actual PID against" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /V" );
Console.ResetColor( );
Console.Error.WriteLine( " display verbose information on the parent process" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: With an expected PID specified, this program returns 'errorlevel'" );
Console.Error.WriteLine( " 0 if the actual PID matches the expected value, or 2 if not;" );
Console.Error.WriteLine( " otherwise, it returns an 'errorlevel' equal to the PID." );
Console.Error.WriteLine( " So to get a batch file's PID, use the following commands:" );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( " GETMYPID.EXE" );
Console.Error.WriteLine( " SET PID=%ErrorLevel%" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Credits: ParentProcess class by Jared Barneck " );
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Error.WriteLine( "www.rhyous.com/2010/04/30" );
Console.Error.WriteLine( " /how-to-get-the-parent-process-that-launched-a-c-application/" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "http://www.robvanderwoude.com" );
return 1;
} #endregion Error Handling }}page last modified: 2025-10-11; loaded in 0.0098 seconds