Создание драйверов. Часть 3. Просмотр состояния драйверов

В данном примере мне хотелось бы показать как с помощью интерфейса SCM(Service Control Manager) можно просмотреть состояние загруженных в системе драйверов. Для этой цели использовалась функция EnumServicesStatus, входящая в состав Advapi32.dll.

#include <iostream.h>
#include <windows.h>
#include <conio.h>
#define SIZE_BUF 4096
 
int main()
{
        SC_HANDLE hSCM;
        ENUM_SERVICE_STATUS Status[SIZE_BUF];
        DWORD SizeBuf = sizeof(Status);
        DWORD Needed = 0;
        DWORD Return = 0;
        DWORD Handle = 0;
 
        hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
        if(hSCM != NULL)
        {
                if(EnumServicesStatus(hSCM,
SERVICE_DRIVER,SERVICE_STATE_ALL,
(LPENUM_SERVICE_STATUS)&Status,SizeBuf,
&Needed,&Return,&Handle))
                {
                        for (unsigned long i=0;i<Return;i++)
                        {
                                CharToOem(Status[i].lpDisplayName,Status[i].lpDisplayName);
                                cout<<Status[i].lpServiceName;
                                cout<<" ("<<Status[i].lpDisplayName<<") ";
                                //??? ????????
                                if(Status[i].ServiceStatus.dwServiceType  == SERVICE_FILE_SYSTEM_DRIVER)
{cout<<"file system driver";}
                                else
                                {if(Status[i].ServiceStatus.dwServiceType == SERVICE_KERNEL_DRIVER)
{cout<<"device driver";}
                                else
                                {if(Status[i].ServiceStatus.dwServiceType == SERVICE_WIN32_OWN_PROCESS)
{cout<<"runs in its own process";}
                                else
                                {if(Status[i].ServiceStatus.dwServiceType == SERVICE_WIN32_SHARE_PROCESS)
{cout<<"shares a process with other services";}
                                else
                                {if(Status[i].ServiceStatus.dwServiceType == SERVICE_INTERACTIVE_PROCESS)
{cout<<"can interact with the desktop";}
                                else{cout<<"Other service type";}}}}}
                                //?????? ????????
                                if(Status[i].ServiceStatus.dwCurrentState  == SERVICE_CONTINUE_PENDING)
{cout<<" Continue is pending";}
                                else
                                {if(Status[i].ServiceStatus.dwCurrentState == SERVICE_PAUSE_PENDING)
{cout<<" Pause is pending";}
                                else
                                {if(Status[i].ServiceStatus.dwCurrentState == SERVICE_PAUSED)
{cout<<"  Paused";}
                                else
                                {if(Status[i].ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{cout<<"  Running";}
                                else
                                {if(Status[i].ServiceStatus.dwCurrentState == SERVICE_START_PENDING)
{cout<<"  Starting";}
                                else
                                {if(Status[i].ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING)
{cout<<"  Stopping";}
                                else
                                {if(Status[i].ServiceStatus.dwCurrentState == SERVICE_STOPPED)
{cout<<"  Not running";}
                                else{cout<<"Other service state";}}}}}}}
                                cout<<endl;
                        }
                }else{cout<<"Error EnumServicesStatus"<<endl;}
        }else{cout<<"Error OpenSCManager"<<endl;}
 
        CloseServiceHandle(hSCM);
 
        
        cout<<"\nPress any key to continue"<<endl;
        while (!getch());
        return 0;
}

Создание драйверов Просмотр состояния драйверов.