Connecting to WMI



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?
.



Relevant Pages

  • Access violation using refresher (when blocking port in Firewall)
    ... The problem only happens when we configure the Firewall in the remote ... When a WMI connection using refreshers is opened there are 2 new ports ... The port numbers change in each restart of the client. ... HRESULT hres = 0; ...
    (microsoft.public.win32.programmer.wmi)
  • Re: Connection Pool problem
    ... First question of the day is, why don't you use sql server's native provider? ... I am using same connection string for all connections and Using Multi ... if(hres == S_OK) ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Connection Pool problem
    ... I am using same connection string for all connections and Using Multi ... and I written code in C++. ... if(hres == S_OK) ... return bRet; ...
    (microsoft.public.dotnet.framework.adonet)
  • Connection Pool Issue
    ... I have troubles with a threaded application on W2003 server. ... I am using same connection string for all connections and Using Multi ... threading. ... if(hres == S_OK) ...
    (microsoft.public.data.ado)
  • Connection Pool problem
    ... I have troubles with a threaded application on W2003 server. ... I am using same connection string for all connections and Using Multi ... threading. ... if(hres == S_OK) ...
    (microsoft.public.dotnet.framework.adonet)

Loading