Saltar al contenido
Underc0de /foro

Obtener Servicios [C]

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

Tema anterior - Siguiente tema

0 Miembros y 0 Visitantes están viendo este tema.

Avatar de Expermicid
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:

Obtener Servicios [C]

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

Saludos
WhatsApp Telegram

2 respuestas

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

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

¿Te quedó una duda sobre Códigos Fuentes?

Preguntala y te responde la comunidad (y el asistente Underc0de IA si nadie llega antes). Es gratis y tarda un minuto.

Ya tengo cuenta
Guardamos tu pregunta en este navegador: después de entrar la publicás con un clic.
Grupo de WhatsApp