Click here to Skip to main content
15,881,600 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: urgent help needed with name collision in libraries Pin
m.dietz10-Sep-06 23:14
m.dietz10-Sep-06 23:14 
QuestionPassword protected files and folders Pin
amaneet5-Sep-06 2:46
amaneet5-Sep-06 2:46 
AnswerRe: Password protected files and folders Pin
Chris Losinger5-Sep-06 3:13
professionalChris Losinger5-Sep-06 3:13 
QuestionRe: Password protected files and folders Pin
David Crow5-Sep-06 4:04
David Crow5-Sep-06 4:04 
QuestionHow to use the WMI Class to get hardware/system information? Pin
HansonDavid5-Sep-06 2:32
HansonDavid5-Sep-06 2:32 
AnswerRe: How to use the WMI Class to get hardware/system information? Pin
Hamid_RT5-Sep-06 2:41
Hamid_RT5-Sep-06 2:41 
GeneralRe: How to use the WMI Class to get hardware/system information? Pin
HansonDavid5-Sep-06 16:47
HansonDavid5-Sep-06 16:47 
GeneralRe: How to use the WMI Class to get hardware/system information? Pin
HansonDavid5-Sep-06 21:46
HansonDavid5-Sep-06 21:46 
Dear Whitesky,
I have a try as the WMI C++ Application Example. I want to get the physical memory information. So I replace the Win32_OperatingSystem with the Win32_PhysicalMemory. I can get the string type data from the VARIANT structure just as example code, according to the Win32_PhysicalMemory class definition. But I don't know how to get the uint16 or uint32 type data from the VARIANT structure. Can you tell me?

HRESULT hres;

// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);


if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}

// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;

hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);

if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}

// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;

// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);

if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}

cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);

if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}

// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----

// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
// bstr_t("SELECT * FROM Win32_OperatingSystem"),
bstr_t("SELECT * FROM Win32_PhysicalMemory"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
cout << "Query for operating system name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}

// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj;
ULONG uReturn = 0;

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

if(0 == uReturn)
{
break;
}

VARIANT vtProp;
VariantInit(&vtProp);
// I can get the string type data
cout<<"BankLabel:";
hr = pclsObj->Get(L"BankLabel", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR))
{
wcout<<vtprop.bstrval<<endl;
variantclear(&vtprop);
="" }

="" but="" i="" don't="" know="" how="" to="" get="" the="" uint16="" or="" uint32="" type="" data="" just="" as="" width.="" can="" you="" help="" me?
="" cout<<"datawidth:";
="" hr="pclsObj-">Get(L"DataWidth", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR))
{
TCHAR chTemp[80] = {0};
itoa(abs(vtProp.iVal), chTemp, 10);
wcout<<chtemp<<endl;
variantclear(&vtprop);
="" }
="" }

="" cleanup
="" =="======
" psvc-="">Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();

return 0; // Program successfully completed.
GeneralRe: How to use the WMI Class to get hardware/system information? Pin
Hamid_RT6-Sep-06 2:36
Hamid_RT6-Sep-06 2:36 
GeneralRe: How to use the WMI Class to get hardware/system information? Pin
Hamid_RT7-Sep-06 8:14
Hamid_RT7-Sep-06 8:14 
AnswerRe: How to use the WMI Class to get hardware/system information? Pin
Viorel.5-Sep-06 3:34
Viorel.5-Sep-06 3:34 
GeneralRe: How to use the WMI Class to get hardware/system information? Pin
HansonDavid5-Sep-06 21:32
HansonDavid5-Sep-06 21:32 
QuestionIncrease the window message priority Pin
helpcode5-Sep-06 2:30
helpcode5-Sep-06 2:30 
AnswerRe: Increase the window message priority Pin
Frank K5-Sep-06 2:42
Frank K5-Sep-06 2:42 
GeneralRe: Increase the window message priority Pin
helpcode6-Sep-06 18:40
helpcode6-Sep-06 18:40 
AnswerRe: Increase the window message priority Pin
ThatsAlok5-Sep-06 2:47
ThatsAlok5-Sep-06 2:47 
AnswerRe: Increase the window message priority Pin
Michael Dunn5-Sep-06 12:31
sitebuilderMichael Dunn5-Sep-06 12:31 
QuestionSerial communication + thread related problem Pin
harsha_12345-Sep-06 2:13
harsha_12345-Sep-06 2:13 
AnswerRe: Serial communication + thread related problem Pin
ThatsAlok5-Sep-06 2:50
ThatsAlok5-Sep-06 2:50 
AnswerRe: Serial communication + thread related problem Pin
Blake Miller6-Sep-06 11:49
Blake Miller6-Sep-06 11:49 
QuestionReading error Pin
Anu_Bala5-Sep-06 2:08
Anu_Bala5-Sep-06 2:08 
AnswerRe: Reading error Pin
_AnsHUMAN_ 5-Sep-06 2:12
_AnsHUMAN_ 5-Sep-06 2:12 
GeneralRe: Reading error Pin
Anu_Bala5-Sep-06 2:24
Anu_Bala5-Sep-06 2:24 
GeneralRe: Reading error Pin
_AnsHUMAN_ 5-Sep-06 2:29
_AnsHUMAN_ 5-Sep-06 2:29 
GeneralRe: Reading error Pin
Anu_Bala5-Sep-06 2:38
Anu_Bala5-Sep-06 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.