2013年10月24日 星期四

Visual Studio筆記 :: Shutdown API sample

VC++
MSDN: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(EXITWINDOWSEX);k(DevLang-%22C%2B%2B%22)&rd=true



#include "stdafx.h"
#include "Windows.h"
#include "Winuser.h"

int _tmain(int argc, _TCHAR* argv[])

{
   HANDLE hToken;
   TOKEN_PRIVILEGES tkp;
  
   // Get a token for this process.   
   if (!OpenProcessToken(GetCurrentProcess(),
      TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
      printf("Failed to get the token for this process");
  
   // Get the LUID for the shutdown privilege.   
   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
  
   tkp.PrivilegeCount = 1;  // one privilege to set   
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  
   // Get the shutdown privilege for this process.
  
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
      (PTOKEN_PRIVILEGES)NULL, 0);
  
   if (GetLastError() != ERROR_SUCCESS)
      printf("Failed to adjust the token privileges.");
  
   // Shut down the system and force all applications to close.   
   if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
      SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
      SHTDN_REASON_MINOR_UPGRADE |
      SHTDN_REASON_FLAG_PLANNED))
      printf("Failed to Shut Down the system.");

return 0;

}

沒有留言:

張貼留言

Thanks for your message.

Python notes: Calculate delay time by WinDBG log

用WinDBG開Event Timestamps可以產生下面格式的log: Fri Sep 21 18:43:50.946 2018 (UTC + 8:00): @#$#^$@#$^ 以下python code用來找出兩個指定log中的時間差