How to get the SerialNumber of disk driver?
-
Hi all! Now I am developing an application to read the disk driver's SerialNumber. I want to read it by WMI. The code is as following:
BOOL CData::GetSerialNumber(BYTE* out)
{
BOOL bRet = FALSE;
HRESULT hr;
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hr << endl;
return bRet;
}
hr = CoInitializeSecurity(
NULL, // Security descriptor
-1, // COM negotiates authentication service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
NULL, // Authentication info
EOAC_NONE, // Additional capabilities of the client or server
NULL); // Reservedif (FAILED(hr)) { cout << "Failed to initialize security. Error code = 0x" << hex << hr << endl; CoUninitialize(); return bRet; // Program has failed. } IWbemLocator \*pLoc = 0; hr = CoCreateInstance(CLSID\_WbemLocator, 0, CLSCTX\_INPROC\_SERVER, IID\_IWbemLocator, (LPVOID \*) &pLoc); if (FAILED(hr)) { cout << "Failed to create IWbemLocator object. Err code = 0x" << hex << hr << endl; CoUninitialize(); return bRet; // Program has failed. } IWbemServices \*pSvc = 0; // Connect to the root\\default namespace with the current user. hr = pLoc->ConnectServer( BSTR(L"ROOT\\\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc); if (FAILED(hr)) { cout << "Could not connect. Error code = 0x" << hex << hr << endl; pLoc->Release(); CoUninitialize(); return bRet; // Program has failed. } cout << "Connected to WMI" << endl; HRESULT hres; // Set the proxy so that impersonation of the client occurs. hres = CoSetProxyBlanket(pSvc, RPC\_C\_AUTHN\_WINNT, RPC\_C\_AUTHZ\_NONE, NULL, RPC\_C\_AUTHN\_LEVEL\_CALL, RPC\_C\_IMP\_LEVEL\_IMPERSONATE, NULL, EOAC\_NONE ); if (FAILED(hres)) { cout << "Could not set proxy blanket. Error c
-
Hi all! Now I am developing an application to read the disk driver's SerialNumber. I want to read it by WMI. The code is as following:
BOOL CData::GetSerialNumber(BYTE* out)
{
BOOL bRet = FALSE;
HRESULT hr;
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hr << endl;
return bRet;
}
hr = CoInitializeSecurity(
NULL, // Security descriptor
-1, // COM negotiates authentication service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
NULL, // Authentication info
EOAC_NONE, // Additional capabilities of the client or server
NULL); // Reservedif (FAILED(hr)) { cout << "Failed to initialize security. Error code = 0x" << hex << hr << endl; CoUninitialize(); return bRet; // Program has failed. } IWbemLocator \*pLoc = 0; hr = CoCreateInstance(CLSID\_WbemLocator, 0, CLSCTX\_INPROC\_SERVER, IID\_IWbemLocator, (LPVOID \*) &pLoc); if (FAILED(hr)) { cout << "Failed to create IWbemLocator object. Err code = 0x" << hex << hr << endl; CoUninitialize(); return bRet; // Program has failed. } IWbemServices \*pSvc = 0; // Connect to the root\\default namespace with the current user. hr = pLoc->ConnectServer( BSTR(L"ROOT\\\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc); if (FAILED(hr)) { cout << "Could not connect. Error code = 0x" << hex << hr << endl; pLoc->Release(); CoUninitialize(); return bRet; // Program has failed. } cout << "Connected to WMI" << endl; HRESULT hres; // Set the proxy so that impersonation of the client occurs. hres = CoSetProxyBlanket(pSvc, RPC\_C\_AUTHN\_WINNT, RPC\_C\_AUTHZ\_NONE, NULL, RPC\_C\_AUTHN\_LEVEL\_CALL, RPC\_C\_IMP\_LEVEL\_IMPERSONATE, NULL, EOAC\_NONE ); if (FAILED(hres)) { cout << "Could not set proxy blanket. Error c
Other than
strID
not being null terminated, your code works fine for me."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius