Tuesday, December 9, 2008

Preventing user from showing Task Manager Using VB.NET

Create a form called form1 and make a CheckBox called chkDisableCtr and a button called btnApply in order for it to work.

Imports Microsoft.Win32

Public Class Form1

Private _reg As New Form1.TaskManager

Private Sub chkDisableCtr_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDisableCtr.CheckedChanged
Me.btnApply.Enabled = True
End Sub

Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
'write the code to disable/enable the Ctrl+Alt+Deleted combination on Win2k/XP
_reg.SetTaskManager(CType(IIf(chkDisableCtr.Checked, TaskManager.TaskManagerState.Disabled, _
TaskManager.TaskManagerState.Enabled), TaskManager.TaskManagerState))
'disable the button
btnApply.Enabled = False
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
_reg.Dispose()
End Sub

Private Sub Loaded(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
chkDisableCtr.Checked = _reg.GetTaskManagerState = TaskManager.TaskManagerState.Disabled
End Sub

Private Class TaskManager

Implements IDisposable

Public Enum TaskManagerState As Integer
Disabled = 1
Enabled = 0
End Enum

Private _hkcu As RegistryKey = Registry.CurrentUser

Private Const _subKey As String = "Software\Microsoft\Windows\CurrentVersion\Policies\System"

Public Sub SetTaskManager(ByVal _state As TaskManagerState)
Dim reg As RegistryKey = _hkcu.OpenSubKey(_subKey, True)
'if we got nothing, and we are supposed to be disabling it, create the key
If reg Is Nothing AndAlso _state = TaskManagerState.Disabled Then
reg = _hkcu.CreateSubKey(_subKey)
ElseIf reg Is Nothing AndAlso _state = TaskManagerState.Enabled Then
'only come here if we are enabling. we don't need to create the key
reg = _hkcu.CreateSubKey(_subKey)
Exit Sub
End If
'change the valie...
reg.SetValue("DisableTaskMgr", CInt(_state))
End Sub

Public Function GetTaskManagerState() As TaskManagerState
Dim _val As Integer = -1
Dim _reg As RegistryKey = _hkcu.OpenSubKey(_subKey)
'if we got nothing then the task manager is enabled
If _reg Is Nothing Then
Return TaskManagerState.Enabled
Else
_val = CInt(_reg.GetValue("DisableTaskMgr"))
End If
'if we got here there was a value and we need to decode it...
'a value of 1 indicates a disbled task manager...
Return CType(IIf(_val = 1, TaskManagerState.Disabled, TaskManagerState.Enabled), TaskManagerState)

End Function

Protected Overrides Sub Finalize()
Me.Dispose()
MyBase.Finalize()
End Sub

Public Sub Dispose() Implements System.IDisposable.Dispose
Try
_hkcu.Close()
_hkcu = Nothing
GC.SuppressFinalize(Me)
Catch
'you shouldn't have dropped it in the first place
End Try
End Sub
End Class

End Class

No comments: