To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > API

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Jan 18th, 2002, 01:44 PM   #1
OhYeahLach
Member
 
Join Date: Jan 01
Location: Chicago
Posts: 40
OhYeahLach is an unknown quantity at this point (<10)
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. Public Const HKEY_CLASSES_ROOT = &H80000000
  3. Public Const HKEY_CURRENT_USER = &H80000001
  4. Public Const HKEY_LOCAL_MACHINE = &H80000002
  5. Public Const HKEY_USERS = &H80000003
  6. Public Const HKEY_PERFORMANCE_DATA = &H80000004
  7. Public Const HKEY_CURRENT_CONFIG = &H80000005
  8. Public Const HKEY_DYN_DATA = &H80000006
  9. Public Const REG_SZ = 1                         ' Unicode nul terminated string
  10. Public Const REG_BINARY = 3                     ' Free form binary
  11. Public Const REG_DWORD = 4                      ' 32-bit number
  12. Public Const ERROR_SUCCESS = 0&
  13. Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  14. Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  15. Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  16. Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  17. Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
  18. '--------------------------------------------------
  19. 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
  20. 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
  21. '--------------------------------------------------
  22. 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
  23. 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
  24. Private Const SPI_SETSCREENSAVEACTIVE = 17
  25. Private Const SPI_GETSCREENSAVEACTIVE = 16
  26. Private Const SPIF_SENDWININICHANGE = &H2
  27. Private Const SPIF_UPDATEINIFILE = &H1
  28. Private Declare Function SystemParametersInfo Lib "user32" Alias _
  29.     "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, _
  30.     ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
  31.    
  32.     'Used to lock workstation
  33. Public Declare Function GetDesktopWindow Lib "user32" () As Long
  34. 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
  35. Public Const WM_SYSCOMMAND As Long = &H112&
  36. Public Const SC_SCREENSAVE As Long = &HF140&
  37. Public Sub CreateKey(hKey As Long, strPath As String)
  38. Dim hCurKey As Long
  39. Dim lRegResult As Long
  40. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  41. If lRegResult <> ERROR_SUCCESS Then
  42.   ' there is a problem
  43. End If
  44. lRegResult = RegCloseKey(hCurKey)
  45. End Sub
  46. Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
  47. Dim hCurKey As Long
  48. Dim lValueType As Long
  49. Dim strBuffer As String
  50. Dim lDataBufferSize As Long
  51. Dim intZeroPos As Integer
  52. Dim lRegResult As Long
  53. ' Set up default value
  54. If Not IsEmpty(Default) Then
  55.   GetSettingString = Default
  56. Else
  57.   GetSettingString = ""
  58. End If
  59. ' Open the key and get length of string
  60. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
  61. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
  62. If lRegResult = ERROR_SUCCESS Then
  63.   If lValueType = REG_SZ Then
  64.     ' initialise string buffer and retrieve string
  65.     strBuffer = String(lDataBufferSize, " ")
  66.     lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
  67.    
  68.     ' format string
  69.     intZeroPos = InStr(strBuffer, Chr$(0))
  70.     If intZeroPos > 0 Then
  71.       GetSettingString = Left$(strBuffer, intZeroPos - 1)
  72.     Else
  73.       GetSettingString = strBuffer
  74.     End If
  75.   End If
  76. Else
  77.   ' there is a problem
  78. End If
  79. lRegResult = RegCloseKey(hCurKey)
  80. End Function
  81. Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
  82. Dim hCurKey As Long
  83. Dim lRegResult As Long
  84. lRegResult = RegCreateKey(hKey, strPath, hCurKey)
  85. lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
  86. If lRegResult <> ERROR_SUCCESS Then
  87.   'there is a problem
  88. End If
  89. lRegResult = RegCloseKey(hCurKey)
  90. End Sub
  91. Sub Main()
  92. 'This checks the registry to see if there is a current screen saver
  93. If GetSettingString(HKEY_CURRENT_USER, "Control Panel\Desktop", "SCRNSAVE.EXE") = "" Then
  94.     SaveSettingString HKEY_CURRENT_USER, "Control Panel\Desktop", "SCRNSAVE.EXE", "C:\WINNT4.00\black16.scr"
  95. End If
  96. 'This changes the registry to set screensaver to active
  97. SaveSettingString HKEY_CURRENT_USER, "Control Panel\Desktop", "ScreenSaveActive", "1"
  98. 'This changes the screen save to be password protected
  99. SaveSettingString HKEY_CURRENT_USER, "Control Panel\Desktop", "ScreenSaverIsSecure", "1"
  100. 'This changes the screensaver to the current screensaver registries
  101. SetScreenSaverState True, True
  102. 'Locks the workstation
  103. LockWorkStation
  104. End Sub
  105. Function SetScreenSaverState(ByVal enabled As Boolean, _
  106.     Optional ByVal PermanentChange As Boolean) As Boolean
  107.     Dim fuWinIni As Long
  108.     If PermanentChange Then
  109.         fuWinIni = SPIF_SENDWININICHANGE Or SPIF_UPDATEINIFILE
  110.     End If
  111.     SetScreenSaverState = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, enabled, _
  112.         ByVal 0&, fuWinIni) <> 0
  113. End Function
  114. Public Sub LockWorkStation()
  115.     Dim hwnd As Long
  116.     Dim nRet As Long
  117.    
  118.     'Get Desktop handle
  119.     hwnd = GetDesktopWindow()
  120.    
  121.     'Start screensaver
  122.     nRet = SendMessage(hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0&)
  123.    
  124. End Sub
OhYeahLach is offline   Reply With Quote
Old Jan 18th, 2002, 02:20 PM   #2
Hack
Super Moderator
 
Hack's Avatar
 
Join Date: Aug 01
Location: Sterling Heights, Michigan
Posts: 52,309
Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)
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?
__________________
Please use [Code]your code goes in here[/Code] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum.

Creating A Wizard In VB.NET
Modifications Required For VB6 Apps To Work On Vista
Paging A Recordset
What is wrong with using On Error Resume Next
IT professionals freelancer site. Register today to find coders, or offer your services for available freelance projects!
Microsoft MVP 2005/2006/2007/2008/2009
Hack is offline   Reply With Quote
Old Jan 18th, 2002, 04:02 PM   #3
OhYeahLach
Member
 
Join Date: Jan 01
Location: Chicago
Posts: 40
OhYeahLach is an unknown quantity at this point (<10)
I only tried it on the workstation.
OhYeahLach is offline   Reply With Quote
Old Jan 21st, 2002, 08:21 AM   #4
hgroot
Member
 
hgroot's Avatar
 
Join Date: Dec 01
Location: Amsterdam
Posts: 52
hgroot is an unknown quantity at this point (<10)
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.
hgroot is offline   Reply With Quote
Old Jan 21st, 2002, 11:27 AM   #5
JoshT
Black Cat
 
JoshT's Avatar
 
Join Date: Nov 00
Location: WNY, USA
Posts: 4,032
JoshT is on a distinguished road (20+)
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.
JoshT is offline   Reply With Quote
Old Jan 22nd, 2002, 07:47 AM   #6
Hack
Super Moderator
 
Hack's Avatar
 
Join Date: Aug 01
Location: Sterling Heights, Michigan
Posts: 52,309
Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)Hack has much to be proud of (1500+)
I believe the LockWorkStation API also works under NT4.
__________________
Please use [Code]your code goes in here[/Code] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum.

Creating A Wizard In VB.NET
Modifications Required For VB6 Apps To Work On Vista
Paging A Recordset
What is wrong with using On Error Resume Next
IT professionals freelancer site. Register today to find coders, or offer your services for available freelance projects!
Microsoft MVP 2005/2006/2007/2008/2009
Hack is offline   Reply With Quote
Old Jan 22nd, 2002, 08:11 AM   #7
hgroot
Member
 
hgroot's Avatar
 
Join Date: Dec 01
Location: Amsterdam
Posts: 52
hgroot is an unknown quantity at this point (<10)
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.
hgroot is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > API


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:12 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.