Click here to Skip to main content
15,923,273 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: VC++ 6 stability compiling STL Pin
toxcct22-Jun-05 0:10
toxcct22-Jun-05 0:10 
GeneralRe: VC++ 6 stability compiling STL Pin
hairy_hats22-Jun-05 0:19
hairy_hats22-Jun-05 0:19 
GeneralWindows Service description Pin
Kri521-Jun-05 23:54
Kri521-Jun-05 23:54 
GeneralRe: Windows Service description Pin
Blake Miller22-Jun-05 4:59
Blake Miller22-Jun-05 4:59 
GeneralRe: Windows Service description Pin
Kri522-Jun-05 6:17
Kri522-Jun-05 6:17 
GeneralRe: Windows Service description Pin
skornel22-Jun-05 5:02
skornel22-Jun-05 5:02 
GeneralRe: Windows Service description Pin
Kri522-Jun-05 6:17
Kri522-Jun-05 6:17 
Generalcall a remote WMI server Pin
JohnnyChiu21-Jun-05 23:46
JohnnyChiu21-Jun-05 23:46 
Hi all:
i am having a problem connecting to query wmi object

HRESULT hres;

// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------

hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
std::cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << std::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))
{
std::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))
{
std::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 remote root\cimv2 namespace
// and obtain pointer pSvc to make IWbemServices calls.
//---------------------------------------------------------
// change the computerName and domain
// strings below to the full computer name and domain
// of the remote computer

hres = pLoc->ConnectServer(
_bstr_t(L"\\\\192.168.1.243\\ROOT\\cimv2"),
_bstr_t(L"username"), // User name
_bstr_t(L"password"), // User password
_bstr_t(L"MS_409"), // Locale
NULL, // Security flags
NULL, //_bstr_t(L"ntlmdomain:domain"), // Authority
0, // Context object
&pSvc // IWbemServices proxy
);

// When you have finished using the credentials,
// erase them from memory.
//SecureZeroMemory(pszName, sizeof(pszName));
//SecureZeroMemory(pszPwd, sizeof(pszPwd));

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

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

if (FAILED(hres))
{
std::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"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
std::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);

// Get the value of the Name property
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << " OS Name : " << vtProp.bstrVal << endl;

// Get the value of the FreePhysicalMemory property
hr = pclsObj->Get(L"FreePhysicalMemory",
0, &vtProp, 0, 0);
wcout << " Free physical memory (in kilobytes): "
<< vtProp.uintVal << endl;
VariantClear(&vtProp);
}

// Cleanup
// ========

pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();

return 0; // Program successfully completed.

running this program, it shows the followings

Connected to ROOT\CIMV2 WMI namespace
Query for operating system name failed. Error code = 0x80070005

anyone helps?
GeneralCan't access the flexgrid control in dialog Pin
nbobby21-Jun-05 23:06
nbobby21-Jun-05 23:06 
GeneralWhich Message to handle for Only ALT key Pin
Vikash Dubey21-Jun-05 22:32
Vikash Dubey21-Jun-05 22:32 
GeneralRe: Which Message to handle for Only ALT key Pin
Nilesh K.21-Jun-05 23:10
Nilesh K.21-Jun-05 23:10 
GeneralRe: Which Message to handle for Only ALT key Pin
Vikash Dubey22-Jun-05 0:36
Vikash Dubey22-Jun-05 0:36 
GeneralRe: Which Message to handle for Only ALT key Pin
David Crow22-Jun-05 3:29
David Crow22-Jun-05 3:29 
GeneralRe: Which Message to handle for Only ALT key Pin
Vikash Dubey22-Jun-05 18:47
Vikash Dubey22-Jun-05 18:47 
GeneralRe: Which Message to handle for Only ALT key Pin
Nilesh K.22-Jun-05 19:04
Nilesh K.22-Jun-05 19:04 
GeneralRe: Which Message to handle for Only ALT key Pin
David Crow23-Jun-05 3:10
David Crow23-Jun-05 3:10 
GeneralWindoes Services Pin
Laji5921-Jun-05 22:12
Laji5921-Jun-05 22:12 
GeneralRe: Windoes Services Pin
ThatsAlok21-Jun-05 22:17
ThatsAlok21-Jun-05 22:17 
GeneralAPI Imap Pin
Member 34468121-Jun-05 22:09
Member 34468121-Jun-05 22:09 
GeneralLots of check boxes... Pin
mcsherry21-Jun-05 22:02
mcsherry21-Jun-05 22:02 
GeneralRe: Lots of check boxes... Pin
Cedric Moonen21-Jun-05 22:24
Cedric Moonen21-Jun-05 22:24 
GeneralRe: Lots of check boxes... Pin
mcsherry21-Jun-05 22:32
mcsherry21-Jun-05 22:32 
GeneralRe: Lots of check boxes... Pin
liquid_22-Jun-05 7:12
liquid_22-Jun-05 7:12 
GeneralRe: Lots of check boxes... Pin
Jose Lamas Rios22-Jun-05 16:17
Jose Lamas Rios22-Jun-05 16:17 
GeneralCOM-port mistery Pin
tomek1821-Jun-05 21:44
tomek1821-Jun-05 21:44 

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.