欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#調(diào)用windows api關(guān)機(關(guān)機api)示例代碼分享

 更新時間:2014年01月06日 11:47:07   作者:  
本文主要介紹了C#調(diào)用windows api關(guān)機的示例代碼,大家參考使用吧

復(fù)制代碼 代碼如下:

using System;
using System.Runtime.InteropServices;

class shoutdown{

  [StructLayout(LayoutKind.Sequential, Pack=1)]
  internal struct TokPriv1Luid
  {
  public int Count;
  public long Luid;
  public int Attr;
  }
  [DllImport("kernel32.dll", ExactSpelling=true) ]
  internal static extern IntPtr GetCurrentProcess();
  [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
  internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );
  [DllImport("advapi32.dll", SetLastError=true) ]
  internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );
  [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
  internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
  ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
  [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
  internal static extern bool ExitWindowsEx( int flg, int rea );
  internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  internal const int TOKEN_QUERY = 0x00000008;
  internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
  internal const int EWX_LOGOFF = 0x00000000;
  internal const int EWX_SHUTDOWN = 0x00000001;
  internal const int EWX_REBOOT = 0x00000002;
  internal const int EWX_FORCE = 0x00000004;
  internal const int EWX_POWEROFF = 0x00000008;
  internal const int EWX_FORCEIFHUNG = 0x00000010;

  private static void DoExitWin(int flg)
  {
  bool ok;
  TokPriv1Luid tp;
  IntPtr hproc = GetCurrentProcess();
  IntPtr htok = IntPtr.Zero;
  ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
  tp.Count = 1;
  tp.Luid = 0;
  tp.Attr = SE_PRIVILEGE_ENABLED;
  ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
  ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
  ok = ExitWindowsEx( flg, 0 );
  }
  public static void Main()
  {
  Console.WriteLine("正在關(guān)閉計算機……");
  // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等實現(xiàn)不同得功能。
  // 在XP下可以看到幫助信息,以得到不同得參數(shù)
  // SHUTDOWN /?
  DoExitWin(EWX_SHUTDOWN);
  }
}

相關(guān)文章

  • C#中using語句的用法

    C#中using語句的用法

    這篇文章介紹了C#中using語句的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#面向?qū)ο笤O(shè)計的七大原則

    C#面向?qū)ο笤O(shè)計的七大原則

    這篇文章主要為大家詳細介紹了C#面向?qū)ο笤O(shè)計的七大原則,未讀文章之前大家說一說都有哪七大原則,是不是七大原則都可以全部列出,想要了解的朋友可以參考一下
    2016-05-05
  • Unity Shader實現(xiàn)新手引導(dǎo)遮罩鏤空效果

    Unity Shader實現(xiàn)新手引導(dǎo)遮罩鏤空效果

    這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)新手引導(dǎo)遮罩鏤空效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C#中使用閉包與意想不到的坑詳解

    C#中使用閉包與意想不到的坑詳解

    這篇文章主要給大家介紹了關(guān)于C#中使用閉包與意想不到的坑,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • C#使用iTextSharp添加PDF水印

    C#使用iTextSharp添加PDF水印

    這篇文章主要為大家詳細介紹了C#使用iTextSharp添加PDF水印的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • c#實現(xiàn)md5加密示例

    c#實現(xiàn)md5加密示例

    這篇文章主要介紹了md5加密,加密結(jié)果可以為32位、48位、64位,只要修改一下參數(shù)就可以實現(xiàn)
    2014-01-01
  • C#拼圖游戲編寫代碼(2)

    C#拼圖游戲編寫代碼(2)

    這篇文章主要為大家詳細介紹了C#拼圖游戲的編寫代碼下篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • C#中decimal保留2位有效小數(shù)的實現(xiàn)方法

    C#中decimal保留2位有效小數(shù)的實現(xiàn)方法

    這篇文章主要介紹了C#中decimal保留2位有效小數(shù)的實現(xiàn)方法,針對decimal變量保留2位有效小數(shù)有多種方法,可以使用Math.Round方法以及ToString先轉(zhuǎn)換為字符串等操作來實現(xiàn)。具體實現(xiàn)方法感興趣的朋友跟隨小編一起看看吧
    2019-10-10
  • C#?TaskScheduler任務(wù)調(diào)度器的實現(xiàn)

    C#?TaskScheduler任務(wù)調(diào)度器的實現(xiàn)

    本文主要介紹了C#?TaskScheduler任務(wù)調(diào)度器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2023-05-05
  • dotnet如何將文件刪除到回收站

    dotnet如何將文件刪除到回收站

    這篇文章主要給大家介紹了關(guān)于dotnet如何將文件刪除到回收站的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用dotnet具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評論