■ Process 클래스를 사용해 부모 프로세스를 구하는 방법을 보여준다.
[TestChild 프로젝트]
▶ ProcessInformation.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using System; using System.Runtime.InteropServices; namespace TestChild { /// <summary> /// 프로세스 정보 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct ProcessInformation { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 예약 /// </summary> public IntPtr Reserved; /// <summary> /// PEB 기준 주소 /// </summary> public IntPtr PEbBaseAddress; /// <summary> /// 예약 2-0 /// </summary> public IntPtr Reserved2_0; /// <summary> /// 예약 2-1 /// </summary> public IntPtr Reserved2_1; /// <summary> /// 고유 프로세스 ID /// </summary> public IntPtr UniqueProcessID; /// <summary> /// 프로세스 ID /// </summary> public IntPtr InheritedFromUniqueProcessID; #endregion } } |
▶ ProcessHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
using System; using System.ComponentModel; using System.Diagnostics; using System.Runtime.InteropServices; namespace TestChild { /// <summary> /// 프로세스 헬퍼 /// </summary> public static class ProcessHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region NT 프로세스 정보 쿼리하기 - NtQueryInformationProcess(processHandle, processInformationClass, processInformation, processInformationLength, returnLength) /// <summary> /// NT 프로세스 정보 쿼리하기 /// </summary> /// <param name="processHandle">프로세스 핸들</param> /// <param name="processInformationClass">프로세스 정보 클래스</param> /// <param name="processInformation">프로세스 정보</param> /// <param name="processInformationLength">프로세스 정보 길이</param> /// <param name="returnLength">반환 길이</param> /// <returns>처리 결과</returns> [DllImport("ntdll")] private static extern int NtQueryInformationProcess ( IntPtr processHandle, int processInformationClass, ref ProcessInformation processInformation, int processInformationLength, out int returnLength ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 부모 프로세스 구하기 - GetParentProcess(processHandle) /// <summary> /// 부모 프로세스 구하기 /// </summary> /// <param name="processHandle">프로세스 핸들</param> /// <returns>부모 프로세스</returns> public static Process GetParentProcess(IntPtr processHandle) { ProcessInformation processInformation = new ProcessInformation(); int returnLength; int status = NtQueryInformationProcess ( processHandle, 0, ref processInformation, Marshal.SizeOf(processInformation), out returnLength ); if(status != 0) { throw new Win32Exception(status); } try { return Process.GetProcessById(processInformation.InheritedFromUniqueProcessID.ToInt32()); } catch { return null; } } #endregion #region 부모 프로세스 구하기 - GetParentProcess(processID) /// <summary> /// 부모 프로세스 구하기 /// </summary> /// <param name="processID">프로세스 ID</param> /// <returns>부모 프로세스</returns> public static Process GetParentProcess(int processID) { Process process = Process.GetProcessById(processID); return GetParentProcess(process.Handle); } #endregion #region 부모 프로세스 구하기 - GetParentProcess() /// <summary> /// 부모 프로세스 구하기 /// </summary> /// <returns>부모 프로세스</returns> public static Process GetParentProcess() { return GetParentProcess(Process.GetCurrentProcess().Handle); } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Diagnostics; namespace TestChild { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Process process = ProcessHelper.GetParentProcess(); Console.WriteLine(process.ProcessName); Console.ReadKey(false); } #endregion } } |
[TestParent 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System.Diagnostics; namespace TestParent { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Process process = new Process(); process.StartInfo.FileName = "TestChild.exe"; process.Start(); process.WaitForExit(); } #endregion } } |