HOWTO: Get Information for a Given Display Driver

ID: Q200876


The information in this article applies to:


SUMMARY

This article describes a technique that allows an application to get information about a specified display device on a Windows 95 or Windows 98 system.


MORE INFORMATION

On Windows 98 systems, multiple display devices can coexist on a system. There is no API available that allows you to obtain information about the display devices directly from the system. This articles describes a technique for parsing the registry database to get this information.

The sample code uses the FindDevice() function to search for devices with the given Class name. After finding the device, the GetDeviceValue() function is used to get the details for the device.




// 
// Function    : GetDeviceValue
// 
// Purpose     : Read a value from the HW or SW of a PnP device.
// 
// Parameters  :
//   szHardwareKey    Hardware key for the device.
//   szKey            Subkey.
//   szValue          The value to be queried.
//   buf              Buffer to receive the value.
//   cbbuf            Size of buffer.
// 
// Returns     : TRUE if value is obtained.
// 

BOOL GetDeviceValue(LPCSTR szHardwareKey, LPSTR szKey, LPSTR Value,
BYTE *buf, DWORD cbbuf)
{
    HKEY    hkeyHW;
    HKEY    hkeySW;
    BOOL    bFlag = FALSE;
    DWORD   nSize;
    char    szSoftwareKey[MAX_PATH];
    *(DWORD*)buf = 0;
    // 
    // Open the HW key.
    // 
    if (RegOpenKey(HKEY_LOCAL_MACHINE, szHardwareKey, &hkeyHW) == 
   ERROR_SUCCESS)
    {
        // 
        // Try to read the value from the HW key.
        // 
        *buf = 0;
        nSize = cbbuf;
        if (RegQueryValueEx(hkeyHW, szValue, NULL, NULL, buf,
      &nSize) == ERROR_SUCCESS)
        {
            bFlag = TRUE;
        }
        else
        {
            // 
            // Now try the SW key.
            // 
            static char szSW[] =  
        "System\\CurrentControlSet\\Services\\Class\\";
            lstrcpy(szSoftwareKey, szSW);
            nSize = sizeof(szSoftwareKey) - sizeof(szSW);
            RegQueryValueEx(hkeyHW, "Driver", NULL, NULL, szSoftwareKey
            + sizeof(szSW) - 1, &nSize);
            if (szKey)
            {
                lstrcat(szSoftwareKey, "\\");
                lstrcat(szSoftwareKey, szKey);
            }
            if (RegOpenKey(HKEY_LOCAL_MACHINE, szSoftwareKey,   
       &hkeySW) == ERROR_SUCCESS)
            {
                *buf = 0;
                nSize = cbbuf;
                if (RegQueryValueEx(hkeySW, szValue, NULL, NULL, buf,  
        &nSize) == ERROR_SUCCESS)
             {
                    bFlag = TRUE;
                }
                RegCloseKey(hkeySW);
            }
        }
        RegCloseKey(hkeyHW);
    }
    return bFlag;
} 


// 
// Function   : FindDevice
// 
// Purpose    : Enumerates the started PnP devices looking for a  
//              device of a particular class.
// 
// Parameters :
//  iDeviceNumber   What device to return (0=first device, 1=second, etc.)
//  szDeviceClass   What class device (for example, "Display"). NULL will 
//                  match all.
//  szDeviceID     Buffer to return the hardware ID (MAX_PATH bytes).                      
// 
//  Return    : TRUE if a device was found.
// 
// Example:
// 
//      for (int i=0; FindDevice(i, "Display", DeviceID); i++)
//      {
//      }
// 

BOOL FindDevice(int iDeviceNumber, LPCSTR szDeviceClass, LPSTR 
      szHardwareKey)
{
    HKEY    hkeyPnP;
    HKEY    hkey;
    DWORD   nCounter;
    DWORD   nSize;
    DWORD   dwReturnValue;
    char    szBuffer[MAX_PATH];
    if (RegOpenKey(HKEY_DYN_DATA, "Config Manager\\Enum", &hkeyPnP)
       !=  ERROR_SUCCESS)
        return FALSE;
    for (nCounter=0; RegEnumKey(hkeyPnP, nCounter, szBuffer,
       sizeof(szBuffer)) == 0; nCounter++)
    {
        static char szHW[] = "Enum\\";
        if (RegOpenKey(hkeyPnP, szBuffer, &hkey) != ERROR_SUCCESS)
            continue;
       lstrcpy(szHardwareKey, szHW);
        nSize = MAX_PATH - sizeof(szHW);
        RegQueryValueEx(hkey, "HardwareKey", NULL, NULL,
       (BYTE*)szHardwareKey + sizeof(szHW) - 1, &nSize);
        dwReturnValue = 0;
        nSize = sizeof(dwReturnValue);
        RegQueryValueEx(hkey, "Problem", NULL, NULL, 
      (BYTE*)&dwReturnValue, &nSize);
        RegCloseKey(hkey);
        if (dwReturnValue != 0)        // If this device has a problem,
          // skip it.
            continue;
        if (szDeviceClass)
        {
            GetDeviceValue(szHardwareKey, NULL, "Class", szBuffer,
       sizeof(szBuffer));
            if (lstrcmpi(szDeviceClass, szBuffer) != 0)
                continue;
        }
        // 
        // We found a device, make sure it is the one the caller wants.
        // 
        if (iDeviceNumber-- == 0)
        {
            RegCloseKey(hkeyPnP);
            return TRUE;
        }
    }
    RegCloseKey(hkeyPnP);
    return FALSE;
}

void main(int argc, char **argv)
{
    int      iDeviceCounter;
    char    szKey[MAX_PATH];
    char    szValue[MAX_PATH];
    #define GETSZ(a, b) \ 
        GetDeviceValue(szKey, a, b, szValue, sizeof(szValue)); \ 
        printf("  %-20s:\t%s\n", b, szValue);
    for (iDeviceCounter=0; FindDevice(iDeviceCounter, "Display", szKey);
       iDeviceCounter++)
    {
        printf("**** Display %d\n", iDeviceCounter+1);
        GETSZ(NULL, "DeviceDesc");
        GETSZ(NULL, "DriverDesc");
        GETSZ(NULL, "DriverDate");
        GETSZ(NULL, "ProviderName");
        GETSZ(NULL, "Mfg");
        GETSZ("DEFAULT", "drv");
        GETSZ("DEFAULT", "minivdd");
    }
} 


REFERENCES

Windows 95 Device Driver Kit.

For another technique to determine the current display driver, please refer to the following article in the Microsoft Knowledge Base:

Q169590 HOWTO: Determine the Currently Installed Display Driver

Additional query words: kbDSupport kbdsh kbcode


Keywords          : kbdisplay kbDDK kbVideo kbWinOS95 kbWinOS98 
Version           : WINDOWS:Windows 95,Windows 98
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: March 7, 1999