title: How to programmatically schedule a task to run when shutting down Windows?
body: <p>This is almost the same as <a href="https://stackoverflow.com/questions/101647/how-to-schedule-a-task-to-run-when-shutting-down-windows">How to schedule a task to run when shutting down windows</a>, except I need to do it <strong>programmatically</strong>. Also, I need the guarantee that when the script is run, <strong>no application can cancel the shutdown anymore</strong>.</p>
  
  <p>Is this possible?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Update, with a little context</strong>: My company develops a sort of an antivirus program, designed to protect internet banking costumers. Naturally, this program is a frequent target of malware, so it needs to defend itself against being uninstalled. The first thing it does is inject itself into a system process, so that it can't be killed without bringing the whole system with it.</p>
  
  <p>One of the typical ways of uninstalling such a program would be marking it for removal at the next shutdown, so our app actively monitors the registry keys which handle these postponed deletes and removes them.</p>
  
  <p>I have recently been tasked with writing - oh the irony - an uninstaller for our app. The uninstaller must, of course, delete the files, so it has to work around everything we ourselves have built. It must be compatible with legacy versions, so changing the protection mechanisms is not an option.</p>