A managed wrapper around PROC_THREAD_ATTRIBUTE_JOB_LIST.
Warning
This class library only works on Windows 10 and newer, or Windows Server 2016 and newer. If you need a solution for Linux or macOS, there is a discussion here.
ProcThreadAttributeJobListDotNet is licensed under The MIT License.
Install the ProcThreadAttributeJobListDotNet package from NuGet.
var extendedLimitInformation = new JobObjectExtendedLimitInformation
{
BasicLimitInformation = { LimitFlags = JobObjectLimit.KillOnJobClose }
};
using var safeJobObjectHandle = extendedLimitInformation.CreateJobObject();
using var process = safeJobObjectHandle.CreateAssociatedProcess(
@"C:\Windows\System32\notepad.exe", @"C:\Windows\System32\drivers\etc\hosts");
// ...CreateAssociatedProcess necessarily creates StartupInfoEx for its AttributeList. The contents of the concomitant
StartupInfo (except for cb) are irrelevant to creating an associated process, so you can use configureStartupInfo
to control the created process further:
var extendedLimitInformation = new JobObjectExtendedLimitInformation
{
BasicLimitInformation = { LimitFlags = JobObjectLimit.KillOnJobClose }
};
using var safeJobObjectHandle = extendedLimitInformation.CreateJobObject();
using var process = safeJobObjectHandle.CreateAssociatedProcess(
@"C:\Windows\System32\notepad.exe", configureStartupInfo: MyConfiguration);
// ...
void MyConfiguration(ref StartupInfo startupInfo)
{
startupInfo.Flags = StartupInfoFlags.UseShowWindow;
startupInfo.ShowWindowCommand = ShowWindowCommand.Maximize;
}Raymond Chen for bringing PROC_THREAD_ATTRIBUTE_JOB_LIST to my attention in A more direct and mistake-free way of creating a process in a job object.
CsWin32 for generating several of the structures and enums required.
PInvoke.net for the rest.