Disable Mouse & Keyboard Windows 7

Anyone know any calls to windows dll's etc. I can use to disable and enable the mouse and keyboard in windows 7 please?

The tricky part is I want to do this from a service so I want to block the input for all users not just the current user (my service is running under SYSTEM not the logged in user).
Last edited on
Last edited on
You cannot call this function directly from a windows service due to session 0 isolation.

However, from a service, you can use WTSEnumerateSessions to get all logged on user sessions, WTSQueryUserToken to get the logged on user's token, and then use CreateProcessAsUser with that token to get code running on the user's desktop.

Beware that you can't block Ctrl+Alt+Del, so the user can still use Task Manager to kill your process.
thanks for your replies.

Modoran I have created a function that does what you describe and is able to create a process for the logged in user with 'CreateProcessAsUser' but what can I call with it disable/enable the keyboard and mouse?

Something like:
start process "cmd.exe" with the parameter "rundll32.exe user32.dll,BlockInput 1"?

The blockinput call does not work for some reason though rundll32.exe user32.dll,LockWorkStation does.
Last edited on
Create another EXE that call BlockInput directly. The EXE is launched from the service on interactive logged on user behalf, using winlogon.exe access token (this bypass UAC).

This also means that you need to detect when a user is logged off snd so on, to launch your EXE again each time from the service.
Ideally I am looking for a method that does not involve creating another executable. Is there anyway to run the blockInput user32 method from the command line?

e.g.
rundll32.exe user32.dll,BlockInput 1

I know you can call its rundll32.exe user32.dll,Lockworkstation function.
Well, BlockInput ONLY works if the calling process is running. rundll32 succeedees, but quickly exits, so windows cancels BlockInput effects automatically.

I wrote a small console app that waits 20 seconds before exiting. You can integrate this code insiode your windows service, using a commmand line switch for example if you don't want a separate EXE (this will launch another process, of course).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <windows.h>
#include <conio.h>
#include <stdio.h>


int main()
{
    HINSTANCE hDLL = LoadLibraryW (L"user32.dll");
    if (hDLL == NULL)
    {
        fprintf (stderr, "Failed to load user32.dll, error code %d\n", GetLastError());
        return 1;
    }


    typedef BOOL (WINAPI *BLOCKINPUT)(BOOL);
    BLOCKINPUT pBlockInput;
    pBlockInput = (BLOCKINPUT)GetProcAddress (hDLL, "BlockInput");
    if (pBlockInput == NULL)
    {
        fprintf (stderr, "Failed to import BlockInput, error code %d\n", GetLastError());
        FreeLibrary(hDLL);
        return 1;
    }

    else
    {
        BOOL res = pBlockInput (TRUE);
        if (!res) {
           fprintf (stderr, "BlockInput failed, error code %d\n", GetLastError());
        }
        Sleep (20);
    }



    FreeLibrary(hDLL);
    _getch();
}
Topic archived. No new replies allowed.