当前位置:首页 > IT技术 > Windows编程 > 正文

C#代码关闭Windows XP
2021-08-08 13:21:14

using System;
C#代码关闭Windows XP_C#using System.Runtime.InteropServices;
C#代码关闭Windows XP_C#   
C#代码关闭Windows XP_C#_03class shoutdown{
C#代码关闭Windows XP_C#_04   [StructLayout(LayoutKind.Sequential, Pack=1)]
C#代码关闭Windows XP_C#_04   internal struct TokPriv1Luid
C#代码关闭Windows XP_C#_06   {
C#代码关闭Windows XP_C#_04      public int Count;
C#代码关闭Windows XP_C#_04      public long Luid;
C#代码关闭Windows XP_C#_04      public int Attr;
C#代码关闭Windows XP_C#_10   }
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04   [DllImport("kernel32.dll", ExactSpelling=true) ]
C#代码关闭Windows XP_C#_04   internal static extern IntPtr GetCurrentProcess();
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04   [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
C#代码关闭Windows XP_C#_04   internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04   [DllImport("advapi32.dll", SetLastError=true) ]
C#代码关闭Windows XP_C#_04   internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04   [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
C#代码关闭Windows XP_C#_04   internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
C#代码关闭Windows XP_C#_04ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04   [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
C#代码关闭Windows XP_C#_04   internal static extern bool ExitWindowsEx( int flg, int rea );
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04   internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
C#代码关闭Windows XP_C#_04   internal const int TOKEN_QUERY = 0x00000008;
C#代码关闭Windows XP_C#_04   internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
C#代码关闭Windows XP_C#_04   internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
C#代码关闭Windows XP_C#_04   internal const int EWX_LOGOFF = 0x00000000;
C#代码关闭Windows XP_C#_04   internal const int EWX_SHUTDOWN = 0x00000001;
C#代码关闭Windows XP_C#_04   internal const int EWX_REBOOT = 0x00000002;
C#代码关闭Windows XP_C#_04   internal const int EWX_FORCE = 0x00000004;
C#代码关闭Windows XP_C#_04   internal const int EWX_POWEROFF = 0x00000008;
C#代码关闭Windows XP_C#_04   internal const int EWX_FORCEIFHUNG = 0x00000010;
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04   private static void DoExitWin(int flg)
C#代码关闭Windows XP_C#_06   {
C#代码关闭Windows XP_C#_04      bool ok;
C#代码关闭Windows XP_C#_04      TokPriv1Luid tp;
C#代码关闭Windows XP_C#_04      IntPtr hproc = GetCurrentProcess();
C#代码关闭Windows XP_C#_04      IntPtr htok = IntPtr.Zero;
C#代码关闭Windows XP_C#_04      ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
C#代码关闭Windows XP_C#_04      tp.Count = 1;
C#代码关闭Windows XP_C#_04      tp.Luid = 0;
C#代码关闭Windows XP_C#_04      tp.Attr = SE_PRIVILEGE_ENABLED;
C#代码关闭Windows XP_C#_04      ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
C#代码关闭Windows XP_C#_04      ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
C#代码关闭Windows XP_C#_04      ok = ExitWindowsEx( flg, 0 );
C#代码关闭Windows XP_C#_10   }
C#代码关闭Windows XP_C#_04
C#代码关闭Windows XP_C#_04      public static void Main()
C#代码关闭Windows XP_C#_06      {
C#代码关闭Windows XP_C#_04         Console.WriteLine("正在关闭计算机……");
C#代码关闭Windows XP_C#_04         // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。
C#代码关闭Windows XP_C#_04         // 在XP下可以看到帮助信息,以得到不同得参数
C#代码关闭Windows XP_C#_04         // SHUTDOWN /?
C#代码关闭Windows XP_C#_04         DoExitWin(EWX_SHUTDOWN);
C#代码关闭Windows XP_C#_10      }
C#代码关闭Windows XP_C#_62}

本文摘自 :https://blog.51cto.com/u