;=============================================================================== ; Description: Starts a service ; Parameters: $sServiceName - name of the service to start ; Requirements: None ; Return Values: On Success - 1 ; On Failure - 0 and @error is set to extended Windows error code ; Note: This function does not check to see if the service has started successfully ;=============================================================================== Func _StartService2($sServiceName) Local $hAdvapi32 Local $hKernel32 Local $arRet Local $hSC Local $hService Local $lError = -1 $hAdvapi32 = DllOpen("advapi32.dll") If $hAdvapi32 = -1 Then Return 0 $hKernel32 = DllOpen("kernel32.dll") If $hKernel32 = -1 Then Return 0 $arRet = DllCall($hAdvapi32, "handle", "OpenSCManagerA", _ "str", "", _ "str", "ServicesActive", _ "dword", 0x0001) If $arRet[0] = 0 Then $arRet = DllCall($hKernel32, "dword", "GetLastError") $lError = $arRet[0] Else $hSC = $arRet[0] $arRet = DllCall($hAdvapi32, "handle", "OpenServiceA", _ "handle", $hSC, _ "str", $sServiceName, _ "dword", 0x0010) If $arRet[0] = 0 Then $arRet = DllCall($hKernel32, "dword", "GetLastError") $lError = $arRet[0] Else $hService = $arRet[0] $arRet = DllCall($hAdvapi32, "bool", "StartServiceA", _ "handle", $hService, _ "dword", 0x0000, _ "str", "") If $arRet[0] = 0 Then $arRet = DllCall($hKernel32, "dword", "GetLastError") $lError = $arRet[0] EndIf DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hService) EndIf DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hSC) EndIf DllClose($hAdvapi32) DllClose($hKernel32) If $lError <> -1 Then SetError($lError) Return 0 EndIf Return 1 EndFunc ;==>_StartService ;=============================================================================== ; Description: Stops a service ; Parameters: $sServiceName - name of the service to stop ; Requirements: None ; Return Values: On Success - 1 ; On Failure - 0 and @error is set to extended Windows error code ; Note: This function does not check to see if the service has stopped successfully ;=============================================================================== Func _StopService2($sServiceName) Local $hAdvapi32 Local $hKernel32 Local $arRet Local $hSC Local $hService Local $lError = -1 $hAdvapi32 = DllOpen("advapi32.dll") If $hAdvapi32 = -1 Then Return 0 $hKernel32 = DllOpen("kernel32.dll") If $hKernel32 = -1 Then Return 0 $arRet = DllCall($hAdvapi32, "handle", "OpenSCManagerA", _ "str", "", _ "str", "ServicesActive", _ "dword", 0x0001) If $arRet[0] = 0 Then $arRet = DllCall($hKernel32, "dword", "GetLastError") $lError = $arRet[0] Else $hSC = $arRet[0] $arRet = DllCall($hAdvapi32, "handle", "OpenServiceA", _ "handle", $hSC, _ "str", $sServiceName, _ "dword", 0x0020) If $arRet[0] = 0 Then $arRet = DllCall($hKernel32, "dword", "GetLastError") $lError = $arRet[0] Else $hService = $arRet[0] $arRet = DllCall($hAdvapi32, "bool", "ControlService", _ "handle", $hService, _ "dword", 0x00000001, _ "str", "") If $arRet[0] = 0 Then $arRet = DllCall($hKernel32, "dword", "GetLastError") $lError = $arRet[0] EndIf DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hService) EndIf DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hSC) EndIf DllClose($hAdvapi32) DllClose($hKernel32) If $lError <> -1 Then SetError($lError) Return 0 EndIf Return 1 EndFunc ;==>_StopService ;=============================================================================== ; Description: Checks if a service is running ; Parameters: $sServiceName - name of the service to check ; Requirements: None ; Return Values: On Success - 1 ; On Failure - 0 ; Note: This function relies on the fact that only a running service responds ; to a SERVICE_CONTROL_INTERROGATE control code. Check the ControlService ; page on MSDN for limitations with using this method. ;=============================================================================== Func _ServiceRunning2($sServiceName) Local $hAdvapi32 Local $arRet Local $hSC Local $hService Local $bRunning = 0 $hAdvapi32 = DllOpen("advapi32.dll") If $hAdvapi32 = -1 Then Return 0 $arRet = DllCall($hAdvapi32, "handle", "OpenSCManagerA", _ "str", "", _ "str", "ServicesActive", _ "dword", 0x0001) If $arRet[0] <> 0 Then $hSC = $arRet[0] $arRet = DllCall($hAdvapi32, "handle", "OpenServiceA", _ "handle", $hSC, _ "str", $sServiceName, _ "dword", 0x0080) If $arRet[0] <> 0 Then $hService = $arRet[0] $arRet = DllCall($hAdvapi32, "bool", "ControlService", _ "handle", $hService, _ "dword", 0x00000004, _ "str", "") $bRunning = $arRet[0] DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hService) EndIf DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hSC) EndIf DllClose($hAdvapi32) Return $bRunning EndFunc ;==>_ServiceRunning