by Nepveux Software (4 Submissions)
Category: Windows System Services
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 17th November 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Determine if the default screensaver is running.
API Declarations
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 * MAX_PATH
End Type
Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'Add a listbox to the project and name it List1
'or List8, etc, but replace all List1 with ListWhatever
'it doesn't need to be visible
'Usage:
'just call IsScreensaverRunning. returns True or False
'Notes:
'1 - the code to get the list of open apps is not mine.
'2 - only works for the DEFAULT screensaver (set in windows)
' if a different screensaver is running, it returns False
Private Function GetScreensaver() As String
Dim Temp As String
Open "c:\windows\system.ini" For Input As #1
Do
Input #1, Temp
Loop Until LCase(Left(Temp, 13)) = "scrnsave.exe="
Close #1
GetScreensaver = UCase(Right(Temp, Len(Temp) - 13))
End Function
Private Function KillApp(myName As String) As Boolean
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim I As Integer
On Local Error GoTo Finish
appCount = 0
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
List1.Clear
Do While rProcessFound
I = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, I - 1))
List1.AddItem (szExename)
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Function
Private Function IsScreensaverRunning() As Boolean
KillApp ("none")
Dim x
Dim SSRunning As Boolean
SSRunning = False
For x = 0 To List1.ListCount - 1
If UCase(List1.List(x)) = GetScreensaver Then
SSRunning = True
Exit For
End If
Next x
IsScreenSaverRunning = SSRunning
End Function