using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace CSharp瞬间关机
{
public partial class Form1 : Form
{
[DllImport("advapi32.dll")]
public static extern bool OpenProcessToken(IntPtr ProcessHandle, int access, ref IntPtr TokenHandle); [DllImport("advapi32.dll")]
public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool Disable, ref TokenLuid NewState, int BufferLength, IntPtr PreviousState,IntPtr ReturnLength);
public struct TokenLuid
{ public int Count;
public long Luid;
public int attr;
}
[DllImport("advapi32.dll")]
public static extern bool LookupPrivilegeValue(string lps,string lpn,ref long lpLuid); public const int SE_PRIVILEGE_ENABLED = 0x00000002;
public const int TOKEN_QUERY = 0x00000008;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess(); //Declare Function NtShutdownSystem& Lib "ntdll" (ByVal ShutdownAction As Int32) [DllImport("ntdll.dll")]
private static extern void NtShutdownSystem(int ShutdownAction);
const int SHUTDOWN = 0;
const int POWEROFF = 2; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
IntPtr a =IntPtr.Zero;
IntPtr b = GetCurrentProcess();
TokenLuid d;
d.Count=1;
d.Luid=0;
d.attr=SE_PRIVILEGE_ENABLED; OpenProcessToken(b, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, ref a);
LookupPrivilegeValue(null,SE_SHUTDOWN_NAME, ref d.Luid);
AdjustTokenPrivileges(a, false, ref d, 0, IntPtr.Zero, IntPtr.Zero);
} private void button1_Click(object sender, EventArgs e)
{
NtShutdownSystem(2);
}
}
}
为什么失败呢...
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace CSharp瞬间关机
{
public partial class Form1 : Form
{
[DllImport("advapi32.dll")]
public static extern bool OpenProcessToken(IntPtr ProcessHandle, int access, ref IntPtr TokenHandle); [DllImport("advapi32.dll")]
public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool Disable, ref TokenLuid NewState, int BufferLength, IntPtr PreviousState,IntPtr ReturnLength);
public struct TokenLuid
{ public int Count;
public long Luid;
public int attr;
}
[DllImport("advapi32.dll")]
public static extern bool LookupPrivilegeValue(string lps,string lpn,ref long lpLuid); public const int SE_PRIVILEGE_ENABLED = 0x00000002;
public const int TOKEN_QUERY = 0x00000008;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess(); //Declare Function NtShutdownSystem& Lib "ntdll" (ByVal ShutdownAction As Int32) [DllImport("ntdll.dll")]
private static extern void NtShutdownSystem(int ShutdownAction);
const int SHUTDOWN = 0;
const int POWEROFF = 2; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
IntPtr a =IntPtr.Zero;
IntPtr b = GetCurrentProcess();
TokenLuid d;
d.Count=1;
d.Luid=0;
d.attr=SE_PRIVILEGE_ENABLED; OpenProcessToken(b, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, ref a);
LookupPrivilegeValue(null,SE_SHUTDOWN_NAME, ref d.Luid);
AdjustTokenPrivileges(a, false, ref d, 0, IntPtr.Zero, IntPtr.Zero);
} private void button1_Click(object sender, EventArgs e)
{
NtShutdownSystem(2);
}
}
}
为什么失败呢...
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货