[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReserved );
[DllImport("shell32.dll")]
public static extern int ShelAbout(int uFlags, int dwReserved);
[ DllImport("user32.dll") ] 
public static extern int SetWindowPos(int hwnd , int hWndInsertAfter, int X , int y , int cx, int cy, int wFlagslong) ;static int dwReserved = 0;
static int SHUTDOWN = 1 ;
static int EWX_FORCE = 4;
static int EWX_REBOOT = 2 ;
static int LOGOFF = 0 ;//重启
private void button2_Click(object sender, System.EventArgs e)
{
sh = ExitWindowsEx(EWX_REBOOT, dwReserved);
}
//注销
private void button3_Click(object sender, System.EventArgs e)
{
sh = ExitWindowsEx(EWX_FORCE, dwReserved);
}
//关机 
private void button1_Click(object sender, System.EventArgs e)
{
sh = ExitWindowsEx(SHUTDOWN, dwReserved);
}为什么不能关机和重启呢?

解决方案 »

  1.   

    C++下的代码可以实现,你自己看一下吧!
     OSVERSIONINFO osv;
            osv.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
            GetVersionEx(&osv);
            if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT)
            {
                    HANDLE hProcess,hToken;
                    TOKEN_PRIVILEGES Privileges;
                    LUID luid;
                    hProcess=GetCurrentProcess();
                    OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);                Privileges.PrivilegeCount=1;
                    LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
                    Privileges.Privileges[0].Luid=luid;
                    Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
                    AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);
            }        switch(iKind)
            {
                    case 0:
                    {
                           // ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0);//关机
                            ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF,8);
                            break;
                    }
                    case 1:
                    {
                            ExitWindowsEx(EWX_LOGOFF, 0);//注销                        break;
                    }
                    case 2:
                    {
                            ExitWindowsEx(EWX_REBOOT, 0);//重启
                            break;
                    }
            }
    }
      

  2.   

    同意 jinjazz(近身剪(充电中...)) 
    因为你的程序没有关机的权限 像你的那个程序在win98下可以关机 在98以上的操作系统中必须给程序赋予能够关机的权限~ 要构造好像3个结构体呢!建议你用Google搜一下C# 关机程序
      

  3.   

    using System;
    using System.Runtime.InteropServices;namespace ExitWindows
    {
    public class Class1
    {
    [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 void 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 );
    } public static void Reboot()
    {
    DoExitWin(  EWX_FORCE | EWX_REBOOT );
    } public static void PowerOff()
    {
    DoExitWin(   EWX_FORCE | EWX_POWEROFF );
    } public static void LogoOff()
    {
    DoExitWin (  EWX_FORCE | EWX_LOGOFF );
    } }}
      

  4.   

    恩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 void 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 );
    }
    这一点写的很好,类似还有se debug权,这段你要收藏