I want my program to lock the screen. Here I took the code to lock the keyboard

unit mklu; interface uses Windows; var m_OldMHook: HHook = 0; k_OldKBHook: HHook = 0; function KbHook(code: Integer; wparam: Word; lparam: LongInt): LongInt; stdcall; procedure KeyBoardOn; procedure KeyBoardOff; function MouseHook(code: Integer; wparam: Word; lparam: LongInt): LongInt; stdcall; procedure MOUSEON; procedure MouseOff; implementation function KbHook(code: Integer; wparam: Word; lparam: LongInt): LongInt; stdcall; begin if code < 0 then Result := CallNextHookEx(k_oldKbHook, code, wparam, lparam) else Result := 1; end; // Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠ»Π°Π²Ρ‹ procedure KeyBoardOn; begin if k_OldKbHook <> 0 then begin UnHookWindowshookEx(k_OldKbHook); k_OldKbHook := 0; end; end; // Π²Ρ‹ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠ»Π°Π²Ρ‹ procedure KeyBoardOff; begin k_OldKbHook := SetWindowsHookEx(WH_KEYBOARD, @KbHook, HInstance, 0); end; function MouseHook(code: Integer; wparam: Word; lparam: LongInt): LongInt; stdcall; begin if code < 0 then Result := CallNextHookEx(m_oldMHook, code, wparam, lparam) else Result := 1; end; // Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΌΡ‹ΡˆΠΊΠΈ procedure MOUSEON; begin if m_OldMHook <> 0 then begin UnHookWindowshookEx(m_OldMHook); m_OldMHook := 0; end; end; // Π²Ρ‹ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΌΡ‹ΡˆΠΊΠΈ procedure MouseOff; begin m_OldMHook := SetWindowsHookEx(WH_MOUSE, @MOUSEHook, HInstance, 0); end; end. ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования: uses mklu; {Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠ°(LockIt = true) ΠΈΠ»ΠΈ Ρ€Π°Π·Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠ°(LockIt= False) ΠΊΠ»Π°Π²Ρ‹} procedure LockUnlockKeyboard(LockIt: Boolean); begin if LockIt then KeyBoardOFF else KeyBoardOn; end; {Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠ°(LockIt = true) ΠΈΠ»ΠΈ Ρ€Π°Π·Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠ°(LockIt= False) ΠΌΡ‹ΡˆΠΊΠΈ} procedure LockUnlockMouse(LockIt: Boolean); begin if LockIt then MouseOff else MouseOn; end; 

It works fine on Windows XP, and on Windows 7 for some reason it does not plow ... What is the problem?

  • Honestly, I don’t remember the details, I was amused for a long time, but I didn’t look at the code at all, and always returned -1 from the hook. This blocker still works for me, and under 7. I can dig out the source code, if you're interested, but C. is there. - user6550

1 answer 1

It seems to me that Win 7 is hampered by User Account Control (UAC). Disable it, and run the application as administrator.

  • Yes, I already knew that there was a problem. But turning off UAC is not an option ... I read here that a manifest had to be made ... BUT, how? I saw in here, but somehow nothing is clear what to do ... ( vmpsoft.com/forum/viewtopic.php?t=167 ) - NIXON_NB