在C#中如何调用windows的API进行关机操作!!! [转]
2006-10-07 22:24
776 查看
http://chenpeng.cnblogs.com/archive/2006/05/24/408313.html
由于工作需要,自已写了一个局域网多媒体软件.里面涉及到计算机的关机,注销,重启操作.需要用到系统的api.刚开始找了很多资料.还是有错误.后在MSDN中找到了相关的资料.可是是英文的.有一难.经过多次修改.终于写出来了.现在我把调用系统的的API执行关机,注销.重启操作的源码发出来.供大家参考....如有不完善的地方.请大家留言.我方便改正!
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading ;
namespace SysLibrary
{
public class SysConfig
{
[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 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 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 bool DoExitWin(int flg)
{
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(flg, 0);
return ok;
}
public void logoff()
{
DoExitWin(EWX_LOGOFF);
}
public void reboot()
{
DoExitWin(EWX_REBOOT);
}
public void Shutdown()
{
DoExitWin(EWX_SHUTDOWN);
}
}
}
由于工作需要,自已写了一个局域网多媒体软件.里面涉及到计算机的关机,注销,重启操作.需要用到系统的api.刚开始找了很多资料.还是有错误.后在MSDN中找到了相关的资料.可是是英文的.有一难.经过多次修改.终于写出来了.现在我把调用系统的的API执行关机,注销.重启操作的源码发出来.供大家参考....如有不完善的地方.请大家留言.我方便改正!
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading ;
namespace SysLibrary
{
public class SysConfig
{
[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 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 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 bool DoExitWin(int flg)
{
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(flg, 0);
return ok;
}
public void logoff()
{
DoExitWin(EWX_LOGOFF);
}
public void reboot()
{
DoExitWin(EWX_REBOOT);
}
public void Shutdown()
{
DoExitWin(EWX_SHUTDOWN);
}
}
}
相关文章推荐
- 在C#中如何调用windows的API进行关机操作!!!
- [C#] 如何调用Windows系统发送错误API
- 如何从C# 桌面应用程序调用Windows 8的WinRT API- WinRT 图表
- c#调用api控制windows关机示例(可以重启/注销)
- 在c#中怎么调用ExitWindowsEx函数实现XP的关机、注销、重启操作?
- 如何从C#桌面应用程序中调用Windows 8的WinRT API (Metro API)
- c#调用api控制windows关机示例
- 在C#中如何与Windows认证的SQL Server进行连接?
- C#中如何进行远程调用别人的接口
- C#调用Geocoding API进行地理编码与逆编码
- 【大数据系列】windows环境下搭建hadoop开发环境使用api进行基本操作
- 如何创建、发布Web Service和 C#WINFORM里如何调用WEB Service API
- C#在一台客户端进行了某个操作以后如何让其他不同的客户端如何实现相同的操作(三)
- 如何:对 Windows 窗体控件进行线程安全调用(转载)
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- 如何:对 Windows 窗体控件进行线程安全调用
- .Net语言 APP开发平台——Smobiler学习日志:如何调用API进行短信发送
- 如何创建、发布Web Service和 C#WINFORM里如何调用WEB Service API
- Wince 或Windows平台 C#调用Bitmap对象后资源应该如何释放
- 【转】 如何利用C#代码来进行操作AD