我正在尝试通过USB获取外部连接到我的系统的物理驱动器的序列号。现在,当我尝试获取序列号时,它会提供通过SATA电缆连接的物理驱动器的序列号。它以加密形式提供序列号。但是当我尝试使用外置硬盘时。它没有给出正确的序列号。
我是通过win32_DiskDrive
winapi来做的。
我正在使用此代码。
void GetDrives()
{
HRESULT hres; // The return result of COM
ULONG uReturn = 0;
int nIndex = 0;
CString szVariantString;
CString szSerialNumber;
VARIANT vtProp;
hres = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
if (FAILED(hres))
{
return; // Program has failed.
}
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
CoUninitialize();
return; // Program has failed.
}
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("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))
{
pLoc->Release();
CoUninitialize();
return ; // Program has failed.
}
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))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return; // Program has failed.
}
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);
IWbemClassObject *pclsObj = nullptr;
CString szDisk;
if (SUCCEEDED(hres))
{
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(!uReturn)
break;
// Variant is always needed to clear as it was initialized with new values
VariantInit(&vtProp);
memset(&vtProp,0,sizeof(VARIANT));
////////////////////////////////////////////////////////////////////////////////////////////////////
VariantClear(&vtProp);
hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
if(&vtProp != NULL)
{
szVariantString = vtProp.bstrVal;
szVariantString.ReleaseBuffer();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
VariantClear(&vtProp);
nIndex++;
}
}
if(pSvc)
pSvc->Release();
if(pLoc)
pLoc->Release();
if(pEnumerator)
pEnumerator->Release();
if(pclsObj)
pclsObj->Release();
CoUninitialize();
}
我接受了msdn的帮助:
http://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx
任何人都可以帮助我。