Jump to content
Sign in to follow this  
Guardiantorj

Runas Changes My Kickoff Application

Recommended Posts

Guardiantorj

I am trying to use a network script on all of my machines at a scheduled time. That is not the problem. My problem is that not all users have rights to change their settings BUT i figured it I run a runas script ahead of my other compiled script and had it run as an account with administrative privileges it should take care of that problem. But when I run my run as script (shown below) it makes the wireless network script open the local C:\ rather than Network Connections. Why would that be? To prove that is wasn't the runas script I tried it again using psExec (http://technet.microsoft.com/en-us/sysinte...s/bb897553.aspx) and it had the same results. What can I do to fix this or is there another direction I should be looking?

RUNAS SCRIPT:

Local $sUserName = "administrator"
Local $sPassword = "admin_password"
local $sDomain= "My_Domain"
local $sProgram = "C:\Documents and Settings\Username\Desktop\Wireless Scripts\Script\Compiled_Script.exe"
runas($sUserName,$sDomain,$sPassword,0, $sProgram)

Wireless Script: (Works when ran by user with admin rights by its self without any other applications kicking it off)

;Tested on XP Pro & Tablet Editions
AutoItSetOption("WinWaitDelay", 1);(milliseconds)
AutoItSetOption("TrayIconDebug", 1);0-off
AutoItSetOption("WinTitleMatchMode", 2);1-from the start, 2-any substring, 3-exact match
HotKeySet("{ESC}", "Terminate")

;set this to 0 to turn off the TrayTips and debug sleeps
Global $Debug_Sleep = 5000;(milliseconds)
;set it to something like 5000 if you want to read the script's TrayTips and glance at the windows
;you may have to manually close the TrayTip(s) generated by the wireless NIC itself
;to be able to see the ones made by the script


Global $SSID = "YourSSID"
Global $Authentication_Method = "WPA-PSK"
Global $Encryption_Method = "TKIP"
Global $Network_Key = "12345678"
Global $WinTitle, $WinText, $WinTimeout, $hwnd


;on my laptop
;if the wireless network interface card has been disabled, then the tab named "Wireless Networks"...
;... will not be present in the "Wireless Network Connection Properties" window
;the script attempts to deal with that


;on my laptop
;if the Wireless Zero Configuration service is not running, then the tab named "Wireless Networks"...
;... will not be present in the "Wireless Network Connection Properties" window
;the script attempts to deal with that


;Also, the connection might not be named "Wireless Network Connection"
;the script attempts to deal with that
;by only looking for the word "Properties" in the NIC's "Properties" window
;but wireless has to be somewhere in the connection's name <<<<<<<<<<<<<<<<<<<<<<<<<<<<<


;one option around not needing wireless in the connection's name
;would be to enable all NICs,
;press ENTER on each NIC entry
;if a status window appears AND text like "View Wireless Networks"
;then you might have the NIC of interest.
;and then there are users like me that have two wireless NICS............


;the worst scenario for this script is:
;Wireless Zero Configuration service stopped
;Wireless Zero Configuration service disabled
;wireless NIC disabled
;radio off on the wireless NIC


;the script attempts to handle all but the radio being off
;on my laptop - the configuration was able to complete with the radio off <<<<<<<<<<<<<<<<<<<<
;but the combination of all of the other things means that the
;tab named "Wireless Networks" is very slow to appear
;in the NIC's "Properties" window
;you may see that properties window appear and disappear repeatedly
;while the script keeps checking for that tab.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;constants for the ServiceControl UDF
;Author: SumTingWong aka pacman ???
;http://www.autoitscript.com/forum/index.php?showtopic=6487
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Global $STANDARD_RIGHTS_REQUIRED = 0x000F0000

; Service Control Manager access types
Global $SC_MANAGER_CONNECT = 0x0001
Global $SC_MANAGER_CREATE_SERVICE = 0x0002
Global $SC_MANAGER_ENUMERATE_SERVICE = 0x0004
Global $SC_MANAGER_LOCK = 0x0008
Global $SC_MANAGER_QUERY_LOCK_STATUS = 0x0010
Global $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020

Global $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 access types
Global $SERVICE_QUERY_CONFIG = 0x0001
Global $SERVICE_CHANGE_CONFIG = 0x0002
Global $SERVICE_QUERY_STATUS = 0x0004
Global $SERVICE_ENUMERATE_DEPENDENTS = 0x0008
Global $SERVICE_START = 0x0010
Global $SERVICE_STOP = 0x0020
Global $SERVICE_PAUSE_CONTINUE = 0x0040
Global $SERVICE_INTERROGATE = 0x0080
Global $SERVICE_USER_DEFINED_CONTROL = 0x0100

Global $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)

; Service controls
Global $SERVICE_CONTROL_STOP = 0x00000001
Global $SERVICE_CONTROL_PAUSE = 0x00000002
Global $SERVICE_CONTROL_CONTINUE = 0x00000003
Global $SERVICE_CONTROL_INTERROGATE = 0x00000004
Global $SERVICE_CONTROL_SHUTDOWN = 0x00000005
Global $SERVICE_CONTROL_PARAMCHANGE = 0x00000006
Global $SERVICE_CONTROL_NETBINDADD = 0x00000007
Global $SERVICE_CONTROL_NETBINDREMOVE = 0x00000008
Global $SERVICE_CONTROL_NETBINDENABLE = 0x00000009
Global $SERVICE_CONTROL_NETBINDDISABLE = 0x0000000A
Global $SERVICE_CONTROL_DEVICEEVENT = 0x0000000B
Global $SERVICE_CONTROL_HARDWAREPROFILECHANGE = 0x0000000C
Global $SERVICE_CONTROL_POWEREVENT = 0x0000000D
Global $SERVICE_CONTROL_SESSIonchange = 0x0000000E

; Service types
Global $SERVICE_KERNEL_DRIVER = 0x00000001
Global $SERVICE_FILE_SYSTEM_DRIVER = 0x00000002
Global $SERVICE_ADAPTER = 0x00000004
Global $SERVICE_RECOGNIZER_DRIVER = 0x00000008
Global $SERVICE_DRIVER = BitOR($SERVICE_KERNEL_DRIVER, _
        $SERVICE_FILE_SYSTEM_DRIVER, _
        $SERVICE_RECOGNIZER_DRIVER)
Global $SERVICE_WIN32_OWN_PROCESS = 0x00000010
Global $SERVICE_WIN32_SHARE_PROCESS = 0x00000020
Global $SERVICE_WIN32 = BitOR($SERVICE_WIN32_OWN_PROCESS, _
        $SERVICE_WIN32_SHARE_PROCESS)
Global $SERVICE_INTERACTIVE_PROCESS = 0x00000100
Global $SERVICE_TYPE_ALL = BitOR($SERVICE_WIN32, _
        $SERVICE_ADAPTER, _
        $SERVICE_DRIVER, _
        $SERVICE_INTERACTIVE_PROCESS)

; Service start types
Global $SERVICE_BOOT_START = 0x00000000
Global $SERVICE_SYSTEM_START = 0x00000001
Global $SERVICE_AUTO_START = 0x00000002
Global $SERVICE_DEMAND_START = 0x00000003
Global $SERVICE_DISABLED = 0x00000004

; Service error control
Global $SERVICE_ERROR_IGNORE = 0x00000000
Global $SERVICE_ERROR_NORMAL = 0x00000001
Global $SERVICE_ERROR_SEVERE = 0x00000002
Global $SERVICE_ERROR_CRITICAL = 0x00000003
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;for debugging;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Do;
;   _StopService("", "WZCSVC")
;   Sleep(100)
;Until Not _ServiceRunning("", "WZCSVC")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

If $Debug_Sleep > 0 Then TrayTip("Checking to see if...", "...the ""Wireless Zero Configuration"" service is running.", 100)
;if the service has been disabled - then this should fix that
RunWait("sc config WZCSVC start= auto", "", @SW_HIDE)
;if WZCSVC was already set to automatic - the code above won't hurt that
;if WZCSVC was already running - the code above won't hurt that
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)
If Not _ServiceRunning("", "WZCSVC") Then
    If $Debug_Sleep > 0 Then TrayTip("Attempting...", "...to start the ""Wireless Zero Configuration"" service.", 100)
   ;now the service should start unless it is completely hosed
    For $i = 1 To 10
        _StartService("", "WZCSVC")
        Sleep(3000)
        If _ServiceRunning("", "WZCSVC") Then ExitLoop
    Next
    If Not _ServiceRunning("", "WZCSVC") Then
        MsgBox(0, "AutoIt", "The Wireless Zero Configuration service could not be started.")
        Exit
    EndIf
EndIf
If $Debug_Sleep > 0 Then
    Sleep($Debug_Sleep)
    TrayTip("Its ALIVE!!!...", "...the ""Wireless Zero Configuration"" service is running.", 100)
    Sleep($Debug_Sleep)
EndIf


If $Debug_Sleep > 0 Then
    TrayTip("Opening the Network Connections explorer window...", "...and ""looking"" for the word wireless.", 100)
    Sleep($Debug_Sleep)
EndIf
Run("control ncpa.cpl")
$hwnd = _Window_Check("Network Connections", "Network Connections", 10)
If Not ControlSend($hwnd, "Network Connections", "SysListView321", "wireless") Then
    MsgBox(0, "AutoIt", "Control could not be found.")
    Exit
EndIf
$ret1 = ControlListView($hwnd, "Network Connections", "SysListView321", "GetSelected")
$ret2 = ControlListView($hwnd, "Network Connections", "SysListView321", "GetText", $ret1)
If StringInStr($ret2, "wireless") = 0 Then
    MsgBox(0, "AutoIt", "No connection named with ""wireless"" in its name could be found.")
    Exit
EndIf
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


;netsh did not work here when I tried to enable a disabled NIC
;http://support.microsoft.com/kb/262265
;the WMI service is off - so this seemed to be the simplest way
;since the window was open any way......
If $Debug_Sleep > 0 Then
    TrayTip("Enabling - if need be...", "...the first wireless connection entry located.", 100)
    Sleep($Debug_Sleep)
EndIf
For $i = 1 To 30
    If $i = 29 Then
        MsgBox(0, "AutoIt", "This does not appear to be a ""wireless"" card after all.")
        Exit
    EndIf
   ;this ENTER should either enable the NIC
   ;or bring up the status window
   ;or bring up the window that lets you choose a wireless network - if the radio is off
   ;or ???
    ControlSend($hwnd, "Network Connections", "SysListView321", "wireless")
    ControlSend($hwnd, "Network Connections", "SysListView321", "{ENTER}")
    If WinWait("", "Enabl", 1) Then ExitLoop;catch Enabling... or Enabled
    If WinWait("Status", "View Wireless Networks", 1) Then ExitLoop
    If WinWait("", "Choose a wireless network", 1) Then ExitLoop
Next
WinClose("Status", "View Wireless Networks")
WinClose("", "Choose a wireless network")
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Opening...", "...the ""Properties"" of first wireless connection entry located.", 100)
    Sleep($Debug_Sleep)
EndIf
For $i = 1 To 30
    If $i = 29 Then
        MsgBox(0, "AutoIt", "The Wireless Zero Configuration service does not seem to be ready to be configured. Run the program again.")
        Exit
    EndIf
   ;this control was found in the code above - probably don't need error checking for the control here
    ControlSend("Network Connections", "Network Connections", "SysListView321", "!{ENTER}");brings up the properties
    Sleep(1000)
   ;if the "Wireless Networks" tab is missing, then keep looping
    If WinExists("Properties", "General") Then
        ControlCommand("Properties", "General", "SysTabControl321", "TabRight", "")
        Sleep(500)
        If WinExists("Properties", "Wireless Networks") Then
            $hwnd = WinGetHandle("Properties", "Wireless Networks");take the $hwnd into the next blocks of code
            ExitLoop
        Else
            WinClose("Properties")
        EndIf
    EndIf
Next
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


;if the "Wireless Zero Configuration" service had been stopped,
;it might take a while for this radio button to be enabled
For $i = 1 To 1000
    If $Debug_Sleep > 0 Then TrayTip("Waiting...", "...for the ""Wireless Zero Configuration"" service to be ready.", 100)
    If ControlCommand($hwnd, "Wireless Networks", "Button1", "IsEnabled", "") Then ExitLoop
    Sleep(100)
    If $i = 99 Then
        MsgBox(0, "AutoIt", "The option to check the ""Use Windows to configure my wireless network settings"" is not enabled.")
        Exit
    EndIf
Next
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Putting a check by - if need be...", "...""Use Windows to configure my wireless network settings""", 100)
    Sleep($Debug_Sleep)
EndIf
;the next line will only check the radio button
;it does not toggle the state to unchecked if it was already checked
ControlCommand($hwnd, "Wireless Networks", "Button1", "Check", "")
If Not ControlCommand($hwnd, "Wireless Networks", "Button1", "IsChecked", "") Then
    MsgBox(0, "AutoIt", """Use Windows to configure my wireless network settings"" was not checked.")
    Exit
EndIf
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Clicking...", "...on ""Add..."".", 100)
    Sleep($Debug_Sleep)
EndIf
ControlFocus($hwnd, "Wireless Networks", "Button7")
If Not ControlClick($hwnd, "Wireless Networks", "Button7") Then
    MsgBox(0, "AutoIt", "Control could not be found.")
    Exit
EndIf
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Entering...", "...the ""Network name (SSID)"".", 100)
    Sleep($Debug_Sleep)
EndIf
$hwnd = _Window_Check("Wireless network properties", "Wireless network key", 10)
If Not ControlSetText($hwnd, "Association", "Edit1", $SSID) Then
    MsgBox(0, "AutoIt", "Control could not be found.")
    Exit
Else
    If Not ControlGetText($hwnd, "Association", "Edit1") == $SSID Then
        MsgBox(0, "AutoIt", "The SSID text was not set properly.")
        Exit
    EndIf
EndIf
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Putting a check by - if need be...", "...""Connect even if this network is not broadcasting"".", 100)
    Sleep($Debug_Sleep)
EndIf
;the next line will only check the radio button
;it does not toggle the state to unchecked if it was already checked
ControlCommand($hwnd, "Wireless network key", "Button1", "Check", "")
For $i = 1 To 100
    Sleep(100)
    If ControlCommand($hwnd, "Wireless network key", "Button1", "IsChecked", "") Then ExitLoop
Next
If Not ControlCommand($hwnd, "Wireless network key", "Button1", "IsChecked", "") Then
    MsgBox(0, "AutoIt", """Connect even if this network is not broadcasting"" was not checked.")
    Exit
EndIf
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Selecting...", "...the ""Network Authentication"" method.", 100)
    Sleep($Debug_Sleep)
EndIf
ControlCommand($hwnd, "Wireless network key", "ComboBox1", "SelectString", $Authentication_Method)
If Not ControlCommand($hwnd, "Wireless network key", "ComboBox1", "GetCurrentSelection", "") == $Authentication_Method Then
    MsgBox(0, "AutoIt", "The authentication method was not set properly.")
    Exit
EndIf
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Selecting...", "...the ""Data encryption"" method.", 100)
    Sleep($Debug_Sleep)
EndIf
ControlCommand($hwnd, "Wireless network key", "ComboBox2", "SelectString", $Encryption_Method)
If Not ControlCommand($hwnd, "Wireless network key", "ComboBox2", "GetCurrentSelection", "") == $Encryption_Method Then
    MsgBox(0, "AutoIt", "The encryption method was not set properly.")
    Exit
EndIf
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Entering...", "...the ""Network key"" and confirming it.", 100)
    Sleep($Debug_Sleep)
EndIf
;not much error checking here
ControlSetText($hwnd, "Wireless network key", "Edit2", $Network_Key)
ControlSetText($hwnd, "Wireless network key", "Edit3", $Network_Key)
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Clicking...", "...on ""OK"" in the ""Wireless network properties"" window.", 100)
    Sleep($Debug_Sleep)
EndIf
ControlClick($hwnd, "", "Button5")
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Clicking...", "...on ""OK"" in the NIC's ""Properties"" window.", 100)
    Sleep($Debug_Sleep)
EndIf
$hwnd = _Window_Check("Properties", "Wireless Networks", 10)
ControlClick($hwnd, "Wireless Networks", "Button11")
WinWaitClose($hwnd)
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


If $Debug_Sleep > 0 Then
    TrayTip("Closing the Network Connections explorer window...", "...and hoping that this all works.", 100)
    Sleep($Debug_Sleep)
EndIf
WinClose("Network Connections")
If $Debug_Sleep > 0 Then Sleep($Debug_Sleep)


Func _Window_Check($WinTitle, $WinText, $WinTimeout)
    If WinWait($WinTitle, $WinText, $WinTimeout) = 0 Then
        MsgBox(0, "AutoIt", "The window named " & $WinTitle & " with the text " & $WinText & " did not open in time.")
        Exit
    EndIf

    If WinActivate($WinTitle, $WinText) = 0 Then
        MsgBox(0, "AutoIt", "The window named " & $WinTitle & " with the text " & $WinText & " can not be found or cannot be activated.")
        Exit
    EndIf

    If WinWaitActive($WinTitle, $WinText, $WinTimeout) = 0 Then
        MsgBox(0, "AutoIt", "The window named " & $WinTitle & " with the text " & $WinText & " did not activate in time.")
        Exit
    EndIf

    $hwnd = WinGetHandle($WinTitle, $WinText)
    If $hwnd = "" Then
        MsgBox(0, "AutoIt", "Could not get the window handle for the window named " & $WinTitle & " with the text " & $WinText & ".")
        Exit
    Else
        Return $hwnd
    EndIf
EndFunc  ;==>_Window_Check


;===============================================================================
; Description:   Starts a service on a computer
; Parameters:   $sComputerName - name of the target computer. If empty, the local computer name is used
;               $sServiceName - name of the service to start
; Requirements:  None
; Return Values: On Success - 1
;               On Failure - 0 and @error is set to extended Windows error code
; Note:       This function does not check to see if the service has started successfully
;===============================================================================
Func _StartService($sComputerName, $sServiceName)
    Local $hAdvapi32
    Local $hKernel32
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    $hAdvapi32 = DllOpen("advapi32.dll")
    If $hAdvapi32 = -1 Then Return 0
    $hKernel32 = DllOpen("kernel32.dll")
    If $hKernel32 = -1 Then Return 0
    $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", _
            "str", $sComputerName, _
            "str", "ServicesActive", _
            "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
        $arRet = DllCall($hKernel32, "long", "GetLastError")
        $lError = $arRet[0]
    Else
        $hSC = $arRet[0]
        $arRet = DllCall($hAdvapi32, "long", "OpenService", _
                "long", $hSC, _
                "str", $sServiceName, _
                "long", $SERVICE_START)
        If $arRet[0] = 0 Then
            $arRet = DllCall($hKernel32, "long", "GetLastError")
            $lError = $arRet[0]
        Else
            $hService = $arRet[0]
            $arRet = DllCall($hAdvapi32, "int", "StartService", _
                    "long", $hService, _
                    "long", 0, _
                    "str", "")
            If $arRet[0] = 0 Then
                $arRet = DllCall($hKernel32, "long", "GetLastError")
                $lError = $arRet[0]
            EndIf
            DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hService)
        EndIf
        DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    DllClose($hAdvapi32)
    DllClose($hKernel32)
    If $lError <> -1 Then
        SetError($lError)
        Return 0
    EndIf
    Return 1
EndFunc  ;==>_StartService


;===============================================================================
; Description:   Stops a service on a computer
; Parameters:   $sComputerName - name of the target computer. If empty, the local computer name is used
;               $sServiceName - name of the service to stop
; Requirements:  None
; Return Values: On Success - 1
;               On Failure - 0 and @error is set to extended Windows error code
; Note:       This function does not check to see if the service has stopped successfully
;===============================================================================
Func _StopService($sComputerName, $sServiceName)
    Local $hAdvapi32
    Local $hKernel32
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    $hAdvapi32 = DllOpen("advapi32.dll")
    If $hAdvapi32 = -1 Then Return 0
    $hKernel32 = DllOpen("kernel32.dll")
    If $hKernel32 = -1 Then Return 0
    $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", _
            "str", $sComputerName, _
            "str", "ServicesActive", _
            "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
        $arRet = DllCall($hKernel32, "long", "GetLastError")
        $lError = $arRet[0]
    Else
        $hSC = $arRet[0]
        $arRet = DllCall($hAdvapi32, "long", "OpenService", _
                "long", $hSC, _
                "str", $sServiceName, _
                "long", $SERVICE_STOP)
        If $arRet[0] = 0 Then
            $arRet = DllCall($hKernel32, "long", "GetLastError")
            $lError = $arRet[0]
        Else
            $hService = $arRet[0]
            $arRet = DllCall($hAdvapi32, "int", "ControlService", _
                    "long", $hService, _
                    "long", $SERVICE_CONTROL_STOP, _
                    "str", "")
            If $arRet[0] = 0 Then
                $arRet = DllCall($hKernel32, "long", "GetLastError")
                $lError = $arRet[0]
            EndIf
            DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hService)
        EndIf
        DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    DllClose($hAdvapi32)
    DllClose($hKernel32)
    If $lError <> -1 Then
        SetError($lError)
        Return 0
    EndIf
    Return 1
EndFunc  ;==>_StopService


;===============================================================================
; Description:   Checks if a service is running on a computer
; Parameters:   $sComputerName - name of the target computer. If empty, the local computer name is used
;               $sServiceName - name of the service to check
; Requirements:  None
; Return Values: On Success - 1
;               On Failure - 0
; Note:       This function relies on the fact that only a running service responds
;               to a SERVICE_CONTROL_INTERROGATE control code. Check the ControlService
;               page on MSDN for limitations with using this method.
;===============================================================================
Func _ServiceRunning($sComputerName, $sServiceName)
    Local $hAdvapi32
    Local $arRet
    Local $hSC
    Local $hService
    Local $bRunning = 0

    $hAdvapi32 = DllOpen("advapi32.dll")
    If $hAdvapi32 = -1 Then Return 0
    $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", _
            "str", $sComputerName, _
            "str", "ServicesActive", _
            "long", $SC_MANAGER_CONNECT)
    If $arRet[0] <> 0 Then
        $hSC = $arRet[0]
        $arRet = DllCall($hAdvapi32, "long", "OpenService", _
                "long", $hSC, _
                "str", $sServiceName, _
                "long", $SERVICE_INTERROGATE)
        If $arRet[0] <> 0 Then
            $hService = $arRet[0]
            $arRet = DllCall($hAdvapi32, "int", "ControlService", _
                    "long", $hService, _
                    "long", $SERVICE_CONTROL_INTERROGATE, _
                    "str", "")
            $bRunning = $arRet[0]
            DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hService)
        EndIf
        DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    DllClose($hAdvapi32)
    Return $bRunning
EndFunc  ;==>_ServiceRunning


Func Terminate()
    Exit
EndFunc  ;==>Terminate

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  

×