Quote:
Here is the function that uses WinServ to restart a service. This function also restart all the dependent services too. This i similar to service.msc. It stop the dependent services restart our target service and then start the dependent service in reverse order. If you have working WinServ this function should work fine. It uses StrGet() function which is availabe in unicode version of autohotkey. If you can find replacement of this function it will work in normal version too.
Just call RestartService("servicedisplayname of servicekeyname")
State argument is not necessary it has same values as the state argument of EnumDependentServices function. State=1 means restart active dependent service only.
Code:
RestartService(ServiceName, State=1)
{
VarSetCapacity(SvcName ,256)
schSCManager := DllCall("AdvApi32\OpenSCManager","int",0,"int",0,"uint",0xF003F)
ServiceName := DllCall("Advapi32\GetServiceKeyName", "Uint", schSCManager, "Uint", &ServiceName, "Str", SvcName, "UintP", 256) ? SvcName : ServiceName
schService := DllCall("Advapi32\OpenService", "Uint", schSCManager, "Uint", &ServiceName, "Uint", 0xF01FF)
if !schService{
VarSetCapacity(LastErrMsg, 1024)
DllCall("FormatMessage", "Uint", 0x1000, "Uint", 0, "Uint", LastErrNum:=A_LastError != 123 ? A_LastError : 1060, "Uint", 0, "Str", LastErrMsg, "Uint", 1024, "Uint", 0)
MsgBox, 262160,Error,Error %LastErrNum%: %LastErrMsg%
return
}
VarSetCapacity(pcbBytesNeeded, 4,0)
VarSetCapacity(lpServicesReturned, 4,0)
DllCall("Advapi32\EnumDependentServices","Uint",schService,"Uint",State,"Uint",0,"Uint",0,"Uint",&pcbBytesNeeded,"Uint",&lpServicesReturned)
cbBufSize:=NumGet(pcbBytesNeeded,0,"Uint")
VarSetCapacity(lpServices, cbBufSize,0)
DllCall("Advapi32\EnumDependentServices","Uint",schService,"Uint",State,"Uint",&lpServices,"Uint",cbBufSize,"Uint",&pcbBytesNeeded,"Uint",&lpServicesReturned)
numServices:=NumGet(lpServicesReturned,0,"Uint")
Loop,%numServices%
{
serviceStruct:=NumGet(lpServices,36*(A_Index-1),"Uint")
dpndService:=strget(serviceStruct+0,-1)
If WinServ(dpndService)
WinServ(dpndService,False)
}
If WinServ(ServiceName)
WinServ(ServiceName,False)
If !WinServ(ServiceName)
WinServ(ServiceName,True)
Loop,%numServices%
{
serviceStruct:=NumGet(lpServices,36*((numServices-1)-(A_Index-1)),"Uint")
dpndService:=strget(serviceStruct+0,-1)
If !WinServ(dpndService)
WinServ(dpndService,true)
}
DllCall("AdvApi32\CloseServiceHandle","Uint",schService)
DllCall("AdvApi32\CloseServiceHandle","Uint",schSCManager)
}