Code: Select all
RunWait,sc stop "AdobeARMservice" ;Stop AdobeARM service.
If (ErrorLevel = 0){
MsgBox Success!
}
MsgBox %Errorlevel%
RunWait,sc start "AdobeARMservice" ;Start AdobeARM service.
If (ErrorLevel = 0){
MsgBox Success!
}
return
Code: Select all
RunWait,sc stop "AdobeARMservice" ;Stop AdobeARM service.
If (ErrorLevel = 0){
MsgBox Success!
}
MsgBox %Errorlevel%
RunWait,sc start "AdobeARMservice" ;Start AdobeARM service.
If (ErrorLevel = 0){
MsgBox Success!
}
return
Code: Select all
If !(A_IsAdmin) ;- if you're NOT 'admin'
{
Run, *RunAs %comspec% /c net stop "FillInHere_MyService", , hide
}
If (A_IsAdmin) ;- I'm 'admin' so it works
Run, %comspec% /c net stop "FillInHere_MyService", , hide
return
Code: Select all
; by tonne at https://autohotkey.com/board/topic/14159-check-state-of-windows-service/
; updated by Drugwash, 2017.07.20
/*
SERVICE_STOPPED = 0x00000001
SERVICE_START_PENDING = 0x00000002
SERVICE_STOP_PENDING = 0x00000003
SERVICE_RUNNING = 0x00000004
SERVICE_CONTINUE_PENDING = 0x00000005
SERVICE_PAUSE_PENDING = 0x00000006
SERVICE_PAUSED = 0x00000007
READ_CONTROL=0x20000
SC_MANAGER_ALL_ACCESS=0xF003F
SERVICE_CONTROL_INTERROGATE = 0x00000004
typedef struct _SERVICE_STATUS
{
DWORD dwServiceType;
DWORD dwCurrentState;
DWORD dwControlsAccepted;
DWORD dwWin32ExitCode;
DWORD dwServiceSpecificExitCode;
DWORD dwCheckPoint;
DWORD dwWaitHint;
} SERVICE_STATUS, *LPSERVICE_STATUS;
*/
; [EXAMPLE]
#NoEnv
#KeyHistory, 0
ListLines, Off
SetBatchLines -1
SetControlDelay, -1
SetWinDelay, -1
DetectHiddenWindows, On
StringCaseSense, Off
SetFormat, Integer, D
;updates()
AW := A_IsUnicode ? "W" : "A", A_CharSize := 2**(A_IsUnicode=TRUE) ; found in my updates.ahk
states := "stopped|pending start|pending stop|running|pending continuation|pending pause|paused"
StringSplit, s, states, |
sName1 := "LanmanWorkstation", sName2 := "Workstation2"
msg1 := (i := GetServiceState(sName1)) ? s%i% : "nonexistant (error " ErrorLevel ")"
msg2 := (i := GetServiceState(sName2)) ? s%i% : "nonexistant (error " ErrorLevel ")"
MsgBox % "Service '" sName1 "' is " msg1 ".`nService '" sName2 "' is " msg2 "."
; [/EXAMPLE]
;================================================================
GetServiceState(dspName)
;================================================================
{
Global Ptr, AW, A_CharSize
; open service manager with sufficient rights
if !hSCM := DllCall("advapi32\OpenSCManager" AW, Ptr, 0, Ptr, 0, "UInt", 0x2003F, Ptr)
return 0, ErrorLevel := DllCall("GetLastError", "UInt")
; open the service
if !hSvc := DllCall("advapi32\OpenService" AW, Ptr, hSCM, "Str", dspName, "UInt", 0x000201FF, Ptr)
{
r := DllCall("GetLastError", "UInt")
; try to find the service name from the display name
DllCall("advapi32\GetServiceKeyName" AW, Ptr, hSCM, "Str", dspName, Ptr, 0, "UIntP", dwLen)
VarSetCapacity(svcName, A_CharSize*(++dwLen), 0)
DllCall("advapi32\GetServiceKeyName" AW, Ptr, hSCM, "Str", dspName, "Str", svcName, "UIntP", dwLen)
if !hSvc := DllCall("advapi32\OpenService" AW, Ptr, hSCM, "Str", svcName, "UInt", 0x000201FF, Ptr)
{
DllCall("advapi32\CloseServiceHandle", Ptr, hSCM)
return 0, ErrorLevel := DllCall("GetLastError", "UInt")
}
}
VarSetCapacity(serviceStatus,32,0) ; accomodate x64 alignment
DllCall("advapi32\ControlService", Ptr, hSvc, "UInt", 0x4, Ptr, &serviceStatus)
dwCurrentState := NumGet(serviceStatus, 4)
DllCall("advapi32\CloseServiceHandle", Ptr, hSvc) ; close the service
DllCall("advapi32\CloseServiceHandle", Ptr, hSCM) ; close the service manager
return dwCurrentState
}
Ran flawlessly. Win7Pro/AHK 1.1.25x Thx for your effort. Much appreciated!Drugwash wrote:Tried to update the function for AHK1.1+ / x64 compatibility.
Please test for correct behavior under x64, since I have no access to such machine.
Enjoy!Spoiler
Code: Select all
GetServiceKeyName(hSCManager, DisplayName)
{
DllCall("advapi32\GetServiceKeyName", "ptr", hSCManager, "str", DisplayName, "ptr", 0, "uint*", size)
size := VarSetCapacity(ServiceName, size * (A_IsUnicode ? 2 : 1), 0) + 1
if !(DllCall("advapi32\GetServiceKeyName", "ptr", hSCManager, "str", DisplayName, "str", ServiceName, "uint*", size))
throw Exception("GetServiceKeyName failed: " A_LastError, -1)
return ServiceName
}
Code: Select all
msg1 := (i := GetServiceState(sName1)) ? s%i% : "nonexistant (error " ErrorLevel ")"
msg2 := (i := GetServiceState(sName2)) ? s%i% : "nonexistant (error " ErrorLevel ")"
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; by tonne at https://autohotkey.com/board/topic/14159-check-state-of-windows-service/
; updated by Drugwash, 2017.07.20
/*
SERVICE_STOPPED = 0x00000001
SERVICE_START_PENDING = 0x00000002
SERVICE_STOP_PENDING = 0x00000003
SERVICE_RUNNING = 0x00000004
SERVICE_CONTINUE_PENDING = 0x00000005
SERVICE_PAUSE_PENDING = 0x00000006
SERVICE_PAUSED = 0x00000007
READ_CONTROL=0x20000
SC_MANAGER_ALL_ACCESS=0xF003F
SERVICE_CONTROL_INTERROGATE = 0x00000004
typedef struct _SERVICE_STATUS
{
DWORD dwServiceType;
DWORD dwCurrentState;
DWORD dwControlsAccepted;
DWORD dwWin32ExitCode;
DWORD dwServiceSpecificExitCode;
DWORD dwCheckPoint;
DWORD dwWaitHint;
} SERVICE_STATUS, *LPSERVICE_STATUS;
*/
; [EXAMPLE]
#NoEnv
#KeyHistory, 0
ListLines, Off
SetBatchLines -1
SetControlDelay, -1
SetWinDelay, -1
DetectHiddenWindows, On
StringCaseSense, Off
SetFormat, Integer, D
;updates()
AW := A_IsUnicode ? "W" : "A", A_CharSize := 2**(A_IsUnicode=TRUE) ; found in my updates.ahk
states := "stopped|pending start|pending stop|running|pending continuation|pending pause|paused"
StringSplit, s, states, |
sName1 := "PlugPlay", sName2 := "Workstation2"
msg1 := (i := GetServiceState(sName1)) ? s%i% : "nonexistant"
msg2 := (i := GetServiceState(sName2)) ? s%i% : "nonexistant"
MsgBox % "Service '" sName1 "' is " msg1 ".`nService '" sName2 "' is " msg2 "."
; [/EXAMPLE]
;================================================================
GetServiceState(dspName)
;================================================================
{
Global Ptr, PtrP, AW, A_CharSize
; open service manager with sufficient rights
if !hSCM := DllCall("advapi32\OpenSCManager" AW, Ptr, 0, Ptr, 0, "UInt", 0x2003F, Ptr)
return 0, ErrorLevel := DllCall("GetLastError", "UInt")
; open the service
if !hSvc := DllCall("advapi32\OpenService" AW, Ptr, hSCM, "Str", dspName, "UInt", 0x000201FF, Ptr)
{
r := DllCall("GetLastError", "UInt")
; try to find the service name from the display name
DllCall("advapi32\GetServiceKeyName" AW, Ptr, hSCM, "Str", dspName, Ptr, 0, PtrP, dwLen)
VarSetCapacity(svcName, A_CharSize*(++dwLen), 0)
DllCall("advapi32\GetServiceKeyName" AW, Ptr, hSCM, "Str", dspName, "Str", svcName, PtrP, dwLen)
if !hSvc := DllCall("advapi32\OpenService" AW, Ptr, hSCM, "Str", svcName, "UInt", 0x000201FF, Ptr)
{
DllCall("advapi32\CloseServiceHandle", Ptr, hSCM)
return 0, ErrorLevel := DllCall("GetLastError", "UInt")
}
}
VarSetCapacity(serviceStatus,32,0) ; accomodate x64 alignment
DllCall("advapi32\ControlService", Ptr, hSvc, "UInt", 0x4, Ptr, &serviceStatus)
dwCurrentState := NumGet(serviceStatus, 4)
DllCall("advapi32\CloseServiceHandle", Ptr, hSvc) ; close the service
DllCall("advapi32\CloseServiceHandle", Ptr, hSCM) ; close the service manager
return dwCurrentState
}