Here is the C++ code that does the same thing as the VBScript code posted by Helen.
// Obtain the initial locator to WMI
// ...
// Connect to WMI through the IWbemLocator::ConnectServer method
// ...
// Set security levels on the proxy
// ...
wchar_t wmihddsn[256];
*wmihddsn=0;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_DiskPartition WHERE BootPartition=True"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if(SUCCEEDED(hres) && pEnumerator)
{
// get the first Win32_DiskPartition
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(SUCCEEDED(hr) && 0 != uReturn)
{
VARIANT vtProp;
wchar_t tmp[1024];
char query[1024];
// Get the value of the partition's DeviceID property
hr = pclsObj->Get(L"DeviceID", 0, &vtProp, 0, 0);
if(SUCCEEDED(hr))
{
if(vtProp.vt == VT_BSTR) {
// wcout << " SerialNumber : " << vtProp.bstrVal << endl;
wcscpy(tmp, vtProp.bstrVal);
}
VariantClear(&vtProp);
// "join" Win32_DiskPartition to Win32_DiskDrive
sprintf(query,
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='%s'} WHERE ResultClass=Win32_DiskDrive",
NarrowWcharString(tmp));
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t(query),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator1);
if(SUCCEEDED(hres) && pEnumerator1)
{
// get the first Win32_DiskDrive
hr = pEnumerator1->Next(WBEM_INFINITE, 1, &pclsObj1, &uReturn);
if(SUCCEEDED(hr) && 0 != uReturn)
{
// Get the value of the disk-drive's DeviceID
hr = pclsObj1->Get(L"DeviceID", 0, &vtProp, 0, 0);
if(SUCCEEDED(hr))
{
if(vtProp.vt == VT_BSTR)
{
wcscpy(tmp, vtProp.bstrVal);
}
VariantClear(&vtProp);
// "join" Win32_DiskDrive to Win32_PhysicalMedia
sprintf(query,
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='%s'} WHERE ResultClass=Win32_PhysicalMedia",
NarrowWcharString(tmp));
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t(query),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator2);
if(SUCCEEDED(hres) && pEnumerator2)
{
// get the first Win32_PhysicalMedia
hr = pEnumerator2->Next(WBEM_INFINITE, 1, &pclsObj2, &uReturn);
if(SUCCEEDED(hr) && 0 != uReturn)
{
// get the PhysicalMedia's SerialNumber
hr = pclsObj2->Get(L"SerialNumber", 0, &vtProp, 0, 0);
if(SUCCEEDED(hr))
{
if(vtProp.vt == VT_BSTR)
{
// wcout << " SerialNumber : " << vtProp.bstrVal << endl;
wcscpy(wmihddsn,vtProp.bstrVal);
}
VariantClear(&vtProp);
}
}
if(pclsObj2) pclsObj2->Release();
}
if(pEnumerator2) pEnumerator2->Release();
} // get disk-drive's DeviceID
}
if(pclsObj1) pclsObj1->Release();
}
if(pEnumerator1) pEnumerator1->Release();
} // get partition's DeviceID
}
if(pclsObj) pclsObj->Release();
} // if succeeded first query
if(pEnumerator) pEnumerator->Release();
// ...
// cleanup