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.
Then, add the source file and give it a suitable name.
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(LThere are %d drivers:\n, cDrivers);
wprintf(LPress 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(LEnumDeviceDrivers() failed; array size needed is %d\n, cbNeeded / sizeof(LPVOID));
return 0;
}
Build and run the project. The following screenshot is a sample output.