Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Enumerate NTServices

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Enumerate NTServices

    The followings are for public domain:
    Code:
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    
    '----------------------------------------------------------------------------------------------------------------------------------------
    ' this code is based on Microsoft MSKB Q183478
    '----------------------------------------------------------------------------------------------------------------------------------------
    FUNCTION IsServiceRunning(lngServiceType AS LONG, BYVAL strText AS STRING, lstServiceName AS LONG, lstDisplayName AS LONG) AS LONG
        LOCAL hSCManager                    AS DWORD
        LOCAL x                             AS LONG
        LOCAL dx                            AS ENUM_SERVICE_STATUS
        LOCAL lpEnumServiceStatus()         AS ENUM_SERVICE_STATUS
        LOCAL lngBytesNeeded                AS LONG
        LOCAL lpServicesReturned            AS DWORD
        LOCAL hNextUnreadEntry              AS DWORD
        LOCAL lngStructsNeeded              AS LONG
        LOCAL lngServiceStatusInfoBuffer    AS LONG
        LOCAL strServiceName                AS ASCIIZ * 250
        LOCAL i                             AS LONG
    
        'local lngServiceType as long (either %SERVICE_ACTIVE or
        '                                       %SERVICE_ACTIVE Or %SERVICE_INACTIVE
        'strText is the name to search, it is always the ServiceName
    
        hSCManager = OpenSCManager(BYVAL 0, BYVAL 0, %SC_MANAGER_ENUMERATE_SERVICE)
        IF hSCManager THEN
            hNextUnreadEntry = 0
            x = EnumServicesStatus(hSCManager, %SERVICE_WIN32, lngServiceType, dx, &H0, _
                     lngBytesNeeded, lpServicesReturned, hNextUnreadEntry)
    
           'We should receive %MORE_DATA error.
           IF NOT GetLastError() = %ERROR_MORE_DATA THEN
                EXIT FUNCTION
           ELSE
    
                'Calculate the number of structures needed
                lngStructsNeeded = lngBytesNeeded / LEN(lpEnumServiceStatus(0)) + 1
                'Redimension the array according to our calculation
                REDIM lpEnumServiceStatus(lngStructsNeeded - 1)
                'Get buffer size in bytes
                lngServiceStatusInfoBuffer = lngStructsNeeded * LEN(lpEnumServiceStatus(0))
                'Get services information starting entry 0
                hNextUnreadEntry = 0
                x = EnumServicesStatus(hSCManager, _
                                       %SERVICE_WIN32, _
                                       lngServiceType, _
                                       lpEnumServiceStatus(0), _
                                       lngServiceStatusInfoBuffer, _
                                       lngBytesNeeded, _
                                       lpServicesReturned, _
                                       hNextUnreadEntry)
                IF x = 0 THEN
                    'failed, call GetLastError why
                ELSE
                    'clear the listboxes
                    IF lstServiceName > 0 THEN SendMessage lstServiceName, %LB_RESETCONTENT, 0, 0
                    IF lstDisplayName > 0 THEN SendMessage lstDisplayName, %LB_RESETCONTENT, 0, 0
                    FOR i = 0 TO lpServicesReturned - 1
                        x = lstrcpy(strServiceName, BYVAL lpEnumServiceStatus(i).lpDisplayName)
                        IF lstDisplayName > 0 THEN SendMessage lstDisplayName, %LB_ADDSTRING, 0, VARPTR(strServiceName)
    
                        x = lstrcpy(strServiceName, BYVAL lpEnumServiceStatus(i).lpServiceName)
                        IF lstServiceName > 0 THEN SendMessage lstServiceName, %LB_ADDSTRING, 0, VARPTR(strServiceName)
                        IF strText > "" THEN
                            IF LCASE$(strText) = LCASE$(strServiceName) THEN FUNCTION = %TRUE
                        END IF
                    NEXT
                END IF
            END IF
            'Clean up.
            CloseServiceHandle hSCManager
    
        END IF
    END FUNCTION
    
    CALLBACK FUNCTION DlgProc()
        LOCAL hndl1 AS LONG
        LOCAL hndl2 AS LONG
        LOCAL strSearch AS STRING
        SELECT CASE CBMSG
            CASE %WM_COMMAND
                IF CBCTLMSG = %BN_CLICKED THEN
                    SELECT CASE CBCTL
                        CASE 103    'active services only
                            CONTROL HANDLE CBHNDL, 101 TO hndl1
                            CONTROL HANDLE CBHNDL, 102 TO hndl2
                            IsServiceRunning %SERVICE_ACTIVE, "", hndl1, hndl2
                        CASE 104    'all services
                            CONTROL HANDLE CBHNDL, 101 TO hndl1
                            CONTROL HANDLE CBHNDL, 102 TO hndl2
                            IsServiceRunning %SERVICE_ACTIVE OR %SERVICE_INACTIVE, "", hndl1, hndl2
                        CASE 105    'is running
                            strSearch = INPUTBOX$("Search for this service name")
                            IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE, strSearch, 0, 0) THEN
                                MSGBOX "Yes"
                            ELSE
                                MSGBOX "No"
                            END IF
                        CASE 106    'is installed
                            strSearch = INPUTBOX$("Search for this service name")
                            IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE OR %SERVICE_INACTIVE, strSearch, 0, 0) THEN
                                MSGBOX "Yes"
                            ELSE
                                MSGBOX "No"
                            END IF
                    END SELECT
                END IF
        END SELECT
    END FUNCTION
    
    FUNCTION PBMAIN()
        LOCAL hDlg AS LONG
        DIALOG NEW %NULL, "Enum Services", , , 300, 200, %WS_VISIBLE OR %WS_SYSMENU TO hDlg
        CONTROL ADD LISTBOX, hDlg, 101, , 2, 2, 140, 160
        CONTROL ADD LISTBOX, hDlg, 102, , 156, 2, 140, 160
        CONTROL ADD BUTTON, hDlg, 103, "Active Services", 5, 162, 60, 14
        CONTROL ADD BUTTON, hDlg, 104, "All Services", 233, 162, 60, 14
        CONTROL ADD BUTTON, hDlg, 105, "Is Running?", 70, 162, 60, 14
        CONTROL ADD BUTTON, hDlg, 106, "Is Installed?", 169, 162, 60, 14
        DIALOG SHOW MODAL hDlg, CALL DlgProc
    END FUNCTION
    ------------------
    * UUB [code] tags corrected by Adminstrator
Working...
X