熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#代碼關閉Windows XP

2022-06-13   來源: .NET編程 
    using System;
  using SystemRuntimeInteropServices;
  
  class shoutdown{
   [StructLayout(LayoutKindSequential Pack=)]
   internal struct TokPrivLuid
   {
   public int Count;
   public long Luid;
   public int Attr;
   }
  
   [DllImport(kerneldll ExactSpelling=true) ]
   internal static extern IntPtr GetCurrentProcess();
  
   [DllImport(advapidll ExactSpelling=true SetLastError=true) ]
   internal static extern bool OpenProcessToken( IntPtr h int acc ref IntPtr phtok );
  
   [DllImport(advapidll SetLastError=true) ]
   internal static extern bool LookupPrivilegeValue( string host string name ref long pluid );
  
   [DllImport(advapidll ExactSpelling=true SetLastError=true) ]
   internal static extern bool AdjustTokenPrivileges( IntPtr htok bool disall
  ref TokPrivLuid newst int len IntPtr prev IntPtr relen );
  
   [DllImport(userdll ExactSpelling=true SetLastError=true) ]
   internal static extern bool ExitWindowsEx( int flg int rea );
  
   internal const int SE_PRIVILEGE_ENABLED = x;
   internal const int TOKEN_QUERY = x;
   internal const int TOKEN_ADJUST_PRIVILEGES = x;
   internal const string SE_SHUTDOWN_NAME = SeShutdownPrivilege;
   internal const int EWX_LOGOFF = x;
   internal const int EWX_SHUTDOWN = x;
   internal const int EWX_REBOOT = x;
   internal const int EWX_FORCE = x;
   internal const int EWX_POWEROFF = x;
   internal const int EWX_FORCEIFHUNG = x;
  
   private static void DoExitWin(int flg)
   {
   bool ok;
   TokPrivLuid tp;
   IntPtr hproc = GetCurrentProcess();
   IntPtr htok = IntPtrZero;
   ok = OpenProcessToken( hproc TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY ref htok );
   tpCount = ;
   tpLuid = ;
   tpAttr = SE_PRIVILEGE_ENABLED;
   ok = LookupPrivilegeValue( null SE_SHUTDOWN_NAME ref tpLuid );
   ok = AdjustTokenPrivileges( htok false ref tp IntPtrZero IntPtrZero );
   ok = ExitWindowsEx( flg );
   }
  
   public static void Main()
   {
   ConsoleWriteLine(正在關閉計算機……);
   // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF EWX_REBOOT等實現不同得功能
   // 在XP下可以看到幫助信息以得到不同得參數
   // SHUTDOWN /?
   DoExitWin(EWX_SHUTDOWN);
   }
  }

From:http://tw.wingwit.com/Article/program/net/201311/12498.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.