List all Device Drivers running in your system.

3 04 2008

Icon Description
Listing up device driver information is very easy. In Platform SDK , there is a set of functions available named “Process Status Helpers”. By using them, we can get the BaseName and FileName of all running device drivers.

Icon How Can I Do It?
Basically there are 3 functions, which we are interested in. They are –

  • EnumDeviceDrivers() – Used to get the list of ImageBaseAddress of all Device Drivers. Using this image base address, we find out the the BaseName and FileName. This function is also used to get the buffer length required for calling the function.
  • GetDeviceDriverBaseName() – If ImageBaseAddress of a Device Driver is passed, it will provide the Driver Base Name.
  • GetDeviceDriverFileName() – If ImageBaseAddress of a Device Driver is passed, it will provide the Driver FileName.

See the code block for iterating all device drivers in a system.

#include "psapi.h" 

// Gets the required buffer length for ImageBaseAddrArray.
DWORD GetImageBaseAddrArrayLen()
{
    // Gets the number of Bytes needed for Buffer.
    // Call EnumDeviceDrivers with size as 0.
    // The required buffer length will be returned
    // via dwRequiredBufferSize.
    DWORD dwRequiredBufferSize = 0;
    EnumDeviceDrivers( 0,  // array of load addresses
                       0,  // size of array as 0.
                       // number of Required buffer size.
                       &dwRequiredBufferSize );  

    return dwRequiredBufferSize;
} 

// Get Driver informations such as Driver BaseName and FileName.
void GetDriverInfo( LPVOID DriverImageBaseAddr, CString& csDriverBaseName, CString& csDriverFileName )
{
    // Get driver BaseName
    TCHAR tchBuffer[ MAX_PATH ] = { 0 };
    GetDeviceDriverBaseName( DriverImageBaseAddr, tchBuffer, MAX_PATH );
    csDriverBaseName = tchBuffer; 

    // Get driver filename.
    GetDeviceDriverFileName( DriverImageBaseAddr, tchBuffer, MAX_PATH );
    csDriverFileName = tchBuffer; 

} 

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    // Allocate array for getting ImageBase addresses of
    // all drivers present in system.
    DWORD dwBufferLen = GetImageBaseAddrArrayLen();
    LPVOID* pImageBaseAddrArray = (LPVOID*)new BYTE[ dwBufferLen ]; 

    DWORD dwBytesReturned = 0;
    // Get the ImageBaseAddresses of all drivers.
    EnumDeviceDrivers( pImageBaseAddrArray,  // array of load addresses
                       dwBufferLen,  // size of array as 0.
                       &dwBytesReturned );  

    // Get the total number of drivers. calculate it by
    // total size in bytes/sizeof(void*)
    DWORD nDrivers = dwBufferLen / sizeof(LPVOID); 

    // Iterate through each driver.
    for( DWORD Index = 0; Index < nDrivers; ++Index )
    {
        // Get Device Driver informations.
        CString csDriverBaseName;
        CString csDriverFileName;
        GetDriverInfo( pImageBaseAddrArray[ Index], csDriverBaseName, csDriverFileName ); 

        // Print the Driver Information or do what ever you want!
        cout << csDriverBaseName.operator LPCTSTR() <<
            _T(" : ") <<
            csDriverFileName.operator LPCTSTR()
            << endl;
    }
}

Create a console appliaction with MFC support and try the source.

Icon Note
Don’t forget to add psapi.lib to your linker settings.

intermediateseries.jpg
Targeted Audience – Intermediate.

Advertisements