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

C#關(guān)機(jī)代碼的實(shí)現(xiàn)淺析

開發(fā) 后端
C#關(guān)機(jī)代碼是如何實(shí)現(xiàn)的呢?當(dāng)我們?cè)趯?shí)際開發(fā)的過程中遇見這樣的需求的時(shí)候我們是如何把握的呢?那么本文就向你詳細(xì)介紹C#關(guān)機(jī)代碼的實(shí)現(xiàn)過程。

C#關(guān)機(jī)代碼的實(shí)現(xiàn)是如何的呢?我們經(jīng)常會(huì)碰到這樣的需求,那么在這方面我們是如何實(shí)現(xiàn)的呢?讓我們還是看看實(shí)際的代碼操作,希望對(duì)于你C#關(guān)機(jī)代碼的開發(fā)有所啟示。

C#關(guān)機(jī)代碼實(shí)現(xiàn)實(shí)例:(支持Win XP下的關(guān)機(jī))

  1. using System.Runtime.InteropServices;  
  2. //C#關(guān)機(jī)代碼  
  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#關(guān)機(jī)代碼  
  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#關(guān)機(jī)代碼  
  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#關(guān)機(jī)代碼  
  59. //點(diǎn)擊按鈕,執(zhí)行關(guān)機(jī)事件  
  60. private void button1_Click(object sender, System.EventArgs e)  
  61. {  
  62.  DoExitWin(1);  
  63. }  

C#關(guān)機(jī)代碼的實(shí)現(xiàn)過程就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#關(guān)機(jī)代碼有所幫助。

【編輯推薦】

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

2009-09-02 17:12:06

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

2009-09-07 09:36:29

C# DisposeDispose方法

2009-08-26 09:54:45

C#打印預(yù)覽C#打印

2009-09-01 18:29:24

C#實(shí)現(xiàn)多個(gè)接口

2009-08-31 16:48:02

C#實(shí)現(xiàn)IDispos

2009-09-02 15:34:37

C#實(shí)現(xiàn)插件構(gòu)架

2009-09-07 14:00:57

C#抓取網(wǎng)頁

2009-09-03 09:44:02

DropDownLisC#遞歸

2009-08-27 18:09:49

C#接口的實(shí)現(xiàn)

2009-08-12 16:26:30

C#讀取XML文檔

2009-09-04 16:18:09

C# MSNMSN Messeng

2009-08-13 18:15:06

C#繼承構(gòu)造函數(shù)

2009-08-28 16:03:15

C#程序?qū)崿F(xiàn)鼠標(biāo)移動(dòng)

2009-08-28 15:57:56

C#線程傳遞參數(shù)

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#窗體關(guān)閉事件

2009-09-11 12:41:41

C#類型轉(zhuǎn)換

2009-08-26 09:48:48

C#異步套接字
點(diǎn)贊
收藏

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