TaskKeyMgr.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ////////////////////////////////////////////////////////////////
  2. // MSDN Magazine -- September 2002
  3. // If this code works, it was written by Paul DiLascia.
  4. // If not, I don't know who wrote it.
  5. // Compiles with Visual Studio 6.0 and Visual Studio .NET on Windows XP.
  6. //
  7. #include "StdAfx.h"
  8. #include "TaskKeyMgr.h"
  9. #define HKCU HKEY_CURRENT_USER
  10. // Magic registry key/value for "Remove Task Manager" policy.
  11. //
  12. LPCTSTR KEY_DisableTaskMgr =
  13. "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
  14. LPCTSTR VAL_DisableTaskMgr = "DisableTaskMgr";
  15. //////////////////
  16. // Disable task-key related stuff.
  17. //
  18. // dwFlags = what to disable
  19. // bDisable = disable (TRUE) or enable (FALSE)
  20. // bBeep = whether to beep for illegal keys (TASKKEYS only)
  21. //
  22. void CTaskKeyMgr::Disable(DWORD dwFlags, BOOL bDisable, BOOL bBeep)
  23. {
  24. // task manager (Ctrl+Alt+Del)
  25. if (dwFlags & TASKMGR)
  26. {
  27. HKEY hk;
  28. if (RegOpenKey(HKCU, KEY_DisableTaskMgr,&hk)!=ERROR_SUCCESS)
  29. RegCreateKey(HKCU, KEY_DisableTaskMgr, &hk);
  30. if (bDisable)
  31. { // disable TM: set policy = 1
  32. DWORD val=1;
  33. RegSetValueEx(hk, VAL_DisableTaskMgr, NULL,
  34. REG_DWORD, (BYTE*)&val, sizeof(val));
  35. } else
  36. { // enable TM: remove policy
  37. RegDeleteValue(hk,VAL_DisableTaskMgr);
  38. }
  39. RegCloseKey(hk);
  40. }
  41. // task keys (Alt-TAB etc)
  42. if (dwFlags & TASKKEYS)
  43. ::DisableTaskKeys(bDisable,bBeep); // install keyboard hook
  44. // task bar
  45. if (dwFlags & TASKBAR) {
  46. HWND hwnd = FindWindow("Shell_traywnd", NULL);
  47. EnableWindow(hwnd, !bDisable);
  48. }
  49. }
  50. BOOL CTaskKeyMgr::IsTaskBarDisabled()
  51. {
  52. HWND hwnd = FindWindow("Shell_traywnd", NULL);
  53. return IsWindow(hwnd) ? !IsWindowEnabled(hwnd) : TRUE;
  54. }
  55. BOOL CTaskKeyMgr::IsTaskMgrDisabled()
  56. {
  57. HKEY hk;
  58. if (RegOpenKey(HKCU, KEY_DisableTaskMgr, &hk)!=ERROR_SUCCESS)
  59. return FALSE; // no key ==> not disabled
  60. DWORD val=0;
  61. DWORD len=4;
  62. BOOL ret = RegQueryValueEx(hk, VAL_DisableTaskMgr,
  63. NULL, NULL, (BYTE*)&val, &len)==ERROR_SUCCESS && val==1;
  64. RegCloseKey(hk);
  65. return ret;
  66. }