Shutting Down Windows from C++

Windows can be programmatically shutdown using the ExitWindowsEx() Win32 function.  However it turns out that the trick to getting it to work is to first grant shutdown priviliges to the calling process via some rather ugly security functions – this type of faffing around can never me remembered so I will post some sample code here in the hopes that it might help me (in the future) or maybe someone else: