Query/Control service on x64

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Query/Control service on x64

Post by jassing »

The code below ( I don't know who wrote it, I tried to search the forums)
I'm compiling the code to x86, but need it to run on both x86 and x64.

The code works fine on x86, but on x64 it does not. (always telling me that a system shutdown is in progress)

Is this going to be doable? If so, any tips on how to get it going on x64 (while still compiling to 32bits)

thanks!
-j

Code: Select all

Structure Status
    dwServiceType.l  
    dwCurrentState.l 
    dwControlsAccepted.l  
    dwWin32ExitCode.l  
    dwServiceSpecificExitCode.l  
    dwCheckPoint.l  
    dwWaitHint.l
  EndStructure
  
Procedure.l QuerySvc(svcName.s)
	Protected.l nStatus
	Protected.Status service

	Protected.l LastError = 0
  Protected.l lpMachineName = #Null, lpDatabaseName = #Null,scmDesiredAccess = #SC_MANAGER_ALL_ACCESS,svcDesiredAccess = #SERVICE_ALL_ACCESS
	Protected.l scmHandler = OpenSCManager_(lpMachineName, lpDatabaseName, scmDesiredAccess)
	
	If scmHandler = #Null
	  LastError = GetLastError_()
	  dbgout("Open SCM failed with error " + Str(LastError))
	Else 
    Protected svcHandler = OpenService_(scmHandler, svcName, svcDesiredAccess)
    If svcHandler = #Null
      LastError = GetLastError_()
      dbgout("Open Service failed with error " + Str(LastError))
    Else
      Protected Result = QueryServiceStatus_(svcHandler, service)
      If Result = 0
        LastError = GetLastError_()
        dbgout("Query failed with error " + Str(LastError))
      Else
      	nStatus = service\dwCurrentState
      	dbgout("Service state: "+Str(nStatus))
      EndIf
      Result = CloseServiceHandle_(svcHandler)  
    EndIf
	  Result = CloseServiceHandle_(scmHandler)
  EndIf

  If LastError
  	nStatus = -1 * LastError
  EndIf
  
  ProcedureReturn nStatus
EndProcedure

OutputDebugString_("Query: "+Str(QuerySvc("mssqlserver")))
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Query/Control service on x64

Post by luis »

It works as it is here, compiled to 32 bit and running under x64 (win7)

BTW: You don't need to define the structure, it's already defined in PB as SERVICE_STATUS.

Code: Select all

Procedure.i QuerySvc(svcName.s)
   Protected nStatus
   Protected service.SERVICE_STATUS

   Protected LastError = 0
   Protected lpMachineName = #Null, lpDatabaseName = #Null,scmDesiredAccess = #SC_MANAGER_ALL_ACCESS,svcDesiredAccess = #SERVICE_ALL_ACCESS
   Protected scmHandler = OpenSCManager_(lpMachineName, lpDatabaseName, scmDesiredAccess)
   
   If scmHandler = #Null
     LastError = GetLastError_()
     Debug("Open SCM failed with error " + Str(LastError))
   Else
    Protected svcHandler = OpenService_(scmHandler, svcName, svcDesiredAccess)
    If svcHandler = #Null
      LastError = GetLastError_()
      Debug("Open Service failed with error " + Str(LastError))
    Else
      Protected Result = QueryServiceStatus_(svcHandler, service)
      If Result = 0
        LastError = GetLastError_()
        Debug("Query failed with error " + Str(LastError))
      Else
         nStatus = service\dwCurrentState
         Debug("Service state: "+Str(nStatus))
      EndIf
      Result = CloseServiceHandle_(svcHandler) 
    EndIf
     Result = CloseServiceHandle_(scmHandler)
  EndIf

  If LastError
     nStatus = -1 * LastError
  EndIf
 
  ProcedureReturn nStatus
EndProcedure

Debug("Query: "+Str(QuerySvc("LanmanWorkstation")))
Returns 4 (service is running)
"Have you tried turning it off and on again ?"
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Query/Control service on x64

Post by jassing »

Thanks luis.
What I discovered is that on some os's when windows updates are installed and need a reboot (iow: the nag screen to reboot after updates) the service query fails with "the system is shutting down" even tho it's not.
Post Reply