Sign in to follow this  
Followers 0
gil900

RetrieveRunningServices - get all ruuning services with "Display name" and "Short name"

3 posts in this topic

#1 ·  Posted (edited)

Hello,
I wrote a function that get all running services with "Display name" and "Short name" in array

This function works with ComSpec (command line..) unlike another function I found here.

The reason I wrote this function is because the another function that woks in another method was started to work >very slow.

 

So i worte a new function that works with ComSpec method.

small thanks to jguinch who wrote code that use that method. but his code has a >serious bug so i had to re-worte the function again.

#include <Array.au3>

$test = _RetrieveRunningServices()
_ArrayDisplay($test)


; #FUNCTION# ====================================================================================================================
; Name ..........: _RetrieveRunningServices
; Description ...: Get all running services with "Display name" and "Short name" in array
; Syntax ........: _RetrieveRunningServices([$Sort = 0])
; Parameters ....: $Sort                - [optional] If $sort = 1 then the function will sort the list by "Display name"
; Return values .: Array:
;                   If error then the array will be empty and index [0][0] in the array will be = 0
;                   If not error then the array will not be empty and [0][0] will be greater than 0.
;                   [0][0] = number of all running services.
; Author ........: gil900
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: yes
; ===============================================================================================================================
Func _RetrieveRunningServices($Sort = 0)
    Local $Output[1][2] , $iPid
    $Output[0][0] = 0
    $iPid = Run(@ComSpec & " /c sc query", @SystemDir, @SW_HIDE, 2)
    If $iPid > 0 Then
        ProcessWaitClose($iPid)
        Local $StdoutRead = StdoutRead($iPid)
        If Not @error Then
            Local $add = 0 , $n = 1 , $aStdoutRead , $tmp
            $aStdoutRead = StringSplit($StdoutRead,@CRLF,1)
            For $a = 1 To $aStdoutRead[0]
                $tmp = StringLeft($aStdoutRead[$a],1)
                If $tmp <> " " And $tmp <> "" Then
                    $tmp = StringSplit($aStdoutRead[$a],": ",1)
                    If $tmp[0] = 2 Then
                        If $add = 0 Then
                            ReDim $Output[$n+1][2]
                            $Output[$n][0] = $tmp[$tmp[0]]
                            $add = 1
                        Else
                            $Output[$n][1] = $tmp[$tmp[0]]
                            $Output[0][0] = $n
                            $n += 1
                            $add = 0
                        EndIf
                    EndIf
                EndIf
            Next
            If $Sort = 1 And $Output[0][0] > 0 Then _ArraySort($Output, 0, 1, 0, 1)
        EndIf
    EndIf
    Return $Output
EndFunc

I hope you find it useful!

Edited by gil900

Share this post


Link to post
Share on other sites



Works fine for me

SciTE v.3.3.7.0 / OS:WIN_81/  CPU:X64 OS:X64  

Thank you!

Keep reporting

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