Obtener Servicios [C]

Iniciado por Expermicid, Diciembre 26, 2013, 12:56:50 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Diciembre 26, 2013, 12:56:50 AM Ultima modificación: Diciembre 26, 2013, 12:59:13 AM por Expermicid
Continuando con un proyecto.

Código: c
#include "windows.h"
#include <stdio.h>

int main()
{
    DWORD i;
    SC_HANDLE sc = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE|GENERIC_READ);

    if (sc != NULL) {
        LPENUM_SERVICE_STATUS lpES = 0;
        DWORD dwBuffSize = 0, dwBytesNeeded = 0, dwServicesReturned = 0, dwResumeHandle = 0;

        BOOL bR = EnumServicesStatus(sc,SERVICE_WIN32,SERVICE_STATE_ALL,lpES,dwBuffSize,&dwBytesNeeded,&dwServicesReturned,&dwResumeHandle);

        if ( bR != FALSE ) {
            CloseServiceHandle(sc);
        }

        lpES = (LPENUM_SERVICE_STATUS) LocalAlloc(LPTR, dwBytesNeeded);
        if ( lpES == 0 ) {
            CloseServiceHandle(sc);
        }

        bR = EnumServicesStatus(sc,SERVICE_WIN32,SERVICE_STATE_ALL,lpES,dwBytesNeeded,&dwBytesNeeded,&dwServicesReturned,&dwResumeHandle);

        LPENUM_SERVICE_STATUS lpCE = lpES;

        for ( i = 0; i < dwServicesReturned; i++ ) {

            printf(lpCE->lpServiceName);
            printf("       ");
            printf(lpCE->lpDisplayName);
            printf("       ");

            SERVICE_STATUS lpS = lpCE->ServiceStatus;
            if ( lpS.dwCurrentState == SERVICE_CONTINUE_PENDING ) {
                printf("Pendiente");
            }
            if ( lpS.dwCurrentState ==  SERVICE_PAUSE_PENDING) {
                printf("Pendiente");
            }
            if ( lpS.dwCurrentState ==  SERVICE_PAUSED) {
                printf("Pausado");
            }
            if ( lpS.dwCurrentState ==  SERVICE_RUNNING) {
                printf("Corriendo");
            }
            if ( lpS.dwCurrentState ==  SERVICE_START_PENDING) {
                printf("Iniciando");
            }
            if ( lpS.dwCurrentState ==  SERVICE_STOP_PENDING) {
                printf("Deteniendo");
            }
            if ( lpS.dwCurrentState ==  SERVICE_STOPPED) {
                printf("Parado");
            }

            printf("\n");

            ++lpCE;
        }

        LocalFree(lpES);
    }

    CloseServiceHandle(sc);
    return 0;
}


Salida:



Aclaracion: La salida la muestro asi porque en la consola sale muy desprolijo pero esa seria la salida.

Saludos

Ya me parecía raro que tuviese GUI, buen trabajo ;).

en lugar de printf("       ");
utiliza
printf("\t");
y va a quedar mucho mas prolijo.