自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#關(guān)機(jī)代碼實例詳解

開發(fā) 后端
C#關(guān)機(jī)代碼實例主要是通過使用P/Invoke技術(shù)來向你展示C#關(guān)機(jī)代碼實例,希望通過實例的形式使你能夠更好的掌握C#關(guān)機(jī)代碼的內(nèi)涵。

C#關(guān)機(jī)代碼是如何執(zhí)行的呢?那么這段代碼主要使用的是P/Invoke技術(shù),如果對這個技術(shù)還未有接觸,請花一些時間學(xué)習(xí)一下。P/Invoke不是一個能在一篇帖子里能講明白的東西。

C#關(guān)機(jī)代碼這段代碼實現(xiàn)所用的就是簡言之,P/Invoke = Platform Invoke,就是在.NET程序中調(diào)用Windows API等非托管函數(shù)的技術(shù)。

C#關(guān)機(jī)代碼實例:

  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的關(guān)鍵  
  12.  
  13. //C#關(guān)機(jī)代碼  
  14. namespace test  
  15. {  
  16. public partial class Form1 : Form  
  17. {  
  18. public Form1()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22.  
  23. //C#關(guān)機(jī)代碼  
  24. // 這個結(jié)構(gòu)體將會傳遞給API。使用StructLayout  
  25. //(...特性,確保其中的成員是按順序排列的,C#編譯器不會對其進(jìn)行調(diào)整。  
  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特性導(dǎo)入了所需的Windows API。  
  36.  
  37. // 導(dǎo)入的方法必須是static extern的,并且沒有方法體。  
  38. //調(diào)用這些方法就相當(dāng)于調(diào)用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#關(guān)機(jī)代碼  
  62. // 以下定義了在調(diào)用WinAPI時需要的常數(shù)。  
  63. //這些常數(shù)通常可以從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. // 通過調(diào)用WinAPI實現(xiàn)關(guān)機(jī),主要代碼再最后一行ExitWindowsEx  
  78. //這調(diào)用了同名的WinAPI,正好是關(guān)機(jī)用的。  
  79. //C#關(guān)機(jī)代碼  
  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#關(guān)機(jī)代碼  
  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#關(guān)機(jī)代碼的實現(xiàn)過程就向你介紹到這里,希望對你了解和學(xué)習(xí)C#關(guān)機(jī)代碼有所幫助。

【編輯推薦】

  1. C#動態(tài)創(chuàng)建數(shù)組實現(xiàn)實例解析
  2. C#動態(tài)創(chuàng)建數(shù)組詳細(xì)實現(xiàn)過程解析
  3. C#聲明數(shù)組的詳細(xì)解析
  4. 如何初始化數(shù)組詳解
  5. C#數(shù)組操作的體會淺談
責(zé)任編輯:仲衡 來源: 百度空間
相關(guān)推薦

2009-09-02 17:24:44

C#關(guān)機(jī)代碼

2009-08-20 11:01:51

C#操作內(nèi)存

2009-09-11 12:31:52

C#實例詳解TypeConvert

2009-08-18 10:14:19

C#插件構(gòu)架

2009-09-04 18:09:12

C# Main函數(shù)

2009-08-28 13:12:56

C#反射實例C#反射

2009-08-21 10:13:02

C#異步初步

2009-08-26 09:22:44

C#實現(xiàn)打印功能

2009-08-26 11:07:36

C#打印窗體

2009-09-02 19:12:37

C#遞歸

2009-09-07 05:50:59

C# Timer用法

2009-09-01 11:25:08

C#讀取Word文件

2009-08-26 11:32:37

C#打印文檔

2009-08-18 17:05:08

C#操作xml文件

2009-08-28 12:47:30

C#靜態(tài)方法應(yīng)用

2009-09-07 06:48:13

C#透明窗體

2009-09-01 10:37:51

C#項目代碼C#代碼規(guī)范

2009-09-07 06:18:57

C#窗體設(shè)計器

2009-09-02 18:44:19

C#遞歸

2009-09-02 11:18:10

C#動態(tài)數(shù)組
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號