Hi,

I'm making a system where only the supervisors can use the Ctrl+Alt+Del to bring up the menu (Vista / Windows 7) or start taskmanager (XP)

Since my application will not be running on anything less than XP I don't need to worry about 95, 98, ME, 2000, etc.

I have this code so far (found on here and a little edit by me) but the Ctrl + Alt + Del doesn't get stopped like I thought it would.

Note to use this from within Visual Studio you might have to untick the box for "Enable the Visual Studio hosting proccess". In VS 2010 it is found in Solution Explorer > My Project > Debug > Enable Debugers > Enable the Visual Studio hosting proccess

I've found several ways to stop taskmanager loading but not to stop the menu being displayed.

This code is working except for my little problem with the Ctrl Alt Del menu and has been tested by me with VS 2010 .NET4 on Windows 7

Form1.vb

Public Class Form1
    Private Sub Form1_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleCreated
        KeyboardJammer.Jam()
    End Sub

    Private Sub Form1_HandleDestroyed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleDestroyed
        KeyboardJammer.UnJam()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'I belive some Windows use this one.
        StopApp.stopApplication("taskman")
        'Windows 7
        StopApp.stopApplication("taskmgr")
    End Sub
End Class

KeyboardJammer.vb

Imports System.Runtime.InteropServices
Imports System.Reflection

Public Class KeyboardJammer

    Private Delegate Function HookCallback(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    Private Shared HookDelegate As HookCallback
    Private Shared HookId As Integer
    Private Const Wh_Keyboard_LL As Integer = 13
    Private Const Vk_Tab As Integer = 9
    Private Const Vk_Escape As Integer = 27
    Private Const vk_Del As Integer = 46
    Private Const Vk_F4 As Integer = 115
    Private Const VK_LWinKey As Integer = 91
    Private Const VK_RWinKey As Integer = 92

    Private Shared Function KeyBoardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
        'All keyboard events will be sent here.

        'Don't process just pass along.
        If nCode < 0 Then
            Return CallNextHookEx(HookId, nCode, wParam, lParam)
        End If

        'Extract the keyboard structure from the lparam
        'This will contain the virtual key and any flags.
        'This is using the my.computer.keyboard to get the
        'flags instead
        Dim KeyboardSruct As KBDLLHOOKSTRUCT = Marshal.PtrToStructure(lParam, GetType(KBDLLHOOKSTRUCT))

        'MsgBox(KeyboardSruct.vkCode.ToString, MsgBoxStyle.OkOnly)

        If KeyboardSruct.vkCode = Vk_Tab And My.Computer.Keyboard.AltKeyDown Then
            'Alt Tab
            Return 1
        ElseIf KeyboardSruct.vkCode = Vk_Escape And My.Computer.Keyboard.CtrlKeyDown Then
            'Control Escape
            Return 1
        ElseIf KeyboardSruct.vkCode = VK_LWinKey Or KeyboardSruct.vkCode = VK_RWinKey Then
            'Left Windows Key o Rigth Windows Key
            Return 1
        ElseIf KeyboardSruct.vkCode = vk_Del And My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.AltKeyDown Then
            'Ctrl Alt Del
            Return 1
        ElseIf KeyboardSruct.vkCode = Vk_F4 And My.Computer.Keyboard.AltKeyDown Then
            'Alt F4
            Return 1
        End If

        'Send the message along 
        Return CallNextHookEx(HookId, nCode, wParam, lParam)

    End Function

    Public Shared Sub Jam()
        'Add the low level keyboard hook
        If HookId = 0 Then
            HookDelegate = AddressOf KeyBoardHookProc
            HookId = SetWindowsHookEx(Wh_Keyboard_LL, HookDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)

            If HookId = 0 Then
                'error
            End If

        End If
    End Sub

    Public Shared Sub UnJam()
        'Remove the hook
        UnhookWindowsHookEx(HookId)
    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
    Private Shared Function CallNextHookEx( _
       ByVal idHook As Integer, _
       ByVal nCode As Integer, _
       ByVal wParam As IntPtr, _
       ByVal lParam As IntPtr) As Integer
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
    Private Shared Function SetWindowsHookEx( _
           ByVal idHook As Integer, _
           ByVal HookProc As HookCallback, _
           ByVal hInstance As IntPtr, _
           ByVal wParam As Integer) As Integer
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
    Private Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Integer
    End Function

    Private Structure KBDLLHOOKSTRUCT
        Public vkCode As Integer
        Public scanCode As Integer
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As IntPtr
    End Structure

End Class

StopApp.vb

Public Class StopApp

    Public Shared Sub stopApplication(ByVal AppName As String)
        For Each selProcess As Process In Process.GetProcesses
            If selProcess.ProcessName = AppName Then
                selProcess.Kill()
                Exit For
            End If
        Next
    End Sub

End Class

it actually sounds more like malwar

it actually sounds more like malwar

jlego. Yea I can see your point, but the system I'm making is a POS so I can't have the "general public" leaning over the counter and stopping the application by what ever means, i don't want the "simple users" or shop assistants being able to stop the application by what ever means, and I don't want anyone except the "super users" or supervisors to be able to access the main system.

I have the login system for these "simple users" or "power users" sorted from within the application and when the power user logs in it stops all the security thats in place, when that user logs out again the security systems are started again.

There has to be a "legal" way to do what I need to do as some Microsoft applications do what I'm trying to do.
By "legal" I mean a non malware way not "legal" in a lawfull sence lol.

So there you go. Not malware lol.

Instead of disabling Control+Alt+Delete, you can just disable the Task Manager.
In HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System, you can add a "DisableTaskMgr" REG_DWORD with "1" as its value. Also, take a look at gpedit.msc -> User Configuration -> Administrative Templates -> System -> Ctrl+Alt+Delete -> Remove Task Manager.

Of course, you need to make sure that regedit and gpedit are not available (also make sure that something like typing "REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 0 /f" in Run does not work).

Also, take a look at gpedit.msc -> User Configuration -> Administrative Templates -> System -> Ctrl+Alt+Delete -> Remove Task Manager.Of course, you need to make sure that regedit and gpedit are not available (also make sure that something like typing "REG add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 0 /f" in Run does not work).

Thanks. I'll be taking a look at doing that after dinner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.