예제 #1
0
        private static void SetNewProcessParent(ref STARTUPINFOEX startupInfoEx, int parentProcessId)
        {
            const int PROC_THREAD_ATTRIBUTE_PARENT_PROCESS = 0x00020000;
            IntPtr    handle  = WinAPI.OpenProcess(ProcessAccessFlags.CreateProcess | ProcessAccessFlags.DuplicateHandle, false, parentProcessId);
            IntPtr    lpValue = Marshal.AllocHGlobal(IntPtr.Size);

            Marshal.WriteIntPtr(lpValue, handle);

            bool success = UpdateProcThreadAttribute(startupInfoEx.lpAttributeList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, lpValue,
                                                     (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero);

            if (!success)
            {
                throw new Exception(string.Format($"Error setting [{parentProcessId}] as the parent PID for the new process"));
            }
        }