AutoHotkey Community

It is currently May 27th, 2012, 10:25 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 17 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: RestartService
PostPosted: April 20th, 2011, 1:10 pm 
Offline

Joined: April 20th, 2011, 12:56 pm
Posts: 1
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)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 22nd, 2011, 6:46 pm 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
I've used this function MANY times and figured I ought to throw a big
thank you out there to not only the folks that brought us this function
(Hardeep), but also to those that have continued to make sure it works
throughout the evolution of AutoHotkey.

Thanks!

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page Previous  1, 2

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot], XX0 and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group