bool init_raw_smbios_data()
{
//zero raw dat memory
memset(global_buffer.smbios_table_data,0,MAX_DATA);
bool ret = false;
HRESULT h_result;
// Initialize
COM.
h_result = CoInitializeEx(0, COINIT_MULTITHREADED);
if (h_result<0)
{
return false; // Program has failed.
}
// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator* p_locator = 0;
h_result = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &p_locator);
if (h_result<0)
{
CoUninitialize();
return false; // Program has failed.
}
IWbemServices* p_service = 0;
// Connect to the root\cimv2 namespace with the
// current user and obtain pointer pSvc
// to make IWbemServices calls.
h_result = p_locator->ConnectServer(
BSTR("ROOT\\
WMI"), //
WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&p_service // IWbemServices proxy
);
if (h_result<0)
{
p_locator->Release();
CoUninitialize();
return false; // Program has failed.
}
// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
h_result = CoSetProxyBlanket(
p_service, // the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (h_result<0)
{
p_service->Release();
p_locator->Release();
CoUninitialize();
return false; // Program has failed.
}
IEnumWbemClassObject* p_enumerator = NULL;
h_result = p_service->CreateInstanceEnum(BSTR("MSSMBios_RawSMBiosTables"), 0, NULL, &p_enumerator);
if (h_result<0)
{
p_service->Release();
p_locator->Release();
CoUninitialize();
return false; // Program has failed.
}
else
{
do
{
IWbemClassObject* p_instance = NULL;
ULONG dw_count = NULL;
h_result = p_enumerator->Next(
WBEM_INFINITE,
1,
&p_instance,
&dw_count);
if(h_result>=0)
{
VARIANT variant_bios_data;
VariantInit(&variant_bios_data);
CIMTYPE type;
h_result = p_instance->Get(BSTR("SmbiosMajorVersion"),0,&variant_bios_data,&type,NULL);
if(h_result <0)
{
VariantClear(&variant_bios_data);
}
else
{
global_buffer.smbios_major_version = (unsigned char)variant_bios_data.iVal;
VariantInit(&variant_bios_data);
h_result = p_instance->Get(BSTR("SmbiosMinorVersion"),0,&variant_bios_data,&type,NULL);
if(h_result<0)
{
VariantClear(&variant_bios_data);
}
else
{
global_buffer.smbios_minor_version = (unsigned char)variant_bios_data.iVal;
VariantInit(&variant_bios_data);
h_result = p_instance->Get(BSTR("SMBiosData"),0,&variant_bios_data,&type,NULL);
if(h_result>=0)
{
if ( ( VT_UI1 | VT_ARRAY ) != variant_bios_data.vt )
{
}
else
{
SAFEARRAY* p_array = NULL;
p_array = V_ARRAY(&variant_bios_data);
unsigned char* p_data = (unsigned char *)p_array->pvData;
global_buffer.length = p_array->rgsabound[0].cElements;
if (global_buffer.length >= MAX_DATA)
{
// p(_T("Return global_buffer would overflow. Abort initialization of raw SMBIOS data.\n"));
p_service->Release();
p_locator->Release();
CoUninitialize();
return false;
}
memcpy(global_buffer.smbios_table_data,p_data,global_buffer.length);
}
}
VariantClear(&variant_bios_data);
}
}
break;
}
} while (h_result == WBEM_S_NO_ERROR);
}
p_service->Release();
p_locator->Release();
CoUninitialize();
return true;
}