title: How to prevent an app from being killed in task manager?
body: <p>I'm working on a parental control app (written in WPF) and would like to disallow anybody (including administrator) to kill my process. A while back, I found the following code online and it almost works perfectly, except that it doesn't work sometimes. </p>
  
  <pre><code>static void SetAcl()
  {
      var sd = new RawSecurityDescriptor(ControlFlags.None, new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), null, null, new RawAcl(2, 0));
      sd.SetFlags(ControlFlags.DiscretionaryAclPresent | ControlFlags.DiscretionaryAclDefaulted);
      var rawSd = new byte[sd.BinaryLength];
  
      sd.GetBinaryForm(rawSd, 0);
      if (!Win32.SetKernelObjectSecurity(Process.GetCurrentProcess().Handle, SecurityInfos.DiscretionaryAcl, rawSd))
          throw new Win32Exception();
  }
  </code></pre>
  
  <p>In Win7, if the app is started by the logged in user, even the admin cannot kill the process (access denied). However, if you switch to another user account (admin or standard user), then check "Show processes for all users", then you kill the process without a problem. Can anybody give me a hint why and how to fix it?</p>
  
  <p>EDIT:<br>
  I understand some people are upset by this question, but here is my dilemma. This is a parental control I wrote primarily for my own use. The main feature is that I want to monitor and limit my kids' on games (not simply turn off all games). I could assign kids a standard user account and they cannot kill the process. However, some games (e.g. Mabinogi) require admin right to be playable. So, I had to type in my admin password each time, which is annoying. </p>
  
  <p>By the way, I'm not sure if it's against Stackoverflow's policy, here is my app if you'd like to check it out: <a href="https://sites.google.com/site/goppieinc/pc-screen-watcher" rel="noreferrer">https://sites.google.com/site/goppieinc/pc-screen-watcher</a>.</p>
  
  <p>EDIT:<br>
  My main point of this post is to ask if somebody could give me a hint why the posted code doesn't always work - e.g. in case you show processes for all users.</p>