Results 1 to 7 of 7

Thread: How to lock an NT workstation when the screensaver is turned off

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Chicago
    Posts
    40

    Smile How to lock an NT workstation when the screensaver is turned off

    This took me a while to figure out, so I thought I would pass it on.
    VB Code:
    1. Option Explicit
    2.  
    3. Public Const HKEY_CLASSES_ROOT = &H80000000
    4. Public Const HKEY_CURRENT_USER = &H80000001
    5. Public Const HKEY_LOCAL_MACHINE = &H80000002
    6. Public Const HKEY_USERS = &H80000003
    7. Public Const HKEY_PERFORMANCE_DATA = &H80000004
    8. Public Const HKEY_CURRENT_CONFIG = &H80000005
    9. Public Const HKEY_DYN_DATA = &H80000006
    10. Public Const REG_SZ = 1                         ' Unicode nul terminated string
    11. Public Const REG_BINARY = 3                     ' Free form binary
    12. Public Const REG_DWORD = 4                      ' 32-bit number
    13. Public Const ERROR_SUCCESS = 0&
    14.  
    15. Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    16. Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    17. Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    18. Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    19. Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    20. '--------------------------------------------------
    21. Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
    22. Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
    23. '--------------------------------------------------
    24. Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    25. Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    26.  
    27. Private Const SPI_SETSCREENSAVEACTIVE = 17
    28. Private Const SPI_GETSCREENSAVEACTIVE = 16
    29. Private Const SPIF_SENDWININICHANGE = &H2
    30. Private Const SPIF_UPDATEINIFILE = &H1
    31. Private Declare Function SystemParametersInfo Lib "user32" Alias _
    32.     "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, _
    33.     ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
    34.    
    35.     'Used to lock workstation
    36. Public Declare Function GetDesktopWindow Lib "user32" () As Long
    37. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    38. Public Const WM_SYSCOMMAND As Long = &H112&
    39. Public Const SC_SCREENSAVE As Long = &HF140&
    40.  
    41.  
    42.  
    43. Public Sub CreateKey(hKey As Long, strPath As String)
    44. Dim hCurKey As Long
    45. Dim lRegResult As Long
    46.  
    47. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    48.  
    49. If lRegResult <> ERROR_SUCCESS Then
    50.   ' there is a problem
    51. End If
    52.  
    53. lRegResult = RegCloseKey(hCurKey)
    54.  
    55. End Sub
    56.  
    57.  
    58.  
    59. Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
    60. Dim hCurKey As Long
    61. Dim lValueType As Long
    62. Dim strBuffer As String
    63. Dim lDataBufferSize As Long
    64. Dim intZeroPos As Integer
    65. Dim lRegResult As Long
    66.  
    67. ' Set up default value
    68. If Not IsEmpty(Default) Then
    69.   GetSettingString = Default
    70. Else
    71.   GetSettingString = ""
    72. End If
    73.  
    74. ' Open the key and get length of string
    75. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    76. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
    77.  
    78. If lRegResult = ERROR_SUCCESS Then
    79.  
    80.   If lValueType = REG_SZ Then
    81.     ' initialise string buffer and retrieve string
    82.     strBuffer = String(lDataBufferSize, " ")
    83.     lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
    84.    
    85.     ' format string
    86.     intZeroPos = InStr(strBuffer, Chr$(0))
    87.     If intZeroPos > 0 Then
    88.       GetSettingString = Left$(strBuffer, intZeroPos - 1)
    89.     Else
    90.       GetSettingString = strBuffer
    91.     End If
    92.  
    93.   End If
    94.  
    95. Else
    96.   ' there is a problem
    97. End If
    98.  
    99. lRegResult = RegCloseKey(hCurKey)
    100. End Function
    101.  
    102. Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
    103. Dim hCurKey As Long
    104. Dim lRegResult As Long
    105.  
    106. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    107.  
    108. lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    109.  
    110. If lRegResult <> ERROR_SUCCESS Then
    111.   'there is a problem
    112. End If
    113.  
    114. lRegResult = RegCloseKey(hCurKey)
    115. End Sub
    116.  
    117.  
    118.  
    119.  
    120.  
    121. Sub Main()
    122.  
    123. 'This checks the registry to see if there is a current screen saver
    124. If GetSettingString(HKEY_CURRENT_USER, "Control Panel\Desktop", "SCRNSAVE.EXE") = "" Then
    125.     SaveSettingString HKEY_CURRENT_USER, "Control Panel\Desktop", "SCRNSAVE.EXE", "C:\WINNT4.00\black16.scr"
    126. End If
    127. 'This changes the registry to set screensaver to active
    128. SaveSettingString HKEY_CURRENT_USER, "Control Panel\Desktop", "ScreenSaveActive", "1"
    129. 'This changes the screen save to be password protected
    130. SaveSettingString HKEY_CURRENT_USER, "Control Panel\Desktop", "ScreenSaverIsSecure", "1"
    131.  
    132. 'This changes the screensaver to the current screensaver registries
    133. SetScreenSaverState True, True
    134.  
    135. 'Locks the workstation
    136. LockWorkStation
    137.  
    138. End Sub
    139.  
    140. Function SetScreenSaverState(ByVal enabled As Boolean, _
    141.     Optional ByVal PermanentChange As Boolean) As Boolean
    142.     Dim fuWinIni As Long
    143.     If PermanentChange Then
    144.         fuWinIni = SPIF_SENDWININICHANGE Or SPIF_UPDATEINIFILE
    145.     End If
    146.     SetScreenSaverState = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, enabled, _
    147.         ByVal 0&, fuWinIni) <> 0
    148. End Function
    149.  
    150. Public Sub LockWorkStation()
    151.  
    152.     Dim hwnd As Long
    153.     Dim nRet As Long
    154.    
    155.     'Get Desktop handle
    156.     hwnd = GetDesktopWindow()
    157.    
    158.     'Start screensaver
    159.     nRet = SendMessage(hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0&)
    160.    
    161. End Sub

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Curiosity questions: Have you tried this code with both NT 4 Workstation and Windows 2000 Workstation?

    Will this work on only workstation, or have you tried in Server as well?

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Chicago
    Posts
    40
    I only tried it on the workstation.

  4. #4
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    I'm sorry to say I can't get the code to work correctly under NT4 workstation.

    I've been writing a program to easy logoff, reboot, etc. But I didn't get the screen locking to work, so this seemed a good solution.

    This code does not start the screen saver for me, although I have one configured with password. Nothing seems to be happening, but when I then press CTRL-ALT-DEL, I have to type my password, as if the workstation had been locked. Normally, it would show the menu with shutdown, taskmanager, etc.

    Anyway, it's a start, when I find out what caused the problem, I'll tell it here.

  5. #5
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    FYI, Windows 2000 + has a really simple to use LockWorkstation API call.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I believe the LockWorkStation API also works under NT4.

  7. #7
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    I suppose the VB declaration would be:

    Private Declare Sub LockWorkStation Lib "user32.dll" ()

    That's copied from http://www.vbforums.com/showthread.p...threadid=44075 .

    I run NT4, and this little trick doesn't work. So it really is win2000+ only.

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