Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Everyone,

I created a C++ code to get the Hard Disk Serial Number using WMI, however instead of getting the serial Number I get a string with:

\\.\PHYSICALDRIVE0
but I was expecting something like:
BCB1-45BB

Who can explain me this result and how to obtain the real serial number?

Here is the code I used:
#define _WIN32_DCOM
#include <iostream>

#include <comdef.h>
#include <wbemidl.h>



int main(int argc, char **argv)
{
    HRESULT hres;

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

    hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres))
    {
        std::cout << "Failed to initialize COM library. Error code = 0x" 
            << std::hex << hres << std::endl;
        return 1;                  // Program has failed.
    }

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you need to specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------

    hres =  CoInitializeSecurity(
        NULL, 
        -1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );

                      
    if (FAILED(hres))
    {
        std::cout << "Failed to initialize security. Error code = 0x" 
            << std::hex << hres << std::endl;
        CoUninitialize();
        return 1;                    // Program has failed.
    }
    
    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------

    IWbemLocator *pLoc = NULL;

    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);
 
    if (FAILED(hres))
    {
        std::cout << "Failed to create IWbemLocator object."
            << " Err code = 0x"
            << std::hex << hres << std::endl;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;
	
    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object 
         &pSvc                    // pointer to IWbemServices proxy
         );
    
    if (FAILED(hres))
    {
        std::cout << "Could not connect. Error code = 0x" 
             << std::hex << hres << std::endl;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }

    std::cout << "Connected to ROOT\\CIMV2 WMI namespace" << std::endl;


    // Step 5: --------------------------------------------------
    // Set security levels on the proxy -------------------------

    hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

    if (FAILED(hres))
    {
        std::cout << "Could not set proxy blanket. Error code = 0x" 
            << std::hex << hres << std::endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // For example, get the name of the operating system
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_DiskDrive"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);
    
    if (FAILED(hres))
    {
        std::cout << "Query for operating system name failed."
            << " Error code = 0x" 
            << std::hex << hres << std::endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------
 
    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;
   
    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
            &pclsObj, &uReturn);

        if(0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;

        
		std::cout << "----------------------------------" << std::endl;
		std::cout << "Retrieve DISK Info" << std::endl;
		std::cout << "----------------------------------" << std::endl;
       // Get the value of the Name property
        hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
        std::wcout << " Disk Name : " << vtProp.bstrVal << std::endl;
        VariantClear(&vtProp);

		hr = pclsObj->Get(L"Model", 0, &vtProp, 0, 0);
        std::wcout << " Disk Model : " << vtProp.bstrVal << std::endl;
        VariantClear(&vtProp);

		 hr = pclsObj->Get(L"Status", 0, &vtProp, 0, 0);
        std::wcout << " Status : " << vtProp.bstrVal << std::endl;
        VariantClear(&vtProp);

		hr = pclsObj->Get(L"DeviceID", 0, &vtProp, 0, 0);
        std::wcout << " Device ID : " << vtProp.bstrVal << std::endl;
        VariantClear(&vtProp);

		hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
        std::wcout << " SerialNumber : " << vtProp.bstrVal << std::endl;
        VariantClear(&vtProp);

			hr = pclsObj->Get(L"InterfaceType", 0, &vtProp, 0, 0);
        std::wcout << " InterfaceType : " << vtProp.bstrVal << std::endl;
        VariantClear(&vtProp);
		
		std::cout << "----------------------------------" << std::endl;
        pclsObj->Release();
    }

    
    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();

    return 0;   // Program successfully completed.
	
}</wbemidl.h></comdef.h></iostream>



This is the Output:
Connected to ROOT\CIMV2 WMI namespace
----------------------------------
Retrieve DISK Info
----------------------------------
 Disk Name : \\.\PHYSICALDRIVE0 
 Disk Model : IC35L080AVVA07-0
 Status : OK
 Device ID : \\.\PHYSICALDRIVE0 
 SerialNumber : \\.\PHYSICALDRIVE0 
 InterfaceType : IDE
----------------------------------


Thanks,
Apóstolo
Posted
Updated 15-Oct-10 9:22am
v2

1 solution

this is my code:

IWbemClassObject *pclsObj;
ULONG uReturn = 0;

while( pEnumerator )
{
HRESULT hr = pEnumerator->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn );
if (!uReturn )
{
break;
}

VARIANT vtProp;
VariantInit( &vtProp );

vDiskDriveInfoList.push_back(HardDiskDriveInfo());

HardDiskDriveInfo& rhddi = *vDiskDriveInfoList.rbegin();

pclsObj->BeginEnumeration( WBEM_FLAG_NONSYSTEM_ONLY );
{
CIMTYPE pvtType;
VARIANT pVal;
BSTR pstrName;

while(pclsObj->Next(0, &pstrName, &pVal, &pvtType, NULL) == WBEM_S_NO_ERROR)
{
CString strName = (LPCTSTR)(_bstr_t)pstrName;

if (strName.Compare(_T("SerialNumber")) == 0 && pVal.vt == VT_BSTR)
{
rhddi.strSerialNumber = (LPCTSTR)(_bstr_t)pVal.bstrVal;
rhddi.strSerialNumber.Trim();
}
else if (strName.Compare(_T("FirmwareRevision")) == 0 && pVal.vt == VT_BSTR)
{
rhddi.strFirmwareRevision = (LPCTSTR)(_bstr_t)pVal.bstrVal;
rhddi.strFirmwareRevision.Trim();
}
else if (strName.Compare(_T("MediaType")) == 0 && pVal.vt == VT_BSTR)
{
rhddi.strMediaType = (LPCTSTR)(_bstr_t)pVal.bstrVal;
rhddi.strMediaType.Trim();
}
else if (strName.Compare(_T("InterfaceType")) == 0 && pVal.vt == VT_BSTR)
{
rhddi.strInterfaceType = (LPCTSTR)(_bstr_t)pVal.bstrVal;
rhddi.strInterfaceType.Trim();
}

SysFreeString(pstrName);
VariantClear( &pVal );
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900