Windows Process Status Helpers API 4

 

 

 

 

 

Enumerating All Device Drivers in the System Program Example

 

The following sample code uses the EnumDeviceDrivers() function to enumerate the current device drivers in the system. It passes the load addresses retrieved from this function call to the GetDeviceDriverBaseName() function to retrieve a name that can be displayed.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Enumerating All Device Drivers in the System Program Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Then, add the source file and give it a suitable name.

 

Enumerating All Device Drivers in the System Program Example: Adding new C++ source file for C++ source code to the existing C++ project

 

Next, add the following source code.

 

#include <windows.h>

#include <psapi.h>

#include <wchar.h>

#include <stdio.h>

 

// Link to Psapi.lib

#pragma comment(lib, "Psapi.lib")

 

#define ARRAY_SIZE 1024

 

int wmain(int argc, WCHAR **argv)

{

   LPVOID drivers[ARRAY_SIZE];

   DWORD cbNeeded;

   int cDrivers, i;

   WCHAR szDriver[ARRAY_SIZE];

 

   if(EnumDeviceDrivers(drivers, sizeof(drivers), &cbNeeded) && cbNeeded < sizeof(drivers))

   {

      cDrivers = cbNeeded / sizeof(drivers[0]);

 

      wprintf(L"There are %d drivers:\n", cDrivers);

      wprintf(L"Press any key for more...\n");

 

      for (i=0; i < cDrivers; i++)

      {

         if(GetDeviceDriverBaseName(drivers[i], szDriver, sizeof(szDriver) / sizeof(szDriver[0])))

            wprintf(L"%d: %s\n", i+1, szDriver);

         _getwch();

      }

   }

   else

         wprintf(L"EnumDeviceDrivers() failed; array size needed is %d\n", cbNeeded / sizeof(LPVOID));

 

   return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

Enumerating All Device Drivers in the System Program Example: A sample console program output

 

 

 

 

< Win32 Process Status Help APIs 3 | Process Status Help APIs Index | Win32 Programming | Win32 Process Status Help APIs 5 >