问题自己解决了。进来拿分吧。 (50分)

  • 主题发起人 主题发起人 wlmmlw
  • 开始时间 开始时间
W

wlmmlw

Unregistered / Unconfirmed
GUEST, unregistred user!
CLSID CLSID_WbemLocator;
IID IID_IWbemLocator;
CLSIDFromString(LPOLESTR("{4590f811-1d3a-11d0-891f-00aa004b2e24}"), &CLSID_WbemLocator);
IIDFromString(LPOLESTR("dc12a687-737f-11cf-884d-00aa004b2e24"), &IID_IWbemLocator);
以上代码对吗?
有人知道CLSID_WbemLocator在哪被初始化吗?
 
//HELP ME [:)][:D][8D][:(][:(!][^][?]
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <windows.h>
#include <comdef.h>
#include <wbemcli.h>
#include <wbemidl.h>
//Add by wlm
#include <objbase.h>
//#include <stdlib.h>
//#include <atlconv.h>

int main(int argc, char **argv)
{
//USES_CONVERSION;
HRESULT hres;
// 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.
}
// Initialize
hres = CoInitializeSecurity(
NULL,
-1, // COM negotiates authen
tication service
NULL, // Authen
tication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authen
tication (recommended)
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation (recommended)
NULL, // Authen
tication 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.
}
// Obtain the initial locator to Windows Management on a particular host computer.
IWbemLocator *pLoc = 0;
CLSID CLSID_WbemLocator;
IID IID_IWbemLocator;
CLSIDFromString(LPOLESTR("{4590f811-1d3a-11d0-891f-00aa004b2e24}"), &amp;CLSID_WbemLocator);
IIDFromString(LPOLESTR("dc12a687-737f-11cf-884d-00aa004b2e24"), &amp;IID_IWbemLocator);
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &amp;pLoc);

if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1;
// Program has failed.
}
IWbemServices *pSvc = 0;
// 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 user's password
0, // Locale. NULL indicates current locale
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object (IWbemContext), if required
&amp;pSvc // Out: 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 WMI" << endl;
// Set the IWbemServices proxy so that impersonation of the user (client) occurs.
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx authen
tication service
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx authen
tication level
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx impersonation level
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.
}

// Use the IWbemServices pointer to make requests of WMI.
// Make requests here:
// For example, query for print queues that have more than 10 jobs
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * Win32_PerfFormattedData_Spooler_PrintQueue Where Name <> '_Total'"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&amp;pEnumerator);

if (FAILED(hres))
{
cout << "Query for print queues failed. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();

CoUninitialize();
return 1;
// Program has failed.
}
else
{
do
{
//Add by wlm
IWbemClassObject *pInstance=0;
ULONG dwCount;
//Add
pInstance = NULL; //Undefined
hres = pEnumerator->Next(
WBEM_INFINITE,
1,
&amp;pInstance,
&amp;dwCount);


} while (hres == WBEM_S_NO_ERROR);

//} //Delete by wlm
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();

CoUninitialize();
return 0;
// Program successfully completed.
}
 
搞定了.加上一个LIB文件就OK.[:D][:D][:D]
 
不要给我! 我是来捧场的:)
 
不要上楼上的网址
 
已经解决了.结贴
 
后退
顶部