[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);
}为什么不能关机和重启呢?
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);
}为什么不能关机和重启呢?
解决方案 »
- 如何给动态生成的HtmlInputCheckBox旁边添加标签说明
- 水果落地游戏
- C#中int32.parse未处理FormatException,怎么解决?
- 用C#在WORD中插入表格,如何将表格的属性设置为--对齐方式为:左对齐 文字环绕为:环绕
- 求dataGridView多表头的制作
- c#读取xml老是报错???
- 多线程中,不同对象生成的线程如何能共享一个阻塞?
- 数字的本地化(C#)(一点经验,圣诞散分)
- C#中切割视图的控件有没有
- 我再没兴趣学C#了!
- 请高手伸手解决WebBrowser超难问题,先给20,结贴时加100分
- 如何判断axWebBrowser1_DocumentComplete事件是否已完成?
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;
}
}
}
因为你的程序没有关机的权限 像你的那个程序在win98下可以关机 在98以上的操作系统中必须给程序赋予能够关机的权限~ 要构造好像3个结构体呢!建议你用Google搜一下C# 关机程序
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 );
} }}
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权,这段你要收藏