Programatically enable disable Screen saver

G

Guest

Hi
I am using the code below to enable and disable the screen saver.It all
works fine with win98 and win 2000 but it is not working for Win XP.What
could be the problem.

Option Explicit
Private Declare Function SystemParametersInfo Lib "user32" Alias
"SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal
lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Const SPI_SETSCREENSAVEACTIVE = 17

Public Function ToggleScreenSaverActive(Active As Boolean) As Boolean
Dim lActiveFlag As Long
Dim retvaL As Long
lActiveFlag = IIf(Active, 1, 0)
retvaL = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, lActiveFlag, 0, 0)
ToggleScreenSaverActive = retvaL > 0
End Function

Private Sub Command1_Click()
ToggleScreenSaverActive (False)
End Sub

Private Sub Command2_Click()
ToggleScreenSaverActive (True)
End Sub

With Thanks
Sridhar
 
A

Ayush

Replied to [Sridhar]s message :
Hi
I am using the code below to enable and disable the screen saver.It all
works fine with win98 and win 2000 but it is not working for Win XP.What
could be the problem.




No need for that big script. The thing that needs change is the registry key :
HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

If you change that to blank(or delete that key) then the screensaver is set to none.
If it contains a screensaver path then the screensaver will run. You can go to
registry editor then make to exports of that key, one with screensaver set to none,
one with your favourite screensaver... The double click on the exported files to run
then and change that key

OR Use this JScript to toggle (just double click):

valOfScrenSav="C:\\Windows\\System32\\ssbezier.scr"

shh=new ActiveXObject('WScript.Shell');
kk="HKEY_CURRENT_USER\\Control Panel\\Desktop\\SCRNSAVE.EXE"
scVal=shh.RegRead(kk)
if (scVal!=""){
shh.RegWrite(kk,"")
}else{
shh.RegWrite(kk,valOfScrenSav)
}


Copy/Paste in Notepad then save the file with JS extension....


Before running the script, change the path :
C:\\Windows\\System32\\ssbezier.scr
to the path of any screensaver and use TWO backslashes, one will not work

→ Ayush [ Good :) Luck ]
 
R

Ramesh, MS-MVP

Does that setting apply immediately (that is, without requiring a logoff/logon)?

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Ayush said:
Hi
I am using the code below to enable and disable the screen saver.It all
works fine with win98 and win 2000 but it is not working for Win XP.What
could be the problem.




No need for that big script. The thing that needs change is the registry key :
HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

If you change that to blank(or delete that key) then the screensaver is set to none.
If it contains a screensaver path then the screensaver will run. You can go to
registry editor then make to exports of that key, one with screensaver set to none,
one with your favourite screensaver... The double click on the exported files to run
then and change that key

OR Use this JScript to toggle (just double click):

valOfScrenSav="C:\\Windows\\System32\\ssbezier.scr"

shh=new ActiveXObject('WScript.Shell');
kk="HKEY_CURRENT_USER\\Control Panel\\Desktop\\SCRNSAVE.EXE"
scVal=shh.RegRead(kk)
if (scVal!=""){
shh.RegWrite(kk,"")
}else{
shh.RegWrite(kk,valOfScrenSav)
}


Copy/Paste in Notepad then save the file with JS extension....


Before running the script, change the path :
C:\\Windows\\System32\\ssbezier.scr
to the path of any screensaver and use TWO backslashes, one will not work

→ Ayush [ Good :) Luck ]
 
A

Ayush

Replied to [Ramesh, MS-MVP]s message :
Does that setting apply immediately (that is, without requiring a logoff/logon)?

Not tried waiting for the screensaver but it shows the results immediately in the
display properties dialog box ....




→ Ayush [ Good :) Luck ]
 
A

Ayush

Replied to [Ayush]s message :
Replied to [Ramesh, MS-MVP]s message :
Does that setting apply immediately (that is, without requiring a
logoff/logon)?

Not tried waiting for the screensaver but it shows the results
immediately in the display properties dialog box ....


Tried the script and it works just by double clicking, no need to Log/off, Log/in
again...



→ Ayush [ Good :) Luck ]
 
R

Ramesh, MS-MVP

Thanks for the update. So the screensaver timeout value needs to be set using API (SystemParametersInfo SPI_SETSCREENSAVETIMEOUT), but not for changing the screensaver itself.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Ayush said:
Replied to [Ramesh, MS-MVP]s message :
Does that setting apply immediately (that is, without requiring a
logoff/logon)?

Not tried waiting for the screensaver but it shows the results
immediately in the display properties dialog box ....


Tried the script and it works just by double clicking, no need to Log/off, Log/in
again...



→ Ayush [ Good :) Luck ]
 
G

Guest

Thanks for the script it works fine and solves my problem.
But what could be the problem with SystemParametersInfo for XP.

Sridhar

Ayush" <"ayushmaan.j[aatt]gmail.com said:
Replied to [Sridhar]s message :
Hi
I am using the code below to enable and disable the screen saver.It all
works fine with win98 and win 2000 but it is not working for Win XP.What
could be the problem.




No need for that big script. The thing that needs change is the registry key :
HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

If you change that to blank(or delete that key) then the screensaver is set to none.
If it contains a screensaver path then the screensaver will run. You can go to
registry editor then make to exports of that key, one with screensaver set to none,
one with your favourite screensaver... The double click on the exported files to run
then and change that key

OR Use this JScript to toggle (just double click):

valOfScrenSav="C:\\Windows\\System32\\ssbezier.scr"

shh=new ActiveXObject('WScript.Shell');
kk="HKEY_CURRENT_USER\\Control Panel\\Desktop\\SCRNSAVE.EXE"
scVal=shh.RegRead(kk)
if (scVal!=""){
shh.RegWrite(kk,"")
}else{
shh.RegWrite(kk,valOfScrenSav)
}


Copy/Paste in Notepad then save the file with JS extension....


Before running the script, change the path :
C:\\Windows\\System32\\ssbezier.scr
to the path of any screensaver and use TWO backslashes, one will not work

→ Ayush [ Good :) Luck ]
 
A

Ayush

Replied to [Sridhar]s message :
Thanks for the script it works fine and solves my problem.

You are welcome !!

But what could be the problem with SystemParametersInfo for XP.

Sorry, dont know about that..


→ Ayush [ Good :) Luck ]
 
R

Ramesh, MS-MVP

You could check what value the function "ToggleScreenSaverActive" returns. "Retval" variable in your code gets the return value from the SystemParametersInfo call.

SystemParametersInfo:
http://msdn2.microsoft.com/en-us/library/ms724947.aspx

Return Value
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

© 2006 Microsoft Corporation. All rights reserved.


--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Thanks for the script it works fine and solves my problem.
But what could be the problem with SystemParametersInfo for XP.

Sridhar

Ayush" <"ayushmaan.j[aatt]gmail.com said:
Replied to [Sridhar]s message :
Hi
I am using the code below to enable and disable the screen saver.It all
works fine with win98 and win 2000 but it is not working for Win XP.What
could be the problem.




No need for that big script. The thing that needs change is the registry key :
HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

If you change that to blank(or delete that key) then the screensaver is set to none.
If it contains a screensaver path then the screensaver will run. You can go to
registry editor then make to exports of that key, one with screensaver set to none,
one with your favourite screensaver... The double click on the exported files to run
then and change that key

OR Use this JScript to toggle (just double click):

valOfScrenSav="C:\\Windows\\System32\\ssbezier.scr"

shh=new ActiveXObject('WScript.Shell');
kk="HKEY_CURRENT_USER\\Control Panel\\Desktop\\SCRNSAVE.EXE"
scVal=shh.RegRead(kk)
if (scVal!=""){
shh.RegWrite(kk,"")
}else{
shh.RegWrite(kk,valOfScrenSav)
}


Copy/Paste in Notepad then save the file with JS extension....


Before running the script, change the path :
C:\\Windows\\System32\\ssbezier.scr
to the path of any screensaver and use TWO backslashes, one will not work

→ Ayush [ Good :) Luck ]
 
G

Guest

The Return Value is 1.

Ramesh said:
You could check what value the function "ToggleScreenSaverActive" returns. "Retval" variable in your code gets the return value from the SystemParametersInfo call.

SystemParametersInfo:
http://msdn2.microsoft.com/en-us/library/ms724947.aspx

Return Value
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

© 2006 Microsoft Corporation. All rights reserved.


--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Thanks for the script it works fine and solves my problem.
But what could be the problem with SystemParametersInfo for XP.

Sridhar

Ayush" <"ayushmaan.j[aatt]gmail.com said:
Replied to [Sridhar]s message :
Hi
I am using the code below to enable and disable the screen saver.It all
works fine with win98 and win 2000 but it is not working for Win XP.What
could be the problem.




No need for that big script. The thing that needs change is the registry key :
HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

If you change that to blank(or delete that key) then the screensaver is set to none.
If it contains a screensaver path then the screensaver will run. You can go to
registry editor then make to exports of that key, one with screensaver set to none,
one with your favourite screensaver... The double click on the exported files to run
then and change that key

OR Use this JScript to toggle (just double click):

valOfScrenSav="C:\\Windows\\System32\\ssbezier.scr"

shh=new ActiveXObject('WScript.Shell');
kk="HKEY_CURRENT_USER\\Control Panel\\Desktop\\SCRNSAVE.EXE"
scVal=shh.RegRead(kk)
if (scVal!=""){
shh.RegWrite(kk,"")
}else{
shh.RegWrite(kk,valOfScrenSav)
}


Copy/Paste in Notepad then save the file with JS extension....


Before running the script, change the path :
C:\\Windows\\System32\\ssbezier.scr
to the path of any screensaver and use TWO backslashes, one will not work

→ Ayush [ Good :) Luck ]
 
R

Ramesh, MS-MVP

Tried Err.LastDllError ? You may have to use "FormatMessage" to return the error message description (passing LastDllError as the parameter)

Windows API Function Errors And FormatMessage:
http://www.cpearson.com/excel/FormatMessage.htm

Karl E. Peterson's Classic VB Code: ApiErrMsg:
http://vb.mvps.org/samples/project.asp?id=ApiErrMsg


--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


The Return Value is 1.

Ramesh said:
You could check what value the function "ToggleScreenSaverActive" returns. "Retval" variable in your code gets the return value from the SystemParametersInfo call.

SystemParametersInfo:
http://msdn2.microsoft.com/en-us/library/ms724947.aspx

Return Value
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

© 2006 Microsoft Corporation. All rights reserved.


--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Thanks for the script it works fine and solves my problem.
But what could be the problem with SystemParametersInfo for XP.

Sridhar

Ayush" <"ayushmaan.j[aatt]gmail.com said:
Replied to [Sridhar]s message :
Hi
I am using the code below to enable and disable the screen saver.It all
works fine with win98 and win 2000 but it is not working for Win XP.What
could be the problem.




No need for that big script. The thing that needs change is the registry key :
HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

If you change that to blank(or delete that key) then the screensaver is set to none.
If it contains a screensaver path then the screensaver will run. You can go to
registry editor then make to exports of that key, one with screensaver set to none,
one with your favourite screensaver... The double click on the exported files to run
then and change that key

OR Use this JScript to toggle (just double click):

valOfScrenSav="C:\\Windows\\System32\\ssbezier.scr"

shh=new ActiveXObject('WScript.Shell');
kk="HKEY_CURRENT_USER\\Control Panel\\Desktop\\SCRNSAVE.EXE"
scVal=shh.RegRead(kk)
if (scVal!=""){
shh.RegWrite(kk,"")
}else{
shh.RegWrite(kk,valOfScrenSav)
}


Copy/Paste in Notepad then save the file with JS extension....


Before running the script, change the path :
C:\\Windows\\System32\\ssbezier.scr
to the path of any screensaver and use TWO backslashes, one will not work

→ Ayush [ Good :) Luck ]
 
A

Ayush

Replied to [Ramesh, MS-MVP]s message :
So the screensaver timeout value needs to be set using API (SystemParametersInfo SPI_SETSCREENSAVETIMEOUT),
but not for changing the screensaver itself.


I am confused.. How can i use SystemParametersInfo ? How the Op was using it ?
 
R

Ramesh, MS-MVP

It's an API call. And the OP is a coder, who uses Visual Basic 6 IDE. Yes, I know this is not a programming group, but thought I can contribute whatever I can.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Ayush said:
So the screensaver timeout value needs to be set using API (SystemParametersInfo SPI_SETSCREENSAVETIMEOUT),
but not for changing the screensaver itself.


I am confused.. How can i use SystemParametersInfo ? How the Op was using it ?
 
A

Ayush

Replied to [Ramesh, MS-MVP]s message :
It's an API call. And the OP is a coder, who uses Visual Basic 6 IDE. Yes, I know this is not a programming group, but thought I can contribute whatever I can.

Oh... and i thought that was VBs . :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top