Connecting to WMI
- From: Shawn Pecze <ShawnPecze@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 15 Jan 2007 14:20:01 -0800
I'm having a problem connecting to WMI.
I have a function that sets up the connection and returns the pointer to the
IWbemServices object. The function takes 3 arguments: namespace, username
and password.
However, I can't get the connection to be made. Here is the function:
IWbemServices* connect(char* cimnamespace, char* username, char* password) {
IWbemLocator *pIWbemLocator = NULL;
IWbemServices *pSvc = NULL;
HRESULT hres;
LOG_MSG(0, "connect", "Entry");
if( username == NULL ) {
LOG_MSG(0, "connect", "Connecting to %s", cimnamespace);
} else {
LOG_MSG(0, "connect", "Connecting to %s with user %s", cimnamespace,
username);
}
LOG_MSG(0, "connect", "Initalize");
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if( FAILED(hres) ) {
LOG_MSG(0, "connect", "Could not initialize");
return NULL;
}
// Initialize Security
hres = CoInitializeSecurity( NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_STATIC_CLOAKING, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres)) {
LOG_MSG(0, "connect", "Failed to initialize security. Error code =
0x%lx", hres);
CoUninitialize();
return NULL; // Program has failed.
}
// Create an instance of the WbemLocator interface.
LOG_MSG(0, "connect", "Create instance of WbemLocator");
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pIWbemLocator);
if( FAILED(hres) ) {
LOG_MSG(0, "connect", "Could not create the WBEM Locator");
LOG_MSG(0, "connect", "Failed with error code 0x%lx", hres);
CoUninitialize();
LOG_MSG(0, "connect", "Exit");
return NULL;
}
// Using the locator, connect to CIMOM in the given namespace.
BSTR pAccount = NULL;
BSTR pPsw = NULL;
if (strncmp(cimnamespace, "\\\\", 2) == 0 ) {
LOG_MSG (0, "connect", "Starts with \\\\");
if (cimnamespace[2] != '.') {
LOG_MSG (0, "connect", "Connecting to a remote system");
LOG_MSG (0, "connect", "Setting up username and password");
pAccount = BSTR(username);
pPsw = BSTR(password);
} else {
LOG_MSG (0, "connect", "Connecting to the local system");
}
} else {
LOG_MSG (0, "connect", "Connecting to the local system");
}
BSTR pNamespace = BSTR(cimnamespace);
BSTR pLocale = BSTR(L"MS_409");
hres = pIWbemLocator->ConnectServer(pNamespace,
pAccount, //using current account
pPsw, //using current password
pLocale, // locale
0L, // securityFlags
NULL, // authority (NTLM domain)
NULL, // context
&pSvc);
if( FAILED( hres ) ) {
LOG_MSG(0, "connect", "Could not connect to namespace %s", cimnamespace);
LOG_MSG(0, "connect", "Failed with error code 0x%lx", hres);
pIWbemLocator->Release();
CoUninitialize();
LOG_MSG(0, "connect", "Exit");
return NULL;
}
LOG_MSG(0, "connect", "Connected to namespace %s", cimnamespace);
// Clean up
LOG_MSG(0, "connect", "Unitializing");
pIWbemLocator->Release();
LOG_MSG(0, "connect", "Exit");
return pSvc;
}
I have a simple main routine that calls this method using command line args.
If I run it and just pass in root\cimv2, the call to ConnectServer fails
with error code 0x8004100e.
If I run and pass in \\hostname\root\cimv2 user password, it fails with
error 0x80041064.
Now, if I hardcode root\cimv2 into the connectserver method, it connects,
but if I hardcode the remote information, it fails with an exception.
I'm not sure what I'm doing wrong. Any ideas?
.
- Prev by Date: WMI Query Performance
- Next by Date: Re: WMI Query Performance
- Previous by thread: WMI Query Performance
- Next by thread: Re: How to develop WMI using C or C++
- Index(es):
Relevant Pages
|
Loading