Search code examples
delphisetwindowshookex

How lock CTRL+ALT+DEL using SetWindowHookEx api?


Good afternoon,

I need lock CTRL+ALT+DEL combination using SetWindowsHookEx and today i have done a code and don't is working until now.

This code is executing in a dll ( this dll is my software ) that is injected in other process.

So, how i can adapt this code below to work?

const
WH_KEYBOARD_LL = 13;
LLKHF_ALTDOWN = $20;

type
KBDLLHOOKSTRUCT = record
vkCode: DWORD;
scanCode: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: Longint ;
end;

var
hhkLowLevelKybd : HHOOK;
FoldProc : LongInt;
hSASWnd : HWND;
hThread : Cardinal;

{$R *.dfm}

Function LowLevelKeyboardProc(nCode : Integer; wParam : Longint; var LParam: KBDLLHOOKSTRUCT) : Longint; stdcall;
var
fEatKeystroke : Boolean;
dwThreadId : Cardinal;
begin

If (nCode = HC_ACTION) Then
begin
If (wParam = WM_KEYDOWN) Or
(wParam = WM_SYSKEYDOWN) Or
(wParam = WM_KEYUP) Or
(wParam = WM_SYSKEYUP) Then
begin
fEatKeystroke :=
(((GetKeyState(VK_CONTROL) And $8000) <> 0) And
((LParam.flags And LLKHF_ALTDOWN ) <> 0) And
 (LParam.vkCode = VK_DELETE));

End;

If fEatKeystroke Then
Result := -1
Else
Result := CallNextHookEx(0, nCode, wParam, LongInt(@LParam));
End;

end;

////////// FormCreate event here ///////////

hhkLowLevelKybd := 0;
hhkLowLevelKybd := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardProc,
HInstance, 0);

end.

Solution

  • Windows does not allow you to intercept Ctrl+Alt+Del for security reasons. Earlier versions (pre-Vista?) used to allow it by replacing the GINA DLL, but it's not been allowed for years.

    That key combination is known as a secure attention sequence which is guaranteed to be trustworthy as part of the login process.

    If your goal is to only allow your application to be run, you can configure it to act in kiosk mode if you're running a suitable version of Windows, as shown in Set up a device for anyone to use (kiosk mode) at TechNet which @LURD kindly provided.