首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关机程序乍就不关机呢,该如何解决

2012-01-01 
关机程序乍就不关机呢参考了网是上的c#关机程序其实地方就几行usingSystem.Runtime.InteropServices//命

关机程序乍就不关机呢
参考了网是上的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;
internal const int EWX_REBOOT = 0x00000002;


internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
public ExitWindows()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool DoExitWindows(int how)//执行
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();//IntPtr是平台提供的一个结构体。它用于描述一个指针或句柄
IntPtr htok = IntPtr.Zero;//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);
switch (how)
{
case 0:
ok = ExitWindowsEx(EWX_LOGOFF, 0);
break;
case 1:
ok = ExitWindowsEx(EWX_SHUTDOWN, 0);
break;
case 2:
ok = ExitWindowsEx(EWX_REBOOT, 0);
break;
case 3:
ok = ExitWindowsEx(EWX_FORCE, 0);
break;
case 4:
ok = ExitWindowsEx(EWX_POWEROFF, 0);
break;
case 5:
ok = ExitWindowsEx(EWX_FORCEIFHUNG, 0);
break;
}
return ok;
}

}
}

[解决办法]
接分

热点排行