Wednesday, March 12, 2008

[C++] How to disable Windows Task Manager

Here's a kick-ass function snippet you can use to lock/unlock Task Manager.
It works setting the appropriate windows registry key. You might wanna do this when you're looking for a way to disable the Ctrl+Alt+Del combination; trapping this particular combination is not as straightforward as trapping the Alt+Tab one or similar, so you can't use the Register/UnregisterHotKey trick (that's gonna be another post).

You'll usually call this function within class contructor/destructor in order to make sure after your object is destroyed you enable back Task Manager (if that's what you want):


void LockTaskManager(bool Lock)
{
HKEY hkey;
DWORD dwDisposition;
DWORD dwType, dwSize;
DWORD value;

if (Lock)
value = 1;
else
value = 0;
if(RegCreateKeyEx(HKEY_CURRENT_USER,
TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\system"),
0,
NULL,
0,
KEY_SET_VALUE,
NULL,
&hkey,
&dwDisposition)== ERROR_SUCCESS)
{
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
RegSetValueEx(hkey, TEXT("DisableTaskMgr"), 0, dwType, (PBYTE)&value, dwSize);
RegCloseKey(hkey);
}
}
RegCreateKeyEx will open the key if existing otherwise it'll create it. The KEY_SET_VALUE parameter on the RegCreateKeyEx is necessary, otherwise you wouldn't be able to set the DisableTaskMgr DWORD value on the reg. This is tested on Win2000 and WinXP SP2.

Enough registry butchering for today.

7 comments:

Anonymous said...

does this even work?

Johnny Idol said...

@Anonymous you bet your ass it does

Prasanna Rani said...

Its working ..... thanks a lot. I was trying the same but for HEKY_LOCAL_MACHINE root. Thank you. :)

some guy said...

Cannot compile in Visual Studio C++ 2008. Illegal blaha blaha. Anyway to walk around?

Johnny Idol said...

@someGuy

this was compiled in VC++ 6 - you're better off posting on http://www.stackoverflow.com with the exact error, chances are it's something fairly trivial! Maybe post alink here in the comments so that I can keep an eye on the thread and update this post

Raúl Infantes said...

You are awesome, many thanks for code...people like you become this world much better. XD

Johnny Idol said...

@Raúl

thanks for the love, I am just your average .NET butcher ;)