用C#控制计算机重启、关机及注销

代码如下:

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Runtime.InteropServices;
None.gif
None.gif
namespace chkipsrv
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class Shudown
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [StructLayout(LayoutKind.Sequential, Pack 
= 1)]
InBlock.gif        
internal struct TokPriv1Luid
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int Count;
InBlock.gif            
public long Luid;
InBlock.gif            
public int Attr;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"kernel32.dll", ExactSpelling = true)]
InBlock.gif        
internal static extern IntPtr GetCurrentProcess();
InBlock.gif
InBlock.gif        [DllImport(
"advapi32.dll", ExactSpelling = true, SetLastError = true)]
InBlock.gif        
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
InBlock.gif
InBlock.gif        [DllImport(
"advapi32.dll", SetLastError = true)]
InBlock.gif        
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
InBlock.gif
InBlock.gif        [DllImport(
"advapi32.dll", ExactSpelling = true, SetLastError = true)]
InBlock.gif        
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
InBlock.gif        
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", ExactSpelling = true, SetLastError = true)]
InBlock.gif        
internal static extern bool ExitWindowsEx(int DoFlag, int rea);
InBlock.gif
InBlock.gif        
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
InBlock.gif        
internal const int TOKEN_QUERY = 0x00000008;
InBlock.gif        
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
InBlock.gif        
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
InBlock.gif        
internal const int EWX_LOGOFF = 0x00000000;
InBlock.gif        
internal const int EWX_SHUTDOWN = 0x00000001;
InBlock.gif        
internal const int EWX_REBOOT = 0x00000002;
InBlock.gif        
internal const int EWX_FORCE = 0x00000004;
InBlock.gif        
internal const int EWX_POWEROFF = 0x00000008;
InBlock.gif        
internal const int EWX_FORCEIFHUNG = 0x00000010;
InBlock.gif
InBlock.gif        
private static void DoExitWin(int DoFlag)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool ok;
InBlock.gif            TokPriv1Luid tp;
InBlock.gif            IntPtr hproc 
= GetCurrentProcess();
InBlock.gif            IntPtr htok 
= IntPtr.Zero;
InBlock.gif            ok 
= OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
InBlock.gif            tp.Count 
= 1;
InBlock.gif            tp.Luid 
= 0;
InBlock.gif            tp.Attr 
= SE_PRIVILEGE_ENABLED;
InBlock.gif            ok 
= LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
InBlock.gif            ok 
= AdjustTokenPrivileges(htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero);
InBlock.gif            ok 
= ExitWindowsEx(DoFlag, 0);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void Reboot()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DoExitWin(EWX_FORCE 
| EWX_REBOOT);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void PowerOff()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DoExitWin(EWX_FORCE 
| EWX_POWEROFF);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void LogOff()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DoExitWin(EWX_FORCE 
| EWX_LOGOFF);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/yangbeibei/archive/2007/02/05/640301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值