Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for parentprocess.cs

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

  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4.  
  5. // Source: "How to get the parent process that launched a C# application?" by Jared Barneck
  6. // http://www.rhyous.com/2010/04/30/how-to-get-the-parent-process-that-launched-a-c-application/
  7.  
  8. namespace ParentProcess
  9. {
  10. 	public class ParentProcess
  11. 	{
  12. 		public static String ProcessName
  13. 		{
  14. 			get { return GetParentProcess( ).ProcessName; }
  15. 		}
  16.  
  17. 		public static int ProcessId
  18. 		{
  19. 			get { return GetParentProcess( ).Id; }
  20. 		}
  21.  
  22. 		public static String FullPath
  23. 		{
  24. 			get
  25. 			{
  26. 				return GetParentProcess( ).MainModule.FileName;
  27. 			}
  28. 		}
  29.  
  30. 		public static String FileName
  31. 		{
  32. 			get
  33. 			{
  34. 				return System.IO.Path.GetFileName( GetParentProcess( ).MainModule.FileName );
  35. 			}
  36. 		}
  37.  
  38. 		public static String DirectoryName
  39. 		{
  40. 			get
  41. 			{
  42. 				return System.IO.Path.GetDirectoryName( GetParentProcess( ).MainModule.FileName );
  43. 			}
  44. 		}
  45.  
  46. 		private static Process GetParentProcess( )
  47. 		{
  48. 			int iParentPid = 0;
  49. 			int iCurrentPid = Process.GetCurrentProcess( ).Id;
  50.  
  51. 			IntPtr oHnd = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  52.  
  53. 			if ( oHnd == IntPtr.Zero )
  54. 				return null;
  55.  
  56. 			PROCESSENTRY32 oProcInfo = new PROCESSENTRY32( );
  57.  
  58. 			oProcInfo.dwSize =
  59. 			(uint) System.Runtime.InteropServices.Marshal.SizeOf( typeof( PROCESSENTRY32 ) );
  60.  
  61. 			if ( Process32First( oHnd, ref oProcInfo ) == false )
  62. 				return null;
  63.  
  64. 			do
  65. 			{
  66. 				if ( iCurrentPid == oProcInfo.th32ProcessID )
  67. 					iParentPid = (int) oProcInfo.th32ParentProcessID;
  68. 			}
  69. 			while ( iParentPid == 0 && Process32Next( oHnd, ref oProcInfo ) );
  70.  
  71. 			if ( iParentPid > 0 )
  72. 				return Process.GetProcessById( iParentPid );
  73. 			else
  74. 				return null;
  75. 		}
  76.  
  77. 		static uint TH32CS_SNAPPROCESS = 2;
  78.  
  79. 		[StructLayout( LayoutKind.Sequential )]
  80. 		public struct PROCESSENTRY32
  81. 		{
  82. 			public uint dwSize;
  83. 			public uint cntUsage;
  84. 			public uint th32ProcessID;
  85. 			public IntPtr th32DefaultHeapID;
  86. 			public uint th32ModuleID;
  87. 			public uint cntThreads;
  88. 			public uint th32ParentProcessID;
  89. 			public int pcPriClassBase;
  90. 			public uint dwFlags;
  91. 			[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 260 )]
  92. 			public string szExeFile;
  93. 		};
  94.  
  95. 		[DllImport( "kernel32.dll", SetLastError = true )]
  96. 		static extern IntPtr CreateToolhelp32Snapshot( uint dwFlags, uint th32ProcessID );
  97.  
  98. 		[DllImport( "kernel32.dll" )]
  99. 		static extern bool Process32First( IntPtr hSnapshot, ref PROCESSENTRY32 lppe );
  100.  
  101. 		[DllImport( "kernel32.dll" )]
  102. 		static extern bool Process32Next( IntPtr hSnapshot, ref PROCESSENTRY32 lppe );
  103. 	}
  104. }
  105.  

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