在c#中调用ExitWindowsEx函数实现XP的关机、注销
2014-03-26 12:59
573 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace System.Helper
{
public class ServerRoot
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int DoFlag, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private static bool DoExitWin(int DoFlag)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(DoFlag, 0);
return ok;
}
/// <summary>
/// 重新启动
/// </summary>
public static bool Reboot()
{
return DoExitWin(EWX_FORCE | EWX_REBOOT);
}
/// <summary>
/// 关机
/// </summary>
public static bool PowerOff()
{
return DoExitWin(EWX_FORCE | EWX_POWEROFF);
}
/// <summary>
/// 注销
/// </summary>
public static bool LogOff()
{
return DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace System.Helper
{
public class ServerRoot
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int DoFlag, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private static bool DoExitWin(int DoFlag)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(DoFlag, 0);
return ok;
}
/// <summary>
/// 重新启动
/// </summary>
public static bool Reboot()
{
return DoExitWin(EWX_FORCE | EWX_REBOOT);
}
/// <summary>
/// 关机
/// </summary>
public static bool PowerOff()
{
return DoExitWin(EWX_FORCE | EWX_POWEROFF);
}
/// <summary>
/// 注销
/// </summary>
public static bool LogOff()
{
return DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
}
相关文章推荐
- 在c#中怎么调用ExitWindowsEx函数实现XP的关机、注销、重启操作?
- DELPHI中调用API函数ExitWindowsEX可以实现系统的关机,注销,和重启
- 关机,注销,重启函数:ExitWindowsEx
- Window关机函数ExitWindowsEx详解
- c#调用api控制windows关机示例(可以重启/注销)
- C#实现控制Windows系统关机、重启和注销的方法
- C#实现控制Windows系统关机、重启和注销的方法:
- Window关机函数ExitWindowsEx详解
- Window关机函数ExitWindowsEx详解 - dwdm - 博客园
- golang 调用win32 api 实现windows注销、重启、关机
- Window关机函数ExitWindowsEx详解
- ExitWindowsEX()函数用来退出、重启或注销系统
- ExitWindowsEx 实现关机、重启
- Window关机函数ExitWindowsEx详解
- C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠
- C# 调用API,实现注销远程登录本机的用户
- C#.NET dll封装(包含UI和实现函数),通过反射调用dll实现控件的快速加载(PrimPointList.cs)
- c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制
- C#实现根据字符串调用同名的函数
- C#.NET dll封装(包含UI和实现函数),通过反射调用dll实现控件的快速加载(PrimPointListCreator.cs)