获取C ++中的Windows硬件序列号 [英] Get Windows Hardware Serial Numbers in c++

查看:112
本文介绍了获取C ++中的Windows硬件序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试获取Windows机器上已安装硬件(如硬盘和主板)的序列号.而且我在VisualStuido2008 IDE中使用c ++.

现在,我仍然只找到使用System.Management的解决方案,而在c ++或WMI中则无法使用.我不经常使用COM,因此我会遇到很多问题和疑问.

当我看到C ++的WMI解决方案时,它们都具有我不需要的异步消息和处理程序,并且使代码难以理解.在MSDN论坛中,这些信息是含糊不清和令人困惑的.

首先,还有其他方法可以简单地在c ++中检索此信息吗?因为我只需要在应用程序开始时获取此信息(主板唯一ID和/或硬盘唯一ID).

好吧,但是我正在尝试使用COM WMI,并且我已经达到了这一点:


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

推荐答案

确定,我找到了解决方法

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

如果(FAILED(hres))
{
std :: cout<< 查询操作系统名称失败."
<< 错误代码= 0x"
<< std :: hex<< hres<< std :: endl;
pSvc-> Release();
pLoc-> Release();
CoUninitialize();
返回1; //程序失败.
}



IWbemClassObject * pclsObj;
ULONG uReturn = 0;

while(pEnumerator)
{
HRESULT hr = pEnumerator-> Next(WBEM_INFINITE,1,
&pclsObj,&uReturn);

if(0 == uReturn)
{
休息;
}

VARIANT vtProp;

std :: cout<< "----------------------------------"<< std :: endl;
std :: cout<< 获取操作系统信息"<< std :: endl;
std :: cout<< "----------------------------------"<< std :: endl;
//获取Name属性的值
hr = pclsObj-> Get(L"Name",0,&vtProp,0,0);
std :: wcout<< "OS名称:"<< vtProp.bstrVal<< std :: endl;
VariantClear(&vtProp);

hr = pclsObj-> Get(L"Version",0,&vtProp,0,0);
std :: wcout<< 操作系统版本:"<< vtProp.bstrVal<< std :: endl;
VariantClear(&vtProp);

hr = pclsObj-> Get(L"WindowsDirectory",0,&vtProp,0,0);
std :: wcout<< "OS目录:"<< 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);

如果(FAILED(hres))
{
std :: cout<< 查询操作系统名称失败."
<< 错误代码= 0x"
<< std :: hex<< hres<< std :: endl;
pSvc-> Release();
pLoc-> Release();
CoUninitialize();
返回1; //程序失败.
}


uReturn = 0;

while(pEnumerator)
{
HRESULT hr = pEnumerator-> Next(WBEM_INFINITE,1,
&pclsObj,&uReturn);

if(0 == uReturn)
{
休息;
}

VARIANT vtProp;

std :: cout<< "----------------------------------"<< std :: endl;
std :: cout<< 检索磁盘信息"<< std :: endl;
std :: cout<< "----------------------------------"<< std :: endl;
//获取Name属性的值
hr = pclsObj-> Get(L"Name",0,&vtProp,0,0);
std :: wcout<< 磁盘名:"<< vtProp.bstrVal<< std :: endl;
VariantClear(&vtProp);

hr = pclsObj-> Get(L"Model",0,&vtProp,0,0);
std :: wcout<< 磁盘型号:"<< vtProp.bstrVal<< std :: endl;
VariantClear(&vtProp);

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

hr = pclsObj-> Get(L"DeviceID",0,&vtProp,0,0);
std :: wcout<< 设备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);
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);


这篇关于获取C ++中的Windows硬件序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆