hippietrail
hippietrail

Reputation: 17014

How can I query the manufacturer of a drive in the Windows API?

I'm interested in how to programatically detect the manufacturer of a hard drive, SSD, etc connected to a Windows PC. If it makes any difference I'll probably use C++ on Windows 10.

Perhaps there's multiple levels, perhaps registry, Windows API, SATA, USB?

I need a method that will work for external drives connected by USB. I think I'm looking for a Windows API that will query the hardware.

Googling I can only see to find ways to see this info from the console or some app, or ways to query other information about the drives but not the manufacturer.

Upvotes: 1

Views: 1100

Answers (1)

Rita Han
Rita Han

Reputation: 9710

You will find hardware disk manufacture name, for example, at the following Registry key:

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\Scsi\Scsi Port 0\Scsi Bus 0\Target Id 0\Logical Unit Id 0

There is a value name "Identifier" seems what you are looking for:

enter image description here

The following is an example for query this value using Registry Functions:

#include <windows.h>
#include <tchar.h>

#define MAX_VALUE_NAME 16383

void QueryKey(HKEY hKey)
{
    DWORD    cValues;   // number of values for key 
    DWORD    retCode;
    TCHAR    pvData[MAX_VALUE_NAME];
    DWORD    cbData = sizeof(TCHAR) * MAX_VALUE_NAME;
    TCHAR    targetValue[] = L"Identifier";

    // Get the value count. 
    retCode = RegQueryInfoKey(
        hKey,           // key handle 
        NULL,           
        NULL,           
        NULL,                    
        NULL,               
        NULL,            
        NULL,            
        &cValues,       // number of values for this key 
        NULL,            
        NULL,         
        NULL,   
        NULL);       

    // Get the key value. 
    if (cValues)
    {
        retCode = RegGetValue(hKey, NULL, targetValue, RRF_RT_REG_SZ, NULL, pvData, &cbData);
        if (retCode != ERROR_SUCCESS)
        {
            _tprintf(TEXT("RegGetValue fails with error: %d\n", retCode));
            return;
        }
        _tprintf(TEXT("%s: %s\n"), targetValue, pvData);
    }
}

void main(void)
{
    HKEY hTestKey;

    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        TEXT("HARDWARE\\DEVICEMAP\\\Scsi\\Scsi Port 0\\Scsi Bus 0\\Target Id 0\\Logical Unit Id 0"),
        0,
        KEY_READ,
        &hTestKey) == ERROR_SUCCESS
        )
    {
        QueryKey(hTestKey);
    }

    RegCloseKey(hTestKey);
}

Upvotes: 1

Related Questions