Checking Print Spooler status (running or not)

4.1k Views Asked by At

I need to detect whether the Print Spooler service is running. I can find various resources for VB.NET (e.g., using ServiceProcess.ServiceController to actually manipulate the service), but nothing for VB6.

Is there any way to check whether the Print Spooler is running in VB6? And ideally start it, but I can survive without that.

3

There are 3 best solutions below

0
On BEST ANSWER

We use wmi in VBA/VB6/VBScript and command prompt.

This lists processes

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
        msgbox objitem.name & " PID=" & objItem.ProcessID & " SessionID=" & objitem.sessionid
'       objitem.terminate
Next

This is typed an command prompt.

wmic process get 

You'll see you can get VBS methods/properties by using wmic help

wmic /? wmic process /? wmic process get /?

So wmic service get caption,status

so

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Service")
For Each objItem in colItems
        msgbox objitem.name & " " & objitem.status
Next
0
On

Both answers already posted are good (and will solve the problem) but I just wanted to answer my own question to incorporate an answer given elsewhere (By user Bonnie West over at VBForums.com), as it gives an additional approach and is probably useful for anyone else who finds this question:

Option Explicit 'In a standard Module

Private Sub Main()
    With CreateObject("Shell.Application")  'Or New Shell if Microsoft Shell Controls And Automation is referenced
        If .IsServiceRunning("Spooler") Then
            .ServiceStop "Spooler", False
        Else
            .ServiceStart "Spooler", False
        End If
    End With
End Sub

Source

0
On

Since there's only one Print Spooler, you can query the Win32_Service class for the single instance. Then, check the Started property to determine if it's started/running:

Set objSpooler = GetObject("winmgmts:root\cimv2:Win32_Service.Name='Spooler'")

If objSpooler.Started Then
    MsgBox "Print Spooler is running."
Else
    MsgBox "Print Spooler is NOT running."
End If