关机程序乍就不关机呢
参考了网是上的c# 关机程序
其实地方就几行
using System.Runtime.InteropServices; //命名空间
[DllImport( "user32.dll ", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea ); //调用api
如果click关机按钮就
ExitWindowsEx(1,0); //关,关,关
可是为什么就是不关机呢?
是我的代码错了还是和我的操作系统有关系(我的是window 2003 server)
------解决方案--------------------03 需要特权MSDN这样说:“
To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information, see Running with Special Privileges.
------解决方案--------------------using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ShutDown
{
public class ExitWindows
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]//公共语言运行库利用StructLayoutAttribute控制类或结构
//的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列。
//LayoutKind分为三种方式排列非托管内存:Auto、Explicit、Sequential
//当选择用Sequential时需要用Pack规定大小。Pack的值为: 0, 1, 2, 4, 8, 16, 32, 64, 128
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);//打开进程相联系的access token
//access token (通道令牌?)是包含一个会话的安全信息
[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,//赋予或解除一个access token的特权
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport( "user32.dll ", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flg, int rea);//重启、注销、关闭计算机
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const int TOKEN_ADJUST_DEFAULT = 0x0080;//以下TOKEN的值本例中没有用到
internal const int TOKEN_ADJUST_GROUPS = 0x0040;
internal const int TOKEN_ADJUST_SESSIONID = 0x0100;
internal const int TOKEN_ASSIGN_PRIMARY = 0x0001;
internal const int TOKEN_DUPLICATE = 0x0002;
internal const long TOKEN_EXECUTE = 0x20000L;
internal const int TOKEN_IMPERSONATE = 0x0004;
internal const int TOKEN_QUERY_SOURCE = 0x0010;
//internal const int TOKEN_READ=0x200e0L;
internal const long TOKEN_WRITE = 0x200e0L;
internal const long TOKEN_ALL_ACCESS = 0xf00ffL;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege ";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
inte