熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#使用API屏蔽系統熱鍵和任務管理器

2022-06-13   來源: .NET編程 

  using System;

  using SystemIO;

  using SystemReflection;

  using SystemRuntimeInteropServices;

  using SystemWindowsForms;

  namespace WATPMS

  {

  /**//// <summary>

  /// Description: Hook Helper類可以屏蔽一些熱鍵並屏蔽任務管理器

  /// Author: ZhangRongHua

  /// Create DateTime: :

  /// UpdateHistory:

  /// </summary>

  public class HookHelper

  {

  Delegates#region Delegates

  public delegate int HookProc(int nCode Int wParam IntPtr lParam);

  #endregion

  變量聲明#region 變量聲明

  private HookProc KeyboardHookProcedure;

  private FileStream MyFs; // 用流來屏蔽ctrl alt delete

  private const byte LLKHF_ALTDOWN = x;

  private const byte VK_CAPITAL = x;

  private const byte VK_ESCAPE = xB;

  private const byte VK_F = x;

  private const byte VK_LCONTROL = xA;

  private const byte VK_NUMLOCK = x;

  private const byte VK_RCONTROL = xA;

  private const byte VK_SHIFT = x;

  private const byte VK_TAB = x;

  public const int WH_KEYBOARD = ;

  private const int WH_KEYBOARD_LL = ;

  private const int WH_MOUSE = ;

  private const int WH_MOUSE_LL = ;

  private const int WM_KEYDOWN = x;

  private const int WM_KEYUP = x;

  private const int WM_LBUTTONDBLCLK = x;

  private const int WM_LBUTTONDOWN = x;

  private const int WM_LBUTTONUP = x;

  private const int WM_MBUTTONDBLCLK = x;

  private const int WM_MBUTTONDOWN = x;

  private const int WM_MBUTTONUP = x;

  private const int WM_MOUSEMOVE = x;

  private const int WM_MOUSEWHEEL = xA;

  private const int WM_RBUTTONDBLCLK = x;

  private const int WM_RBUTTONDOWN = x;

  private const int WM_RBUTTONUP = x;

  private const int WM_SYSKEYDOWN = x;

  private const int WM_SYSKEYUP = x;

  private static int hKeyboardHook = ;

  #endregion

  函數轉換#region 函數轉換

  [DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]

  public static extern int SetWindowsHookEx(int idHook HookProc lpfn IntPtr hInstance int threadId);

  //   卸載鉤子

  [DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]

  public static extern bool UnhookWindowsHookEx(int idHook);

  //   繼續下一個鉤子

  [DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]

  public static extern int CallNextHookEx(int idHook int nCode Int wParam IntPtr lParam);

  //   取得當前線程編號

  [DllImport(kerneldll)]

  private static extern int GetCurrentThreadId();

  [DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]

  private static extern short GetKeyState(int vKey);

  #endregion

  方法#region 方法

  /**//// <summary>

  /// 鉤子回調函數在這裡屏蔽熱鍵

  /// <remark>

  /// Author:ZhangRongHua

  /// Create DateTime: :

  /// Update History:

  ///  </remark>

  /// </summary>

  /// <param name=nCode>The n code</param>

  /// <param name=wParam>The w param</param>

  /// <param name=lParam>The l param</param>

  /// <returns></returns>

  private int KeyboardHookProc(int nCode Int wParam IntPtr lParam)

  {

  KeyMSG m = (KeyMSG) MarshalPtrToStructure(lParam typeof (KeyMSG));

  if (((Keys) mvkCode == KeysLWin) || ((Keys) mvkCode == KeysRWin) ||

  ((mvkCode == VK_TAB) && ((mflags & LLKHF_ALTDOWN) != )) ||

  ((mvkCode == VK_ESCAPE) && ((mflags & LLKHF_ALTDOWN) != )) ||

  ((mvkCode == VK_F) && ((mflags & LLKHF_ALTDOWN) != )) ||

  (mvkCode == VK_ESCAPE) && ((GetKeyState(VK_LCONTROL) & x) != ) ||

  (mvkCode == VK_ESCAPE) && ((GetKeyState(VK_RCONTROL) & x) != )

  )

  {

  return ;

  }

  return CallNextHookEx(hKeyboardHook nCode wParam lParam);

  }

  /**//// <summary>

  /// 啟動Hook並用流屏蔽任務管理器

  /// <remark>

  /// Author:ZhangRongHua

  /// Create DateTime: :

  /// Update History:

  ///  </remark>

  /// </summary>

  public void HookStart()

  {

  if (hKeyboardHook == )

  {

  //   創建HookProc實例

  KeyboardHookProcedure = new HookProc(KeyboardHookProc);

  hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD

  KeyboardHookProcedure

  MarshalGetHINSTANCE(AssemblyGetExecutingAssembly()GetModules()[])

  );

  //   如果設置鉤子失敗

  if (hKeyboardHook == )

  {

  HookStop();

  //throw new Exception(SetWindowsHookEx   failedeeeeeeee);

  }

  //用二進制流的方法打開任務管理器而且不關閉流這樣任務管理器就打開不了

  MyFs = new FileStream(EnvironmentExpandEnvironmentVariables(%windir%\\system\\taskmgrexe)

  FileModeOpen);

  byte[] MyByte = new byte[(int) MyFsLength];

  MyFsWrite(MyByte (int) MyFsLength);

  }

  }

  /**//// <summary>

  /// 卸載hook並關閉流取消屏蔽任務管理器

  /// <remark>

  /// Author:ZhangRongHua

  /// Create DateTime: :

  /// Update History:

  ///  </remark>

  /// </summary>

  public void HookStop()

  {

  bool retKeyboard = true;

  if (hKeyboardHook != )

  {

  retKeyboard = UnhookWindowsHookEx(hKeyboardHook);

  hKeyboardHook = ;

  }

  if (null != MyFs)

  {

  MyFsClose();

  }

  if (!(retKeyboard))

  {

  throw new Exception(UnhookWindowsHookEx     failedsssss);

  }

  }

  #endregion

  Nested type: KeyMSG#region Nested type: KeyMSG

  public struct KeyMSG

  {

  public int dwExtraInfo;

  public int flags;

  public int scanCode;

  public int time;

  public int vkCode;

  }

  #endregion

  }

  }

  //PS也可以通過將[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System]

  //下的DisableTaskmgr項的值設為來屏蔽任務管理器


From:http://tw.wingwit.com/Article/program/net/201311/12339.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.