C#关机代码实例详解

开发 后端
C#关机代码实例主要是通过使用P/Invoke技术来向你展示C#关机代码实例,希望通过实例的形式使你能够更好的掌握C#关机代码的内涵。

C#关机代码是如何执行的呢?那么这段代码主要使用的是P/Invoke技术,如果对这个技术还未有接触,请花一些时间学习一下。P/Invoke不是一个能在一篇帖子里能讲明白的东西。

C#关机代码这段代码实现所用的就是简言之,P/Invoke = Platform Invoke,就是在.NET程序中调用Windows API等非托管函数的技术。

C#关机代码实例:

  1. // 引入必要的命名空间  
  2.  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.ComponentModel;  
  6. using System.Data;  
  7. using System.Drawing;  
  8. using System.Text;  
  9. using System.Windows.Forms;  
  10. using System.Runtime.InteropServices;  
  11. // 提供DllImport等特性,是P/Invoke的关键  
  12.  
  13. //C#关机代码  
  14. namespace test  
  15. {  
  16. public partial class Form1 : Form  
  17. {  
  18. public Form1()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22.  
  23. //C#关机代码  
  24. // 这个结构体将会传递给API。使用StructLayout  
  25. //(...特性,确保其中的成员是按顺序排列的,C#编译器不会对其进行调整。  
  26.  
  27. [StructLayout(LayoutKind.Sequential, Pack = 1)]  
  28. internal struct TokPriv1Luid  
  29. {  
  30. public int Count;  
  31. public long Luid;  
  32. public int Attr;  
  33. }  
  34.  
  35. // 以下使用DllImport特性导入了所需的Windows API。  
  36.  
  37. // 导入的方法必须是static extern的,并且没有方法体。  
  38. //调用这些方法就相当于调用Windows API。  
  39.  
  40. [DllImport("kernel32.dll", ExactSpelling = true)]  
  41. internal static extern IntPtr GetCurrentProcess();  
  42.  
  43. [DllImport("advapi32.dll", ExactSpelling =   
  44. true, SetLastError = true)]  
  45. internal static extern bool OpenProcessToken(  
  46. IntPtr h, int acc, ref IntPtr phtok);  
  47.  
  48. [DllImport("advapi32.dll", SetLastError = true)]  
  49. internal static extern bool LookupPrivilegeValue  
  50. (string host, string name, ref long pluid);  
  51.  
  52. [DllImport("advapi32.dll", ExactSpelling =   
  53. true, SetLastError = true)]  
  54. internal static extern bool   
  55. AdjustTokenPrivileges(IntPtr htok, bool disall,  
  56. ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);  
  57.  
  58. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]  
  59. internal static extern bool ExitWindowsEx(int flg, int rea);  
  60.  
  61. //C#关机代码  
  62. // 以下定义了在调用WinAPI时需要的常数。  
  63. //这些常数通常可以从Platform SDK的包含文件(头文件)中找到  
  64.  
  65. internal const int SE_PRIVILEGE_ENABLED = 0x00000002;  
  66. internal const int TOKEN_QUERY = 0x00000008;  
  67. internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;  
  68. internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";  
  69. internal const int EWX_LOGOFF = 0x00000000;  
  70. internal const int EWX_SHUTDOWN = 0x00000001;  
  71. internal const int EWX_REBOOT = 0x00000002;  
  72. internal const int EWX_FORCE = 0x00000004;  
  73. internal const int EWX_POWEROFF = 0x00000008;  
  74. internal const int EWX_FORCEIFHUNG = 0x00000010;  
  75.  
  76.  
  77. // 通过调用WinAPI实现关机,主要代码再最后一行ExitWindowsEx  
  78. //这调用了同名的WinAPI,正好是关机用的。  
  79. //C#关机代码  
  80. private static void DoExitWin(int flg)  
  81. {  
  82. bool ok;  
  83. TokPriv1Luid tp;  
  84. IntPtr hproc = GetCurrentProcess();  
  85. IntPtr htok = IntPtr.Zero;  
  86. ok = OpenProcessToken(hproc,   
  87. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);  
  88. tp.Count = 1;  
  89. tp.Luid = 0;  
  90. tp.Attr = SE_PRIVILEGE_ENABLED;  
  91. ok = LookupPrivilegeValue(  
  92. null, SE_SHUTDOWN_NAME, ref tp.Luid);  
  93. ok = AdjustTokenPrivileges(  
  94. htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero);  
  95. ok = ExitWindowsEx(flg, 0);  
  96. }   
  97.  
  98. //C#关机代码  
  99. private void button1_Click(  
  100. object sender, EventArgs e)  
  101. {  
  102. if (radioButton1.Checked == true)  
  103. {  
  104. DoExitWin(EWX_SHUTDOWN);   
  105. }  
  106. else 
  107. {  
  108. Application.Exit();   
  109. }  
  110. //MessageBox.Show("2");  
  111. }  
  112. }  
  113. }  

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

【编辑推荐】

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

2009-09-02 17:24:44

C#关机代码

2009-08-20 11:01:51

C#操作内存

2009-09-11 12:31:52

C#实例详解TypeConvert

2009-08-18 10:14:19

C#插件构架

2009-08-18 17:05:08

C#操作xml文件

2009-08-28 12:47:30

C#静态方法应用

2009-08-26 11:32:37

C#打印文档

2009-09-04 18:09:12

C# Main函数

2009-09-07 05:50:59

C# Timer用法

2009-08-28 13:12:56

C#反射实例C#反射

2009-08-26 11:07:36

C#打印窗体

2009-08-21 10:13:02

C#异步初步

2009-09-02 19:12:37

C#递归

2009-09-01 11:25:08

C#读取Word文件

2009-08-26 09:22:44

C#实现打印功能

2009-09-01 10:37:51

C#项目代码C#代码规范

2009-09-07 06:48:13

C#透明窗体

2009-09-01 15:47:20

C#取整函数

2009-09-09 12:55:59

C# TextBox事

2009-08-31 09:41:05

C#反射静态方法开发
点赞
收藏

51CTO技术栈公众号