-
January 31st, 2008, 04:54 AM
#1
From diskdrive to logicaldrive
hello everyone,
I'm using WMI to get information about the harddisks installed on my system.
To do that i have used WMI-Select statement and then i have queried it.
Code:
HRESULT hr;
int result;
// Connect with WMI service
CComPtr< IWbemLocator > locator;
hr = CoCreateInstance( CLSID_WbemAdministrativeLocator, NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, reinterpret_cast< void** >( &locator ) );
if ( FAILED( hr ) )
{
std::cerr << "Instantiation of IWbemLocator failed" << std::endl;
return -1;
}
// connect to local service with current credentials
CComPtr< IWbemServices > service;
hr = locator->ConnectServer( L"root\\cimv2", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &service );
if (FAILED(hr))
{
cout << "Could not connect. Error code = 0x"
<< hex << hr << endl;
return -2; // Program has failed.
}
HRESULT hres;
// Set the proxy so that impersonation of the client occurs.
hres = CoSetProxyBlanket(service,
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 code = 0x"
<< hex << hres << endl;
return -3; // Program has failed.
}
CComPtr<IEnumWbemClassObject> pEnumerator;
// QUERY
char consulta[400];
sprintf(consulta,"SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\" AND Model = \"Sony PSP USB Device\"");
bstr_t consultab(consulta);
hres = service->ExecQuery(
bstr_t("WQL"),
consultab,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
ULONG uReturn = 0;
int numero_de_psp=0;
while (pEnumerator)
{
CComPtr<IWbemClassObject> pclsObj;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
if (numero_de_psp>1) return 2;
if (numero_de_psp==1) return 1;
if (numero_de_psp<1) return 0;
break;
}
numero_de_psp++;
/// RETRIEVING DeviceID property of Win32_DiskDrive class
VARIANT deviceIDb;
std::string deviceID;
_bstr_t tipo_aux;
// Get the value of the DeviceID property
hr = pclsObj->Get(L"DeviceID", 0, &deviceIDb, 0, 0);
tipo_aux=deviceIDb.bstrVal;
deviceID=tipo_aux; /// Device's DeviceID, something like \\.\PHYSICALDRIVE1
Up to this point, the code works well and i get the DeviceID of the devices wich Select statement returns.
The problem comes when i try to use this value to retrieve the instances from the WIN32DiskPartition class, using the ASSOCIATORS OF WQL-statement. This statement doesn't fail, but it doesn't return any value.
Code:
// Second query
sprintf(consulta,"ASSOCIATORS OF {Win32_DiskDrive.DeviceID=\"%s\"}WHERE AssocClass=Win32_DiskDriveToDiskPartition",deviceID.c_str());
consultab=consulta;
// Enumerator for the partitions
CComPtr<IEnumWbemClassObject> pEnumeratorParticiones;
hres = service->ExecQuery(
bstr_t("WQL"),
consultab,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumeratorParticiones);
uReturn=0;
while (pEnumeratorParticiones)
{
CComPtr<IWbemClassObject> pclsParticion;
hr = pEnumeratorParticiones->Next(WBEM_INFINITE, 1,
&pclsParticion, &uReturn);
if(0 == uReturn)
{
/// 0 value is returned
break;
}
// Get the value of the Name property of the partition associated to the hard disk
hr = pclsParticion->Get(L"name", 0, &name, 0, 0);
tipo_aux=name.bstrVal;
nombre=tipo_aux;
}
}
It seems as if the DeviceID value couldn't be used to query.
Any suggestions?
Thanks in advance, and sorry for my spanglish :-)
-
January 31st, 2008, 06:32 AM
#2
Re: From diskdrive to logicaldrive
Hello again,
I have already solved the problem, it seems that the DeviceID you have to use it's not exactly the same that you have obteined from the SELECT-statement.
The DeviceID you have into the deviceID variable is something like "\\.\PHYSICALDRIVE0" and to make the query you need "\\\\.\\PHYSICALDRIVE0" , so i have added this line after retrieving the DeviceID
Code:
hr = pclsObj->Get(L"DeviceID", 0, &deviceIDb, 0, 0);
tipo_aux=deviceIDb.bstrVal;
deviceID=tipo_aux;
deviceID.replace(0,4,"\\\\\\\\.\\\\",0,13);
and now ASSOCIATORS OF-statement works.
and now i just have to get the drive-letter from Win32LogicalDisk through Win32_LogicalDiskToPartition.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|