Click here to Skip to main content
14,266,297 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone,

I'm trying to get the serial numbers of the installed hardware(like hard disk and motherboard) on a Windows Machine. And I'm using c++ with VisualStuido2008 IDE.

Still now I've only found solutions that uses the System.Management which I could not use in c++, or with WMI. I don't use COM frequently and this way I face a lot of problems and questions.

When I see WMI solutions for C++ they all have the asynchronous messages and handlers that i dont need and make the code difficult to understand. And in the MSDN forums the informations is vague and confusing.

First of all, is there any other way to simple retrieve this information in c++? Because I just need to get this info(motherboard unique ID and/or Hard Disk unique ID) at the beginning of my application.

Well, however I am trying to use COM WMI and I have reached this point:


int
main(int argc, char* argv[])
{

	IWbemLocator * pIWbemLocator = NULL;
	IWbemServices * pWbemServices = NULL;
	IEnumWbemClassObject * pEnumObject  = NULL;

	HRESULT hr;
	hr = CoInitializeEx(0, COINIT_MULTITHREADED); 
	if (FAILED(hr)) 
	{ 
		std::cout << "Failed to initialize COM library. Error code = 0x" << std::hex << hr << std::endl; 
	}




	hr = CoCreateInstance(CLSID_WbemLocator, 0, 
		CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pIWbemLocator);

	if (FAILED(hr))
	{
		std::cout << "Failed to create IWbemLocator object. Err code = 0x"
			<< std::hex << hr << std::endl;
		// Program has failed.
	}



	// Connect to the root\default namespace with the current user.
	hr = pIWbemLocator->ConnectServer(
		BSTR(L"ROOT\\DEFAULT"), 
		NULL, NULL, 0, NULL, 0, 0, &pWbemServices);

	if (FAILED(hr))
	{
		std::cout << "Could not connect. Error code = 0x" 
			<< std::hex << hr << std::endl;

	}

	std::cout << "Connected to WMI" << std::endl;


	HRESULT hres;


	// Set the proxy so that impersonation of the client occurs.
	hres = CoSetProxyBlanket(pWbemServices,
		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))
	{
		std::cout << "Could not set proxy blanket. Error code = 0x" 
			<< std::hex << hres << std::endl;

	}


/*********************************
here I need to now how to get the specific info I need from the conected interfaces

**********************************/

	pWbemServices->Release();
	pIWbemLocator->Release();
	pEnumObject->Release();
	//	pClassObject->Release();

	CoUninitialize();

	return 0;
}
Posted
Updated 15-Oct-10 0:35am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

OK I found the solution

IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_OperatingSystem"),
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.
}



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 OS 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 << " OS Name : " << vtProp.bstrVal << std::endl;
VariantClear(&vtProp);

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

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


pclsObj->Release();
}

pEnumerator->Release();

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.
}


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);
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100