日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#關(guān)機(jī)代碼實(shí)例詳解

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

為武漢等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及武漢網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、武漢網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

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

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

 
 
 
  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. // 這個(gè)結(jié)構(gòu)體將會(huì)傳遞給API。使用StructLayout  
  25. //(...特性,確保其中的成員是按順序排列的,C#編譯器不會(huì)對(duì)其進(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的,并且沒(méi)有方法體。  
  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í)需要的常數(shù)。  
  63. //這些常數(shù)通??梢詮腜latform 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. // 通過(guò)調(diào)用WinAPI實(shí)現(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, false, ref 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ī)代碼的實(shí)現(xiàn)過(guò)程就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#關(guān)機(jī)代碼有所幫助。


分享標(biāo)題:C#關(guān)機(jī)代碼實(shí)例詳解
標(biāo)題路徑:http://www.5511xx.com/article/djciiej.html