title: How to unload Windows Kernel Mode Driver from the inside?
body: <p>I'm writing a Windows kernel driver and i'm trying to implement the following.</p>
  
  <p>Suppose the driver has been called from user mode via <a href="http://msdn.microsoft.com/en-us/library/aa363216%28v=vs.85%29.aspx" rel="noreferrer">DeviceIoControl</a> function with a specific IOCTL code. How can driver unload itself in this situation? In other words, how to achieve the same behavior as running <code>net stop</code> command?</p>
  
  <p>More precisely, what should i write when implementing <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff543287%28v=vs.85%29.aspx" rel="noreferrer">DispatchDeviceControl callback</a></p>
  
  <pre><code>NTSTATUS IoctlDeviceControl(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) {
      // some code...
  
      switch (ioctlCode) {
          case IOCTL_MY_UNLOAD:  &lt;---
          ...
      }
  
      return Status;
  }
  </code></pre>