Trigger a BSOD when your process is closed

(Originally published March 2011.)

NtSetInformationProcess has a neat undocumented feature. If you pass 29 for the type key, and 1 for the value, your process is marked as essential to the system, and closing it triggers a Blue Screen of Death:

NtSetInformationProcess([Process Handle], 29, 1, 4)

Where the third parameter is an int* (technically a void*, but that’s not relevant). The full code to mark a process essential in C#:

static extern int NtSetInformationProcess(IntPtr p, int c, ref int i, int l);
void SetEssential() {
int e = 1;
NtSetInformationProcess(Process.GetCurrentProcess().Handle, 29, ref e, 4);
void SetUnessential() {
int e = 0;
NtSetInformationProcess(Process.GetCurrentProcess().Handle, 29, ref e, 4);

Note that calling Process.GetCurrentProcess().Kill() after calling SetEssential() is a quick way to immediately crash the system. (This might be of particular use to those who encrypt their entire drive. Hotkey this and you can secure your system in an instant.) To that extent, anything which closes your process will unless you put SetUnessential() in your destructor.