Sign in to follow this  
Followers 0
Baboon

query service status / check if service is started

12 posts in this topic

Hi,

does anyone have a script that will query the status of a service in NT4. I need to check if a service is started as part of a script and exit if it is, any help would be much appreciated. I cannot use WSH because of security restrictions. Many thanks.

Share this post


Link to post
Share on other sites



Global $STANDARD_RIGHTS_REQUIRED = 0x000F0000
Global $SC_MANAGER_CONNECT = 0x0001
Global $SC_MANAGER_CREATE_SERVICE = 0x0002
Global $SC_MANAGER_ENUMERATE_SERVICE = 0x0004
Global $SC_MANAGER_LOCK = 0x0008
Global $SC_MANAGER_QUERY_LOCK_STATUS = 0x0010
Global $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020

Global $SC_MANAGER_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _
                                     $SC_MANAGER_CONNECT, _
                                     $SC_MANAGER_CREATE_SERVICE, _
                                     $SC_MANAGER_ENUMERATE_SERVICE, _
                                     $SC_MANAGER_LOCK, _
                                     $SC_MANAGER_QUERY_LOCK_STATUS, _
                                     $SC_MANAGER_MODIFY_BOOT_CONFIG)
                                     
Global $SERVICE_QUERY_CONFIG = 0x0001
Global $SERVICE_CHANGE_CONFIG = 0x0002
Global $SERVICE_QUERY_STATUS = 0x0004
Global $SERVICE_ENUMERATE_DEPENDENTS = 0x0008
Global $SERVICE_START = 0x0010
Global $SERVICE_STOP = 0x0020
Global $SERVICE_PAUSE_CONTINUE = 0x0040
Global $SERVICE_INTERROGATE = 0x0080
Global $SERVICE_USER_DEFINED_CONTROL = 0x0100

Global $SERVICE_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _
                                  $SERVICE_QUERY_CONFIG, _
                                  $SERVICE_CHANGE_CONFIG, _
                                  $SERVICE_QUERY_STATUS, _
                                  $SERVICE_ENUMERATE_DEPENDENTS, _
                                  $SERVICE_START, _
                                  $SERVICE_STOP, _
                                  $SERVICE_PAUSE_CONTINUE, _
                                  $SERVICE_INTERROGATE, _
                                  $SERVICE_USER_DEFINED_CONTROL)

Global $SERVICE_CONTROL_STOP = 0x00000001

; Example - start print spooler
If _ServiceExists("Spooler") Then _StopService("Spooler")


Func _StartService($sServiceName)
  Local $arRet
  Local $hSC
  Local $hService
  Local $lError = -1

  $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
                    "str", "", _
                    "str", "ServicesActive", _ 
                    "long", $SC_MANAGER_CONNECT)
  If $arRet[0] = 0 Then
     $arRet = DllCall("kernel32.dll", "long", "GetLastError")
     $lError = $arRet[0]
  Else
     $hSC = $arRet[0]
     $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                      "long", $hSC, _
                      "str", $sServiceName, _
                      "long", $SERVICE_START)
     If $arRet[0] = 0 Then
        $arRet = DllCall("kernel32.dll", "long", "GetLastError")
        $lError = $arRet[0]
     Else
        $hService = $arRet[0]
        $arRet = DllCall("advapi32.dll", "int", "StartService", _
                         "long", $hService, _
                         "long", 0, _
                         "str", "")
        If $arRet[0] = 0 Then
           $arRet = DllCall("kernel32.dll", "long", "GetLastError")
           $lError = $arRet[0]
        EndIf
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)         
     EndIf
     DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
  EndIf
  If $lError <> -1 Then SetError($lError)
EndFunc

Func _StopService($sServiceName)
  Local $arRet
  Local $hSC
  Local $hService
  Local $lError = -1

  $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
                    "str", "", _
                    "str", "ServicesActive", _
                    "long", $SC_MANAGER_CONNECT)
  If $arRet[0] = 0 Then
     $arRet = DllCall("kernel32.dll", "long", "GetLastError")
     $lError = $arRet[0]
  Else
     $hSC = $arRet[0]
     $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                      "long", $hSC, _
                      "str", $sServiceName, _
                      "long", $SERVICE_STOP)
     If $arRet[0] = 0 Then
        $arRet = DllCall("kernel32.dll", "long", "GetLastError")
        $lError = $arRet[0]
     Else
        $hService = $arRet[0]
        $arRet = DllCall("advapi32.dll", "int", "ControlService", _
                         "long", $hService, _
                         "long", $SERVICE_CONTROL_STOP, _
                         "long_ptr", 0)
        If $arRet[0] = 0 Then
           $arRet = DllCall("kernel32.dll", "long", "GetLastError")
           $lError = $arRet[0]
        EndIf
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)         
     EndIf
     DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
  EndIf
  If $lError <> -1 Then SetError($lError)
EndFunc

Func _ServiceExists($sServiceName)
  Local $arRet
  Local $hSC
  Local $bExist = 0
  
  $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
                    "str", "", _
                    "str", "ServicesActive", _
                    "long", $SC_MANAGER_CONNECT)
  If $arRet[0] <> 0 Then
     $hSC = $arRet[0]
     $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                      "long", $hSC, _
                      "str", $sServiceName, _
                      "long", $SERVICE_INTERROGATE)
     If $arRet[0] <> 0 Then
        $bExist = 1
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $arRet[0])
     EndIf
     DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
  EndIf
  Return $bExist
EndFunc

Func _ServiceRunning($sServiceName)
  Local $arRet
  Local $hSC
  Local $hService   
  Local $bRunning = 0
 
  $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
                   "str", "", _
                   "str", "ServicesActive", _
                   "long", $SC_MANAGER_CONNECT)
  If $arRet[0] <> 0 Then
     $hSC = $arRet[0]
     $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                      "long", $hSC, _
                      "str", $sServiceName, _
                      "long", $SERVICE_INTERROGATE)
     If $arRet[0] <> 0 Then
        $hService = $arRet[0]
        $arRet = DllCall("advapi32.dll", "int", "ControlService", _
                         "long", $hService, _
                         "long", $SERVICE_CONTROL_INTERROGATE, _
                         "long_ptr", 0)
        $bRunning = $arRet[0]
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
     EndIf
     DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
  EndIf
  Return $bRunning
EndFunc


Who else would I be?

Share this post


Link to post
Share on other sites

Hi,

thanks for the code! When I run it I get an "unknown function" error as attached, have you come across this?

Regards,

Phil

error1.zip

Share this post


Link to post
Share on other sites

Thanks again!

Share this post


Link to post
Share on other sites

Hi,

when I run this code (substituted DHCP as service name) I get an error about undeclared variable. Have you come across this before?

Many thanks again!

Global $STANDARD_RIGHTS_REQUIRED = 0x000F0000

Global $SC_MANAGER_CONNECT = 0x0001

Global $SC_MANAGER_CREATE_SERVICE = 0x0002

Global $SC_MANAGER_ENUMERATE_SERVICE = 0x0004

Global $SC_MANAGER_LOCK = 0x0008

Global $SC_MANAGER_QUERY_LOCK_STATUS = 0x0010

Global $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020

Global $SC_MANAGER_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _

$SC_MANAGER_CONNECT, _

$SC_MANAGER_CREATE_SERVICE, _

$SC_MANAGER_ENUMERATE_SERVICE, _

$SC_MANAGER_LOCK, _

$SC_MANAGER_QUERY_LOCK_STATUS, _

$SC_MANAGER_MODIFY_BOOT_CONFIG)

Global $SERVICE_QUERY_CONFIG = 0x0001

Global $SERVICE_CHANGE_CONFIG = 0x0002

Global $SERVICE_QUERY_STATUS = 0x0004

Global $SERVICE_ENUMERATE_DEPENDENTS = 0x0008

Global $SERVICE_START = 0x0010

Global $SERVICE_STOP = 0x0020

Global $SERVICE_PAUSE_CONTINUE = 0x0040

Global $SERVICE_INTERROGATE = 0x0080

Global $SERVICE_USER_DEFINED_CONTROL = 0x0100

Global $SERVICE_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _

$SERVICE_QUERY_CONFIG, _

$SERVICE_CHANGE_CONFIG, _

$SERVICE_QUERY_STATUS, _

$SERVICE_ENUMERATE_DEPENDENTS, _

$SERVICE_START, _

$SERVICE_STOP, _

$SERVICE_PAUSE_CONTINUE, _

$SERVICE_INTERROGATE, _

$SERVICE_USER_DEFINED_CONTROL)

Global $SERVICE_CONTROL_STOP = 0x00000001

; First ensure that the laptop is running in encrypted mode, if not advise user and exit

script.

If _ServiceRunning("DHCP") Then

MsgBox(0+48, "Warning", "iPass can Only be launched when the Laptop is running in Encrypted VPN Dial-up Access mode.")

Exit

Else

Run("C:\Program Files\iPass\iPassConnect\iPassConnectGUI.exe", "C:\Program

Files\iPass\iPassConnect\")

EndIf

Func _StartService($sServiceName)

Local $arRet

Local $hSC

Local $hService

Local $lError = -1

$arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _

"str", "", _

"str", "ServicesActive", _

"long", $SC_MANAGER_CONNECT)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hSC = $arRet[0]

$arRet = DllCall("advapi32.dll", "long", "OpenService", _

"long", $hSC, _

"str", $sServiceName, _

"long", $SERVICE_START)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hService = $arRet[0]

$arRet = DllCall("advapi32.dll", "int", "StartService", _

"long", $hService, _

"long", 0, _

"str", "")

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)

EndIf

If $lError <> -1 Then SetError($lError)

EndFunc

Func _StopService($sServiceName)

Local $arRet

Local $hSC

Local $hService

Local $lError = -1

$arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _

"str", "", _

"str", "ServicesActive", _

"long", $SC_MANAGER_CONNECT)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hSC = $arRet[0]

$arRet = DllCall("advapi32.dll", "long", "OpenService", _

"long", $hSC, _

"str", $sServiceName, _

"long", $SERVICE_STOP)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hService = $arRet[0]

$arRet = DllCall("advapi32.dll", "int", "ControlService", _

"long", $hService, _

"long", $SERVICE_CONTROL_STOP, _

"long_ptr", 0)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)

EndIf

If $lError <> -1 Then SetError($lError)

EndFunc

Func _ServiceExists($sServiceName)

Local $arRet

Local $hSC

Local $bExist = 0

$arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _

"str", "", _

"str", "ServicesActive", _

"long", $SC_MANAGER_CONNECT)

If $arRet[0] <> 0 Then

$hSC = $arRet[0]

$arRet = DllCall("advapi32.dll", "long", "OpenService", _

"long", $hSC, _

"str", $sServiceName, _

"long", $SERVICE_INTERROGATE)

If $arRet[0] <> 0 Then

$bExist = 1

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $arRet[0])

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)

EndIf

Return $bExist

EndFunc

Func _ServiceRunning($sServiceName)

Local $arRet

Local $hSC

Local $hService

Local $bRunning = 0

$arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _

"str", "", _

"str", "ServicesActive", _

"long", $SC_MANAGER_CONNECT)

If $arRet[0] <> 0 Then

$hSC = $arRet[0]

$arRet = DllCall("advapi32.dll", "long", "OpenService", _

"long", $hSC, _

"str", $sServiceName, _

"long", $SERVICE_INTERROGATE)

If $arRet[0] <> 0 Then

$hService = $arRet[0]

$arRet = DllCall("advapi32.dll", "int", "ControlService", _

"long", $hService, _

"long", $SERVICE_CONTROL_INTERROGATE, _

"long_ptr", 0)

$bRunning = $arRet[0]

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)

EndIf

Return $bRunning

EndFunc

Share this post


Link to post
Share on other sites

when I run this code (substituted DHCP as service name) I get an error about undeclared variable. Have you come across this before?

<{POST_SNAPBACK}>

Yes have seen it many times, but thats not really your questions..... :)

It means that you use a variable that has NOT been declared.

Its like running this one line script:

MsgBox(0,'demo',$a)

$a has no value yet so cannot be displayed.

Same in your script for: $SERVICE_CONTROL_INTERROGATE


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Global $SC_MANAGER_CONNECT = 0x0001
Global $SERVICE_INTERROGATE = 0x0080
Global $SERVICE_CONTROL_INTERROGATE = 0x00000004

; First ensure that the laptop is running in encrypted mode
; if not advise user and exit script.
If _ServiceRunning("DHCP") Then
   MsgBox(0+48, "Warning", "iPass can Only be launched when the Laptop is running in Encrypted VPN Dial-up Access mode.")
   Exit
Else
   Run("C:\Program Files\iPass\iPassConnect\iPassConnectGUI.exe", "C:\Program Files\iPass\iPassConnect\")
EndIf


Func _ServiceRunning($sServiceName)
  Local $arRet
  Local $hSC
  Local $hService   
  Local $bRunning = 0
 
  $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
                   "str", "", _
                   "str", "ServicesActive", _
                   "long", $SC_MANAGER_CONNECT)
  If $arRet[0] <> 0 Then
     $hSC = $arRet[0]
     $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                      "long", $hSC, _
                      "str", $sServiceName, _
                      "long", $SERVICE_INTERROGATE)
     If $arRet[0] <> 0 Then
        $hService = $arRet[0]
        $arRet = DllCall("advapi32.dll", "int", "ControlService", _
                         "long", $hService, _
                         "long", $SERVICE_CONTROL_INTERROGATE, _
                         "long_ptr", 0)
        $bRunning = $arRet[0]
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
     EndIf
     DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
  EndIf
  Return $bRunning
EndFunc

Share this post


Link to post
Share on other sites

ah, thank-you very much, you sir are a gentleman! That was really driving me crazy.

One final qauestion if I may, this script works great on Win2K but on NT4 causes an error "instruction at Ox00428e6a referenced memory at 0x400000004, the m,emory could not be written". Is this not compatible with NT4?

Best regards,

Phil

Share this post


Link to post
Share on other sites

ah, thank-you very much, you sir are a gentleman! That was really driving me crazy.

One final qauestion if I may, this script works great on Win2K but  on NT4 causes an error "instruction at Ox00428e6a referenced memory at 0x400000004, the m,emory could not be written". Is this not compatible with NT4?

Best regards,

Phil

<{POST_SNAPBACK}>

The service control APIs used in these functions are supposed to be NT-compatible so I am not sure where it's going wrong. I will check it out if I can dig out an old NT machine. :">

Cheers.

Share this post


Link to post
Share on other sites

Fixed the crashing problem with NT

Global $SC_MANAGER_CONNECT = 0x0001
Global $SERVICE_INTERROGATE = 0x0080
Global $SERVICE_CONTROL_INTERROGATE = 0x00000004

; First ensure that the laptop is running in encrypted mode
; if not advise user and exit script.
If _ServiceRunning("DHCP") Then
  MsgBox(0+48, "Warning", "iPass can Only be launched when the Laptop is running in Encrypted VPN Dial-up Access mode.")
  Exit
Else
  Run("C:\Program Files\iPass\iPassConnect\iPassConnectGUI.exe", "C:\Program Files\iPass\iPassConnect\")
EndIf


Func _ServiceRunning($sServiceName)
 Local $arRet
 Local $hSC
 Local $hService   
 Local $bRunning = 0

 $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
                  "str", "", _
                  "str", "ServicesActive", _
                  "long", $SC_MANAGER_CONNECT)
 If $arRet[0] <> 0 Then
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
                     "long", $hSC, _
                     "str", $sServiceName, _
                     "long", $SERVICE_INTERROGATE)
    If $arRet[0] <> 0 Then
       $hService = $arRet[0]
       $arRet = DllCall("advapi32.dll", "int", "ControlService", _
                        "long", $hService, _
                        "long", $SERVICE_CONTROL_INTERROGATE, _
                        "str", "")
       $bRunning = $arRet[0]
       DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
 EndIf
 Return $bRunning
EndFunc

What I did was change the third param in the ControlService call from "long_ptr" to "str".

Share this post


Link to post
Share on other sites

THANK-YOU!!! That works perfectly. :)

Share this post


Link to post
Share on other sites

I have modified the initial script to incorporate a service status section, can someone make a UDF from it.

Global $STANDARD_RIGHTS_REQUIRED = 0x000F0000

Global $SC_MANAGER_CONNECT = 0x0001

Global $SC_MANAGER_CREATE_SERVICE = 0x0002

Global $SC_MANAGER_ENUMERATE_SERVICE = 0x0004

Global $SC_MANAGER_LOCK = 0x0008

Global $SC_MANAGER_QUERY_LOCK_STATUS = 0x0010

Global $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020

Global $SC_MANAGER_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _

$SC_MANAGER_CONNECT, _

$SC_MANAGER_CREATE_SERVICE, _

$SC_MANAGER_ENUMERATE_SERVICE, _

$SC_MANAGER_LOCK, _

$SC_MANAGER_QUERY_LOCK_STATUS, _

$SC_MANAGER_MODIFY_BOOT_CONFIG)

Global $SERVICE_QUERY_CONFIG = 0x0001

Global $SERVICE_CHANGE_CONFIG = 0x0002

Global $SERVICE_QUERY_STATUS = 0x0004

Global $SERVICE_ENUMERATE_DEPENDENTS = 0x0008

Global $SERVICE_START = 0x0010

Global $SERVICE_STOP = 0x0020

Global $SERVICE_PAUSE_CONTINUE = 0x0040

Global $SERVICE_INTERROGATE = 0x0080

Global $SERVICE_USER_DEFINED_CONTROL = 0x0100

Global $SERVICE_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _

$SERVICE_QUERY_CONFIG, _

$SERVICE_CHANGE_CONFIG, _

$SERVICE_QUERY_STATUS, _

$SERVICE_ENUMERATE_DEPENDENTS, _

$SERVICE_START, _

$SERVICE_STOP, _

$SERVICE_PAUSE_CONTINUE, _

$SERVICE_INTERROGATE, _

$SERVICE_USER_DEFINED_CONTROL)

Global $SERVICE_CONTROL_STOP = 0x00000001

Global $SERVICE_CONTROL_INTERROGATE = 0x00000004

Dim $ComputerName,$ServiceName

$ComputerName = @ComputerName

$ServiceName = "Spooler"

If _ServiceStatus($ComputerName,$ServiceName) = "Running" Then

_ServiceStop($ComputerName,$ServiceName)

If @error Then

If @Error = 5 then

MsgBox(64, "", "Failed to Stop " & $ServiceName & " service - Access Denied")

Else

MsgBox(64, "", "Failed to Stop " & $ServiceName & " service - Error " & @error)

EndIf

Else

MsgBox(64, "",$ServiceName & " service stopped")

EndIf

ElseIf _ServiceStatus($ComputerName,$ServiceName) = "Stopped" Then

_ServiceStart($ComputerName,$ServiceName)

If @error Then

If @Error = 5 then

MsgBox(64, "", "Failed to start " & $ServiceName & " service - Access Denied")

Else

MsgBox(64, "", "Failed to start " & $ServiceName & " service - Error " & @error)

EndIf

Else

MsgBox(64, "",$ServiceName & " service started")

EndIf

Else

MsgBox(64, "",@error)

EndIf

Func _ServiceStart($sComputerName, $sServiceName)

Local $arRet

Local $hSC

Local $hService

Local $lError = -1

$arRet = DllCall("advapi32.dll", "long", "OpenSCManager", "str", $sComputerName, "str", "ServicesActive", "long", $SC_MANAGER_CONNECT)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hSC = $arRet[0]

$arRet = DllCall("advapi32.dll", "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", $SERVICE_ALL_ACCESS)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hService = $arRet[0]

$arRet = DllCall("advapi32.dll", "int", "StartService", "long", $hService, "long", 0, "str", "")

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)

EndIf

If $lError <> -1 Then SetError($lError)

EndFunc

Func _ServiceStop($sComputerName, $sServiceName)

Local $arRet

Local $hSC

Local $hService

Local $lError = -1

$arRet = DllCall("advapi32.dll", "long", "OpenSCManager", "str", $sComputerName, "str", "ServicesActive", "long", $SC_MANAGER_CONNECT)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hSC = $arRet[0]

$arRet = DllCall("advapi32.dll", "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", $SERVICE_ALL_ACCESS)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hService = $arRet[0]

$arRet = DllCall("advapi32.dll", "int", "ControlService", "long", $hService, "long", $SERVICE_CONTROL_STOP, "str", "")

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)

EndIf

If $lError <> -1 Then SetError($lError)

EndFunc

Func _ServiceStatus($sComputerName, $sServiceName)

Local $arRet

Local $hSC

Local $hService

Local $SERVICE_STATUS

Local $nState = 0

Local $lError = -1

$arRet = DllCall("advapi32.dll", "long", "OpenSCManager", "str", $sComputerName, "str", "ServicesActive", "long", $SC_MANAGER_CONNECT)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hSC = $arRet[0]

$arRet = DllCall("advapi32.dll", "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", $SERVICE_QUERY_STATUS)

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$hService = $arRet[0]

$SERVICE_STATUS = DllStructCreate("dword;dword;dword;dword;dword;dword;dword")

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

Else

$arRet = DllCall("advapi32.dll", "int", "QueryServiceStatus", "long", $hService, "ptr", DllStructGetPtr($SERVICE_STATUS))

If $arRet[0] = 0 Then

$arRet = DllCall("kernel32.dll", "long", "GetLastError")

$lError = $arRet[0]

ElseIf $arRet[0] > 0 Then

$nState = DllStructGetData($SERVICE_STATUS, 2)

EndIf

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)

EndIf

DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)

EndIf

If $lError <> -1 Then SetError($lError)

Select

Case $lError = 1060

Return "Error ! Service Does Not Exist"

Case $nState = 0

Return "Error ! Service Does Not Exist"

Case $nState = 1

Return "Stopped"

Case $nState = 2

Return "Pending"

Case $nState = 3

Return "Stop Pending"

Case $nState = 4

Return "Running"

Case $nState = 5

Return "Continue Pending"

Case $nState = 6

Return "Pause Pending"

Case $nState = 7

Return "Paused"

EndSelect

EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0