C#关机代码的实现浅析

开发 后端
C#关机代码是如何实现的呢?当我们在实际开发的过程中遇见这样的需求的时候我们是如何把握的呢?那么本文就向你详细介绍C#关机代码的实现过程。

C#关机代码的实现是如何的呢?我们经常会碰到这样的需求,那么在这方面我们是如何实现的呢?让我们还是看看实际的代码操作,希望对于你C#关机代码的开发有所启示。

C#关机代码实现实例:(支持Win XP下的关机)

  1. using System.Runtime.InteropServices;  
  2. //C#关机代码  
  3. [StructLayout(LayoutKind.Sequential, Pack=1)]   
  4.  internal struct TokPriv1Luid   
  5. {   
  6.  public int Count;   
  7.  public long Luid;   
  8.  public int Attr;   
  9. }   
  10.  
  11. //C#关机代码  
  12. [DllImport("kernel32.dll", ExactSpelling=true) ]   
  13. internal static extern IntPtr GetCurrentProcess();   
  14.  
  15. [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]   
  16. internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );   
  17.  
  18. [DllImport("advapi32.dll", SetLastError=true) ]   
  19. internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );   
  20.  
  21. [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]   
  22. internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,   
  23.  ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );   
  24.  
  25. [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]   
  26. internal static extern bool ExitWindowsEx( int flg, int rea );   
  27.  
  28. internal const int SE_PRIVILEGE_ENABLED = 0x00000002;   
  29. internal const int TOKEN_QUERY = 0x00000008;   
  30. internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;   
  31. internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";   
  32. internal const int EWX_LOGOFF = 0x00000000;   
  33. internal const int EWX_SHUTDOWN = 0x00000001;   
  34. internal const int EWX_REBOOT = 0x00000002;   
  35. internal const int EWX_FORCE = 0x00000004;   
  36. internal const int EWX_POWEROFF = 0x00000008;   
  37. internal const int EWX_FORCEIFHUNG = 0x00000010;   
  38. //C#关机代码  
  39. private void DoExitWin( int flg )   
  40. {   
  41.  bool ok;   
  42.  TokPriv1Luid tp;   
  43.  IntPtr hproc = GetCurrentProcess();   
  44.  IntPtr htok = IntPtr.Zero;   
  45.  ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );   
  46.  tp.Count = 1;   
  47.  tp.Luid = 0;   
  48.  tp.Attr = SE_PRIVILEGE_ENABLED;   
  49.  ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );   
  50.  ok = AdjustTokenPrivileges( htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero );   
  51.  ok = ExitWindowsEx( flg, 0 );   
  52. }   
  53.  
  54. public void Reboot()   
  55. {   
  56.  DoExitWin( EWX_REBOOT );   
  57. }   
  58. //C#关机代码  
  59. //点击按钮,执行关机事件  
  60. private void button1_Click(object sender, System.EventArgs e)  
  61. {  
  62.  DoExitWin(1);  
  63. }  

C#关机代码的实现过程就向你介绍到这里,希望对你了解和学习C#关机代码有所帮助。

【编辑推荐】

  1. C#动态创建数组详细实现过程解析
  2. C#声明数组的详细解析
  3. 如何初始化数组详解
  4. C#数组操作的体会浅谈
  5. C#关机代码实例详解
责任编辑:仲衡 来源: 博客园
相关推荐

2009-09-02 17:12:06

C#关机代码

2009-09-07 09:36:29

C# DisposeDispose方法

2009-08-26 09:54:45

C#打印预览C#打印

2009-09-01 18:29:24

C#实现多个接口

2009-08-31 16:48:02

C#实现IDispos

2009-09-02 15:34:37

C#实现插件构架

2009-08-27 18:09:49

C#接口的实现

2009-09-07 14:00:57

C#抓取网页

2009-09-03 09:44:02

DropDownLisC#递归

2009-08-12 16:26:30

C#读取XML文档

2009-09-04 16:18:09

C# MSNMSN Messeng

2009-08-13 18:15:06

C#继承构造函数

2009-08-28 16:03:15

C#程序实现鼠标移动

2009-08-28 15:57:56

C#线程传递参数

2009-08-21 09:20:44

C#异步套接字

2009-09-09 11:29:32

C# TextBox事

2009-08-25 15:52:27

C#工具栏

2009-09-07 05:31:39

C#窗体关闭事件

2009-08-26 09:48:48

C#异步套接字

2009-09-11 12:41:41

C#类型转换
点赞
收藏

51CTO技术栈公众号