Sign in to follow this  
Followers 0
IsMaelhIT

_NTServices.au3

5 posts in this topic

#1 ·  Posted (edited)

Hi, I use _NTServices.au3 succesfully, but when I reboot it fail, I try to make sure 'winmgmt' is running,

I get error 3 ???

I believe it fail because it takes too long for 'winmgmt' to start after login.

$status = _ServiceStatus( 'winmgmt' )
If ( $status = 'stopped' Or $status = 'paused' ) _
And Not _ServiceStart( 'winmgmt' ) Then
;
$err = @error
If UBound( $error ) <= $err Then ReDim $error[ $err + 1 ]
$error[$err] = 'WMI Service Not Available'
call9_( 'panic_', $err )
;
EndIf
I have 0 knowledge of DllCall, where can I learn about ?

;=============================================================================
; Ideas from http://www.andreavb.com/tip060002.html, author: Andrea Tincani
; AutoIt scripts written by CatchFish, Feb 24th, 2006.
; 100% public domain. Feel free to remove the comments and claim it your own.
;=============================================================================
; 31/05/2012: some bugfixes in _ServiceStatus by Kasty. Now it doesn't ask for more
; privileges than actually needed, allowing it to work for processes that
; don't hold administrator or system rights. Also, when a service is not installed,
; it returns an empty string, rather than a number (makes comparison logic much easier).
;=============================================================================

#include-once

#include "SecurityConstants.au3"

;===============
; API Constants

;===============
Const $SERVICES_ACTIVE_DATABASE = "ServicesActive"
; Service Control
Const $SERVICE_CONTROL_STOP = 0x1
Const $SERVICE_CONTROL_PAUSE = 0x2
; Service State - for CurrentState
Const $SERVICE_STOPPED = 0x1
Const $SERVICE_START_PENDING = 0x2
Const $SERVICE_STOP_PENDING = 0x3
Const $SERVICE_RUNNING = 0x4
Const $SERVICE_CONTINUE_PENDING = 0x5
Const $SERVICE_PAUSE_PENDING = 0x6
Const $SERVICE_PAUSED = 0x7
; Service Control Manager object specific access types
Const $SC_MANAGER_CONNECT = 0x1
Const $SC_MANAGER_CREATE_SERVICE = 0x2
Const $SC_MANAGER_ENUMERATE_SERVICE = 0x4
Const $SC_MANAGER_LOCK = 0x8
Const $SC_MANAGER_QUERY_LOCK_STATUS = 0x10
Const $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x20
Const $SC_MANAGER_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, $SC_MANAGER_CONNECT, $SC_MANAGER_CREATE_SERVICE, $SC_MANAGER_ENUMERATE_SERVICE, _
$SC_MANAGER_LOCK, $SC_MANAGER_QUERY_LOCK_STATUS, $SC_MANAGER_MODIFY_BOOT_CONFIG)
; Service object specific access types
Const $SERVICE_QUERY_CONFIG = 0x1
Const $SERVICE_CHANGE_CONFIG = 0x2
Const $SERVICE_QUERY_STATUS = 0x4
Const $SERVICE_ENUMERATE_DEPENDENTS = 0x8
Const $SERVICE_START = 0x10
Const $SERVICE_STOP = 0x20
Const $SERVICE_PAUSE_CONTINUE = 0x40
Const $SERVICE_INTERROGATE = 0x80
Const $SERVICE_USER_DEFINED_CONTROL = 0x100
Const $SERVICE_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, $SERVICE_QUERY_CONFIG, $SERVICE_CHANGE_CONFIG, $SERVICE_QUERY_STATUS, _
$SERVICE_ENUMERATE_DEPENDENTS, $SERVICE_START, $SERVICE_STOP, $SERVICE_PAUSE_CONTINUE, $SERVICE_INTERROGATE, $SERVICE_USER_DEFINED_CONTROL)

;====================================================================
; DO NOT call these functions as they are used for internal affairs.
;====================================================================
Func __CreateStructServiceStatus()
; ServiceType, CurrentState, ControlsAccepted, Win32ExitCode, ServiceSpecificExitCode, CheckPoint, WaitHint
Return DllStructCreate("int;int;int;int;int;int;int")
EndFunc

Func __CloseServiceHandle($v_hSCObject, $v_hAdvAPI = "advapi32.dll")
Local $_Result = DllCall($v_hAdvAPI, "int", "CloseServiceHandle", "int", $v_hSCObject)
If @error = 0 Then
Return $_Result[0]
Else
SetError(@error)
Return 0
EndIf
EndFunc

Func __ControlService($v_hService, $v_dwControl, $v_lpServiceStatus, $v_hAdvAPI = "advapi32.dll")
Local $_Result = DllCall($v_hAdvAPI, "int", "ControlService", "int", $v_hService, "int", $v_dwControl, "ptr", $v_lpServiceStatus)
If @error = 0 Then
Return $_Result[0]
Else
SetError(@error)
Return 0
EndIf
EndFunc

Func __OpenSCManager($v_lpMachineName, $v_lpDatabaseName, $v_dwDesiredAccess, $v_hAdvAPI = "advapi32.dll")
Local $_Result = DllCall($v_hAdvAPI, "int", "OpenSCManagerA", "str", $v_lpMachineName, "str", $v_lpDatabaseName, "int", $v_dwDesiredAccess)
If @error = 0 Then
Return $_Result[0]
Else
SetError(@error)
Return 0
EndIf
EndFunc

Func __OpenService($v_hSCManager, $v_lpServiceName, $v_dwDesiredAccess, $v_hAdvAPI = "advapi32.dll")
Local $_Result = DllCall($v_hAdvAPI, "int", "OpenServiceA", "int", $v_hSCManager, "str", $v_lpServiceName, "int", $v_dwDesiredAccess)
If @error = 0 Then
Return $_Result[0]
Else
SetError(@error)
Return 0
EndIf
EndFunc

Func __QueryServiceStatus($v_hService, $v_lpServiceStatus, $v_hAdvAPI = "advapi32.dll")
Local $_Result = DllCall($v_hAdvAPI, "int", "QueryServiceStatus", "int", $v_hService, "ptr", $v_lpServiceStatus)
If @error = 0 Then
Return $_Result[0]
Else
SetError(@error)
Return 0
EndIf
EndFunc

Func __StartService($v_hService, $v_dwNumServiceArgs, $v_lpServiceArgVectors, $v_hAdvAPI = "advapi32.dll")
Local $_Result = DllCall($v_hAdvAPI, "int", "StartServiceA", "int", $v_hService, "int", $v_dwNumServiceArgs, "int", $v_lpServiceArgVectors)
If @error = 0 Then
Return $_Result[0]
Else
SetError(@error)
Return 0
EndIf
EndFunc

;===================================================================================================================
; Here below are 4 NT-service related functions: _ServiceStatus(), _ServicePause(), _ServiceStart(), _ServiceStop()
;===================================================================================================================
Func _ServiceStatus($v_ServiceName, $v_ComputerName = "")
Local $_ServiceStat = __CreateStructServiceStatus()
Local $_hSManager
Local $_hService
Local $_hServiceStatus
Local $_Result = ""
Local $_Err = 0
Local $_Ext = 0

$_hSManager = __OpenSCManager($v_ComputerName, $SERVICES_ACTIVE_DATABASE, $SC_MANAGER_ENUMERATE_SERVICE)
If $_hSManager <> 0 Then
     $_hService = __OpenService($_hSManager, $v_ServiceName, $SERVICE_QUERY_STATUS)
     If $_hService <> 0 Then
         $_hServiceStatus = __QueryServiceStatus($_hService, DllStructGetPtr($_ServiceStat))
         If $_hServiceStatus <> 0 Then
             Switch DllStructGetData($_ServiceStat, 2)
             Case $SERVICE_STOPPED
                 $_Result = "Stopped"
             Case $SERVICE_START_PENDING
                 $_Result = "Start Pending"
             Case $SERVICE_STOP_PENDING
                 $_Result = "Stop Pending"
             Case $SERVICE_RUNNING
                 $_Result = "Running"
             Case $SERVICE_CONTINUE_PENDING
                 $_Result = "Continue Pending"
             Case $SERVICE_PAUSE_PENDING
                 $_Result = "Pause Pending"
             Case $SERVICE_PAUSED
                 $_Result = "Paused"
Case Else
$_Err = 4
             EndSwitch
Else
$_Ext = @error
$_Err = 3
         EndIf
         __CloseServiceHandle($_hService)
Else
$_Ext = @error
$_Err = 2
     EndIf
     __CloseServiceHandle($_hSManager)
Else
$_Ext = @error
$_Err = 1
EndIf

Return SetError($_Err, $_Ext, $_Result)
EndFunc

Func _ServicePause($v_ServiceName, $v_ComputerName = "")
Local $_ServiceStatus = __CreateStructServiceStatus()
Local $_hSManager
Local $_hService
Local $_Result = 0
Local $_Err = 0
Local $_Ext = 0

$_hSManager = __OpenSCManager($v_ComputerName, $SERVICES_ACTIVE_DATABASE, $SC_MANAGER_ALL_ACCESS)
If $_hSManager <> 0 Then
     $_hService = __OpenService($_hSManager, $v_ServiceName, $SERVICE_ALL_ACCESS)
     If $_hService <> 0 Then
         $_Result = __ControlService($_hService, $SERVICE_CONTROL_PAUSE, DllStructGetPtr($_ServiceStatus))
If $_Result = 0 Then
$_Ext = @error
$_Err = 3
EndIf
         __CloseServiceHandle($_hService)
Else
$_Ext = @error
$_Err = 2
     EndIf
     __CloseServiceHandle($_hSManager)
Else
$_Ext = @error
$_Err = 1
EndIf

Return SetError($_Err, $_Ext, $_Result)
EndFunc

Func _ServiceStart($v_ServiceName, $v_ComputerName = "")
Local $_hSManager
Local $_hService
Local $_Result = 0
Local $_Err = 0
Local $_Ext = 0

$_hSManager = __OpenSCManager($v_ComputerName, $SERVICES_ACTIVE_DATABASE, $SC_MANAGER_ALL_ACCESS)
If $_hSManager <> 0 Then
     $_hService = __OpenService($_hSManager, $v_ServiceName, $SERVICE_ALL_ACCESS)
     If $_hService <> 0 Then
         $_Result = __StartService($_hService, 0, 0)
If $_Result = 0 Then
$_Ext = @error
$_Err = 3
EndIf
         __CloseServiceHandle($_hService)
Else
$_Ext = @error
$_Err = 2
     EndIf
     __CloseServiceHandle($_hSManager)
Else
$_Ext = @error
$_Err = 1
EndIf

Return SetError($_Err, $_Ext, $_Result)
EndFunc

Func _ServiceStop($v_ServiceName, $v_ComputerName = "")
Local $_ServiceStatus = __CreateStructServiceStatus()
Local $_hSManager
Local $_hService
Local $_Result = 0
Local $_Err = 0
Local $_Ext = 0

$_hSManager = __OpenSCManager($v_ComputerName, $SERVICES_ACTIVE_DATABASE, $SC_MANAGER_ALL_ACCESS)
If $_hSManager <> 0 Then
     $_hService = __OpenService($_hSManager, $v_ServiceName, $SERVICE_ALL_ACCESS)
     If $_hService <> 0 Then
         $_Result = __ControlService($_hService, $SERVICE_CONTROL_STOP, DllStructGetPtr($_ServiceStatus))
If $_Result = 0 Then
$_Ext = @error
$_Err = 3
EndIf
         __CloseServiceHandle($_hService)
Else
$_Ext = @error
$_Err = 2
     EndIf
     __CloseServiceHandle($_hSManager)
Else
$_Ext = @error
$_Err = 1
EndIf

Return SetError($_Err, $_Ext, $_Result)
EndFunc

;===============================================================================================================================================
; NT-service related funtions use internal service names. So use this function to convert the display name, as you see in the Services Manager.
;===============================================================================================================================================
Func _ToInternalServiceName($v_ServiceDisplayName)
Local $_Result = ""
Local $_NTSvcKey = "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices"
Local $_SubKey
Local $i = 1

If $v_ServiceDisplayName = "" Then Return $_Result
While 1
$_SubKey = RegEnumKey($_NTSvcKey, $i)
$i += 1
If @error = 0 Then
If RegRead($_NTSvcKey & "" & $_SubKey, "DisplayName") = $v_ServiceDisplayName Then
$_Result = $_SubKey
ExitLoop
EndIf
Else
ExitLoop
EndIf
WEnd

Return $_Result
EndFunc
Edited by IsMaelhIT

Share this post


Link to post
Share on other sites



:D

Share this post


Link to post
Share on other sites

Please wait at least 24 hours before bumping your thread.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

anyone ?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for all the replies :D

Edited by IsMaelhIT

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0