Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


Setting Screensaver with VBscript


Setting Screensaver with VBscript

Author
Message
Mobius
Mobius
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 632, Visits: 1K
I'm trying to develop a script that will check to see if a screensaver is enabled, and if not, set it to the default Windows XP screensaver. So far, I've assembled quite a conglomeration of code using different sources, and I've worked out several errors, but this particular one I can't quite get past:



Option Explicit

Dim HKEY_CURRENT_USER

HKEY_CURRENT_USER= "&H80000001"

Dim objReg, strComputer, strKeyPath, strValue, ValueName

'strComputer = "."

strKeyPath = "Control Panel\Desktop"

Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")

'***set variable strkeypath***
strKeyPath = "Control Panel\Desktop"

'***set variable valuename***
ValueName = "ScreenSaveActive"

'***set variable strvalue***
strValue = "1"

objReg.CreateKey HKEY_CURRENT_USER, strKeyPath, ValueName




That last line is where I get a Type Mismatch error. Any ideas?






-Mobius

Mobius
Mobius
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 632, Visits: 1K
You know, thats why I love A1, because whenever I come across a problem I can't catch, I post my code here and FIVE seconds later, I realize my stupid mistake. In the second line:



Const HKEY_CURRENT_USER = &H80000001



The value to be assigned was previously encapsulated in quotations, and it shouldn't be. Duh.









That said, I have accomplished phase 1 of my little project. I am able to set the screensaver to whatever I want, assuming the file is in sys32. Now, I would like to only run this script if no screensaver is currently set. Does anyone know how to check for that?





-Mobius

Mark
Mark
Forum God
Forum God (139K reputation)

Group: Moderators
Posts: 1.1K, Visits: 11K
I'm not sure if this is it but new I have my screensaver set it has a "SCRNSAVE.EXE" key under "HKEY_CURRENT_USER\Control Panel\Desktop". When I set the screensaver to none that key doesn't exist.
Mobius
Mobius
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 632, Visits: 1K
Yes, sorry I forgot to close this thread when I came up with the solution....you're right Mark, and it took me a while to figure that out but I finally came up with a working script. I'll be sure to post it later.





-Mobius

Keithuk
Keithuk
Forum God
Forum God (291K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
Hi Mobius.

Mobius (8/1/2008)
I'm trying to develop a script that will check to see if a screensaver is enabled, and if not, set it to the default Windows XP screensaver.

To check if a screensaver is active then I would use. Wink

Option Explicit

Private Declare Function SystemParametersInfo Lib "user32" Alias _
"SystemParametersInfoA"  (ByVal uiAction As Long, _
ByVal uiParam As Long, pvParam As Any, ByVal fWInIni As LongAs Boolean

Const SPI_GETSCREENSAVEACTIVE As Long = &H10
Const SPI_GETSCREENSAVERRUNNING As Long = &H72

Private Sub Form_Load()

Dim bActive As Boolean

' Find out if screen saver is active, and display a suitable message.
SystemParametersInfo SPI_GETSCREENSAVEACTIVE, 0, bActive, False
If bActive Then
    MsgBox "Screen saver is active"
Else
    MsgBox "Screen saver is not active"
End If

End Sub


Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

Mobius
Mobius
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 632, Visits: 1K
Enjoy!



'*******************************************************
'* Script that ensures that a screensaver is set, *
'* for security purposes *
'* *
'* Author: Mobius *
'* *
'* Date: July 31, 2008 *
'*******************************************************


Option Explicit

On Error Resume Next

'Creating objects and variables

Const HKEY_CURRENT_USER = &H80000001

Dim WSHShell, RegKey, ScreenSaver, objReg, lastscreensaver

Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")

RegKey = "HKEY_CURRENT_USER\Control Panel\Desktop\"

objReg.SetStringValue HKEY_CURRENT_USER,RegKey,"LastScreenSaver","None"

Set WSHShell = CreateObject("WScript.Shell")

'Checks registry for current screensaver

ScreenSaver = WSHShell.RegRead (regkey & "scrnsave.exe")

'A case statement to handle an error: Acts as an indicator for whether or not a screensaver is enabled.

Select Case Err

   'If Screensaver is enabled, this case saves the current screensaver to
   'the registry as the last used screensaver.

   Case 0:

      WSHShell.RegWrite regkey & "LastScreenSaver", ScreenSaver

   'If Screensaver not enabled, this case checks to see if there's a "last used screensaver" stored. If so, it sets
   'the screensaver to this value. If not, it sets the screensaver to the default XP Screensaver.

   Case Else:

         'Clears the error code.

         Err.Clear()

         'Checks to see if a "last used screen saver has been saved.

         lastscreensaver = WSHShell.RegRead (regkey & "LastScreenSaver")
         
         'Sets the screensaver to the last used screensaver.
      
         WSHShell.RegWrite regkey & "scrnsave.exe", lastscreensaver

         'This Select Case Structure handles an error that arises if no saved screensaver exists.

         Select Case Err
         
            Case 0:
            
               'Don't do anything
         
            Case Else:
               
               'Sets the screensaver to the default XP screensaver...
               
               WSHShell.RegWrite regkey & "scrnsave.exe", "C:\WINDOWS\System32\logon.scr"
               
               '...then saves this as the last used screensaver so this error never arises again.
               
               WSHShell.RegWrite regkey & "LastScreenSaver", "C:\WINDOWS\System32\logon.scr"
            
         End Select
      
End Select

Err.Clear()

On Error Goto 0












-Mobius

Mobius
Mobius
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 632, Visits: 1K
Haha, yeah Keith, see that was part of the problem, when doing my research for this, I kept running into articles that didn't separate the terms "running" and "enabled". They both flew under the banner of "active". I didn't care if the screensaver was running. I just wanted to make sure my users weren't disabling it to try to stick it to the man.



I sure showed them....yay for scripting!





-Mobius

Keithuk
Keithuk
Forum God
Forum God (291K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
Mobius (8/4/2008)
Haha, yeah Keith, see that was part of the problem, when doing my research for this, I kept running into articles that didn't separate the terms "running" and "enabled". They both flew under the banner of "active". I didn't care if the screensaver was running.

Well thats one of your problems "running" and "enabled". There is no point in checking if its running because you will see that. All as you need to know is if its enabled then you can disable it while your program is running then enable it when it closes. Wink

Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

Edited
8/4/2008 by Keithuk
Mobius
Mobius
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 632, Visits: 1K
Yeah, well the purpose was to have a script that I could run on the 800+ machines my team admins, every morning, and if there's no screensaver turned on, then it turns one on. Preferably the last one the user selected, if such a saved setting exists. Otherwise, its the default XP scrnsaver.



Users are supposed to have the screensaver come on after 30 minutes or less, and it locks the workstation upon return. If the user is able to select "No screensaver" it makes that completely pointless. So this is kind of a fix for earlier stupidity, if you will. Smile





-Mobius

Keithuk
Keithuk
Forum God
Forum God (291K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
Well the thing you have to watch is normal users don't have admin rights to set screensavers I know I don't at work. I've been into the registry and set one but it says I don't have admin rights to do this.

I what I would recommend you do if you haven't done it already is download API-Guide its a very good guide to API calls and it has loads of example code.

Then you can do a search for SystemParametersInfo which sets the screensaver.

While you are on there you can download the updated API Viewer as well. The VB6 API Viewer is 10 years old now so it needs updating. Wink

Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search