Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Visual Basic (VB 4/5/6) » Internet/Communications Programming » Setting Screensaver with VBscript


Setting Screensaver with VBscriptExpand / Collapse
Author
Message
Posted 8/1/2008 10:43:02 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 9:26:27 AM
Posts: 541, Visits: 856
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
_____________________________________________________________
No trees were harmed in the making of this post.
However, a large number of electrons were horribly inconvenienced.
Post #25490
Posted 8/1/2008 11:23:28 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 9:26:27 AM
Posts: 541, Visits: 856
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
_____________________________________________________________
No trees were harmed in the making of this post.
However, a large number of electrons were horribly inconvenienced.
Post #25492
Posted 8/2/2008 7:55:06 PM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 4:38:01 AM
Posts: 886, Visits: 5,287
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.
Post #25495
Posted 8/3/2008 3:11:08 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 9:26:27 AM
Posts: 541, Visits: 856
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
_____________________________________________________________
No trees were harmed in the making of this post.
However, a large number of electrons were horribly inconvenienced.
Post #25496
Posted 8/3/2008 9:10:02 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 4:36:16 PM
Posts: 1,463, Visits: 3,400
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.

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

http://www.martin2k.co.uk/forums/

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Post #25497
Posted 8/4/2008 2:01:25 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 9:26:27 AM
Posts: 541, Visits: 856
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
_____________________________________________________________
No trees were harmed in the making of this post.
However, a large number of electrons were horribly inconvenienced.
Post #25504
Posted 8/4/2008 2:03:46 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 9:26:27 AM
Posts: 541, Visits: 856
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
_____________________________________________________________
No trees were harmed in the making of this post.
However, a large number of electrons were horribly inconvenienced.
Post #25505
Posted 8/4/2008 5:19:10 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 4:36:16 PM
Posts: 1,463, Visits: 3,400
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.

Keith

http://www.martin2k.co.uk/forums/

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Post #25507
Posted 8/5/2008 12:40:18 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Yesterday @ 9:26:27 AM
Posts: 541, Visits: 856
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.

Mobius
_____________________________________________________________
No trees were harmed in the making of this post.
However, a large number of electrons were horribly inconvenienced.
Post #25522