Stop a running Screensaver .

R

RAFAAJ2000

Hi,

I am trying to turnoff a screensaver as soon as it kicks off.

I know how to detect if a screen saver is currently running via
SPI_GETSCREENSAVERRUNNING but I can't seem to STOP it once it has kicked off
programmatically.

I have tried simulating a Mouse Move or SendKey and even using the
SendMessage with the WM_CLOSE Msg but no luck !

Actually, I ma not sure about this, but it looks as if no code is executed
once the screensaver kicks off as the lines of code subsequent to the
screensaver starting don't execute !

I am doing this in an XL macro under XL2002 WIN XP.

Any help on this ?

Regards.
 
S

Steve Yandl

Try this.

_____________________________________

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")

For Each objProcess in colProcesses
If Right(objProcess.Name, 4) = ".scr" Then
objProcess.Terminate
End If
Next

____________________________________

Steve
 
R

RAFAAJ2000

Steve,

Fantastic !....It works beautifully !......It never occured to me to use the
WMI for solving this . I had been only exploring APIs so far.

Thank you very much indeed. :)

I am still curious to see a pure API solution only for the sake of learning :)

Regards.
 
M

Michel Pierron

Hi RAFAAJ2000,
You can try:

Private Declare Function SystemParametersInfo& _
Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long _
, ByRef lpvParam As Any, ByVal fuWinIni As Long)

Sub ScreenSaverOff()
Call SetScreenSaver(False)
End Sub

Sub ScreenSaverOn()
Call SetScreenSaver(True)
End Sub

Private Sub SetScreenSaver(ByVal Enable As Boolean)
Dim Ret As Long
Ret = SystemParametersInfo(17, Enable, ByVal 0, 0)
If Ret = 0 Then MsgBox "Error !", 64
End Sub

MP
 
R

RAFAAJ2000

Michel,

The Const 17 stands for SPI_SETSCREENSAVEACTIVE which actually toggles the
creation\removal of a screensaver like when the user selects a sreensaver
through Control Panel.This is not what I am after. What I want is to be able
to temporarly interrupt a running screensaver like when the users moves the
Mouse or press a key.

Steve's solution via using the WMI is the only one I have seen so far that
works !

Being essentially an API wrapper,the WMI Lib indicates that there must exist
a solution using APIs alone. I would be interested to know how that is done
for the sake of learning and also because relying on WSH, WMI is not
foolproof as these external Libs can often be disabled by network
administrators.

On the other hand, using APIs for this would guaranty that the code always
work in any machine running Windows .

Thanks for the input though Michel.

Regards.
 
M

Michel Pierron

Try this:

Private Declare Function OpenProcess& Lib _
"kernel32.dll" (ByVal dwDesiredAccess& _
, ByVal blnheritHandle&, ByVal dwAppProcessId&)
Private Declare Function TerminateProcess& Lib _
"kernel32.dll" (ByVal ApphProcess&, ByVal uExitCode&)
Private Declare Function CloseHandle& Lib _
"kernel32.dll" (ByVal hObject&)
Private Declare Function ProcessFirst& Lib _
"kernel32" Alias "Process32First" (ByVal hSnapshot& _
, uProcess As PROCESSENTRY32)
Private Declare Function ProcessNext& Lib _
"kernel32" Alias "Process32Next" (ByVal hSnapshot& _
, uProcess As PROCESSENTRY32)
Private Declare Function CreateToolhelpSnapshot& Lib _
"kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags&, lProcessID&)

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 260
End Type

Sub KillScreenSaverProcess()
Dim h&: h = CreateToolhelpSnapshot(2&, 0&)
If h = 0 Then Exit Sub
Dim P32 As PROCESSENTRY32: P32.dwSize = Len(P32)
Dim r&: r = ProcessFirst(h, P32)
Dim ScreenSaver&, ExitCode&
Do While r
If InStr(1, P32.szexeFile, ".scr", 1) Then
ScreenSaver = OpenProcess(&H1F0FFF, False, P32.th32ProcessID)
Call TerminateProcess(ScreenSaver, ExitCode)
Call CloseHandle(ScreenSaver)
Exit Sub
End If
r = ProcessNext(h, P32)
Loop
End Sub

MP
 
R

RAFAAJ2000

Fantastic ! :)

Works beautifully without even erroring out like when using the WMI .

I 'll take a good look at those handy Process related APIs for future
reference.

Very educational indeed :)

Thanks very much indeed for your help Michel .

Regards.
 
R

RAFAAJ2000

Hi again,

Can you recommend a reference book or web source on the WMI object library ?

I find this library a convienient alternative to using long Non-Object
-Oriented API based code and therefore I believe it is worth learnig .
However the syntax of many of its commands are so odd I really could do with
some methodical explanation.

Any suggestions ?

Regards.
 

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