Results 1 to 3 of 3

Thread: How to lock a computer half-decently

  1. #1

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    How to lock a computer half-decently

    This guide tells you how to create a half-decent vb7 computer locking programme which:

    • Disables the Task Manager
    • Disables ALT-TAB
    • Disables Window Key
    • Disables the Cursor


    First things first, you have to make a form, with a textbox on it for the password, and remove the borders. Set the Form to Maximise at start, add a timer, and then we can start the code. You can set the timer to any intervale you want, but it works much better in the range 1-10
    This code is used to make the locker.

    VB Code:
    1. Option Explicit
    2. Sub Form_Load
    3.  
    4. App.TaskVisible = False
    5. 'this line prevents the app from showing in the app pane of Task Manager
    6.  
    7. Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    8. 'This part is fairly standard - it ensures that the form always stays on top.
    9.  
    10. Module1.Hook
    11. 'This starts a hook which is explained down a bit.
    12.  
    13. ShowCursor (False)
    14. 'this hides the cursor
    15.  
    16. Module1.SendKey (VK_TAB)
    17. 'this line is optional. but it selects your textbox when it loads.
    18. End Sub
    19.  
    20. Private Sub Timer1_Timer()
    21.  
    22.     SetForegroundWindow (Me.hwnd)
    23. 'this part makes sure that your screen is the foreground window every tick
    24.  
    25. Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    26. 'this repeats the topmost call, and is the code that hides task manager as soon as it loads, and activates your form.
    27.  
    28.  
    29.  
    30.   Call keybd_event(VK_LWIN, MapVirtualKey(VK_LWIN, 0),KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0)
    31. 'this line sets the windows key to up, even if it isn't down.
    32.  
    33. Call keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), _
    34.      KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0)
    35.      Call keybd_event(VK_ESCAPE, MapVirtualKey(VK_ESCAPE, 0), _
    36.      KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0)
    37. 'these two lines assist in the code but are not definetly needed, they just capture and prevent the esc and alt keys inside your app.
    38.  
    39. Call SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    40. ' i call the topmost command again, just to ensure that the task manager didn't show while the code was running
    41. End Sub
    42.  
    43. Private Sub passbox_Change()
    44. If passbox.Text = pass Then
    45.     ShowCursor (True)
    46. 'reshows the cursor
    47.     Module1.Unhook
    48. 'removes the hook
    49.     Unload Me
    50. 'closes the form
    51. End If
    52.  
    53. End Sub
    54.  
    55. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    56. If Not Text1.Text = pass Then
    57.     Cancel = -1
    58. ' this line of code prevents the app from closing on most occasions.  But not all, so i use a hook later.
    59. End If


    Then this is a module to run the hook as well as types and declarations.

    VB Code:
    1. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    2.                 ByVal hwnd As Long, _
    3.                 ByVal nIndex As Long) As Long
    4.  
    5. Private Declare Function SetLayeredWindowAttributes Lib "user32" ( _
    6.                 ByVal hwnd As Long, _
    7.                 ByVal crKey As Long, _
    8.                 ByVal bAlpha As Byte, _
    9.                 ByVal dwFlags As Long) As Long
    10. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As rect) As Long
    11. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    12. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
    13.   ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    14. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    15. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    16. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    17. Const SW_HIDE = 0
    18. Const SW_NORMAL = 1
    19. Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
    20. Private Const HWND_TOPMOST = -1
    21. Private Const HWND_NOTOPMOST = -2
    22. Private Const SWP_NOMOVE = &H2
    23. Private Const SWP_NOSIZE = &H1
    24. Private Const GWL_STYLE = (-16)
    25. Private Const GWL_EXSTYLE = (-20)
    26. Private Const WS_EX_LAYERED = &H80000
    27. Private Const LWA_COLORKEY = &H1
    28. Private Const LWA_ALPHA = &H2
    29. Const MAX_TOOLTIP As Integer = 64
    30. Const NIF_ICON = &H2
    31. Const NIF_MESSAGE = &H1
    32. Const NIF_TIP = &H4
    33. Const NIM_ADD = &H0
    34.  Const NIM_DELETE = &H2
    35.  Const WM_MOUSEMOVE = &H200
    36. Const WM_LBUTTONDOWN = &H201
    37. Const WM_LBUTTONUP = &H202
    38. Const WM_LBUTTONDBLCLK = &H203
    39. Const WM_RBUTTONDOWN = &H204
    40. Const WM_RBUTTONUP = &H205
    41. Const WM_RBUTTONDBLCLK = &H206
    42. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    43. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    44. Dim endmsg As MSG
    45. Public pass As String
    46. Private Const VK_LWIN = &H5B  '91    'Left Windows key = &HMicrosoft® Natural® keyboard  '
    47. Private Const VK_ESCAPE = &H1B  '27  'ESC key
    48. Private Const VK_SHIFT = &H10  '16   'SHIFT key
    49. Private Const VK_CONTROL = &H11  '17 'CTRL key
    50. Private Const VK_MENU = &H12  '18    'ALT key
    51. Private Const VK_RBUTTON = &H2  '2   'Right mouse button
    52. Private Const VK_APPS = &H5D  '93    'Applications key = &HNatura
    53. Const VK_TAB = &H9
    54. Private Declare Sub keybd_event Lib "user32" _
    55.   (ByVal bVk As Byte, ByVal bScan As Byte, _
    56.   ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    57.  
    58. Private Declare Function MapVirtualKey Lib "user32" _
    59.    Alias "MapVirtualKeyA" _
    60.   (ByVal wCode As Long, ByVal wMapType As Long) As Long
    61.  
    62. Private Const KEYEVENTF_EXTENDEDKEY = &H1
    63. Private Const KEYEVENTF_KEYUP = &H2
    64.  
    65. Public Const WM_NULL = &H0
    66. Public Const WM_CREATE = &H1
    67. Public Const WM_DESTROY = &H2
    68. Public Const WM_MOVE = &H3
    69. Public Const WM_SIZE = &H5
    70. Type rect
    71.     left As Long
    72.     right As Long
    73.     Top As Long
    74.     Bottom As Long
    75. End Type
    76.  
    77. Type POINTAPI
    78.     X As Long
    79.     Y As Long
    80. End Type
    81. Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
    82. Declare Function GetFocus Lib "user32" () As Long
    83. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    84.     (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal MSG As Long, _
    85.     ByVal wParam As Long, ByVal lParam As Long) As Long
    86.     Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
    87. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    88.     (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    89.     Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
    90. Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    91. Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
    92. Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    93. Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    94. Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    95. Type MSG
    96.     hwnd    As Long
    97.     message As Long
    98.     wParam  As Long
    99.     lParam  As Long
    100.     time    As Long
    101.     pt      As POINTAPI
    102. End Type
    103. Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
    104. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    105. Public Const GWL_WNDPROC = -4
    106. Public Const WM_QUERYENDSESSION = &H11
    107. Public Const WM_CANCELMODE = &H1F
    108.  
    109. Public SDAttempt        As Long
    110. Global lpPrevWndProc    As Long
    111. Global gHW              As Long
    112. Const VK_H = 72
    113. Const VK_E = 69
    114. Const VK_L = 76
    115. Const VK_O = 79
    116. Const KEYEVENTF_KEYUP = &H2
    117. Const INPUT_MOUSE = 0
    118. Const INPUT_KEYBOARD = 1
    119. Const INPUT_HARDWARE = 2
    120.  
    121. Type MOUSEINPUT
    122.   dx As Long
    123.   dy As Long
    124.   mouseData As Long
    125.   dwFlags As Long
    126.   time As Long
    127.   dwExtraInfo As Long
    128. End Type
    129.  Type KEYBDINPUT
    130.   wVk As Integer
    131.   wScan As Integer
    132.   dwFlags As Long
    133.   time As Long
    134.   dwExtraInfo As Long
    135. End Type
    136. Type HARDWAREINPUT
    137.   uMsg As Long
    138.   wParamL As Integer
    139.   wParamH As Integer
    140. End Type
    141.  Type GENERALINPUT
    142.   dwType As Long
    143.   xi(0 To 23) As Byte
    144. End Type
    145.  
    146. Dim sSql    As String
    147.  
    148.  
    149. 'Holds on
    150. Public Sub Hook()
    151. 'starts the hook
    152.     lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
    153. End Sub
    154.  
    155. 'Lets close
    156. Public Sub Unhook()
    157. 'ends the hook
    158.     Dim temp    As Long
    159.     temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
    160. End Sub
    161.  
    162. Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
    163.     ByVal wParam As Long, ByVal lParam As Long) As Long
    164.    
    165.     Dim a As Long
    166.    
    167.     If uMsg = WM_QUERYENDSESSION Then
    168.         MsgBox "Logoff/Shutdown not permitted", vbSystemModal + vbCritical
    169.         Hook
    170.     End If
    171.    
    172.     WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
    173.    
    174. End Function
    175.  
    176. ' this sub is used to send the tab key at the form_load
    177. Public Sub SendKey(bKey As Byte)
    178.     Dim GInput(0 To 1) As GENERALINPUT
    179.     Dim KInput As KEYBDINPUT
    180.     KInput.wVk = bKey  'the key we're going to press
    181.     KInput.dwFlags = 0 'press the key
    182.     'copy the structure into the input array's buffer.
    183.     GInput(0).dwType = INPUT_KEYBOARD   ' keyboard input
    184.     CopyMemory GInput(0).xi(0), KInput, Len(KInput)
    185.     'do the same as above, but for releasing the key
    186.     KInput.wVk = bKey  ' the key we're going to realease
    187.     KInput.dwFlags = KEYEVENTF_KEYUP  ' release the key
    188.     GInput(1).dwType = INPUT_KEYBOARD  ' keyboard input
    189.     CopyMemory GInput(1).xi(0), KInput, Len(KInput)
    190.     'send the input now
    191.     Call SendInput(2, GInput(0), Len(GInput(0)))
    192. End Sub

    Thats it. I apologise of there is some unneccessary code, but i tend to dump a tonne of apis into a program, and then call them.

    Use this wisely. Messing up the code too much can stop you computer from functioning properly.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: How to lock a computer half-decently

    Not so sure this is a good thread..

  3. #3

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: How to lock a computer half-decently

    Meaning What? This code is not designed to be hostile. It is designed for people who might want to lock a computer if a registration key is not entered etc. Good programmers don't write viruses. If i was to include my code to lock it even when you restarted, then yes, i understand that it would be somewhat iffy, but i have not.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width