Causing 0x80070005 at release pServ

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi
I try to get info from remote host via WMI, local and remote host are
Windows 2000. Everything seemed fine and I can read the data from remote
objects. But only at the last: call pSvc->Release(), it causing 0x80070005
every time.
Here is my code:

HRESULT WMIInterfaceInit(CString strLocalUser, CString strLocalPassword,
CString strLocalWorkgroug)
{
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 hres; // 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 ------------------------

// Auth Identity structure
SEC_WINNT_AUTH_IDENTITY_W authidentity;
SecureZeroMemory( &authidentity, sizeof(authidentity) );

authidentity.User = (unsigned short*)strLocalUser.LockBuffer();
authidentity.UserLength = wcslen( (WCHAR*)(authidentity.User) );
authidentity.Domain = (unsigned short*)strLocalWorkgroug.LockBuffer();
authidentity.DomainLength = wcslen( (WCHAR*)(authidentity.Domain) );
authidentity.Password = (unsigned short*)strLocalPassword.LockBuffer();
authidentity.PasswordLength = wcslen(
(WCHAR*)(authidentity.Password) );
authidentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;

SOLE_AUTHENTICATION_INFO authninfo[2];
SecureZeroMemory( authninfo, sizeof(SOLE_AUTHENTICATION_INFO)*2 );

// NTLM Settings
authninfo[0].dwAuthnSvc = RPC_C_AUTHN_WINNT;
authninfo[0].dwAuthzSvc = RPC_C_AUTHZ_NONE;
authninfo[0].pAuthInfo = &authidentity;

// Kerberos Settings
authninfo[1].dwAuthnSvc = RPC_C_AUTHN_GSS_KERBEROS ;
authninfo[1].dwAuthzSvc = RPC_C_AUTHZ_NONE;
authninfo[1].pAuthInfo = &authidentity;

SOLE_AUTHENTICATION_LIST authentlist;

authentlist.cAuthInfo = 2;
authentlist.aAuthInfo = authninfo;

hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
&authentlist,
EOAC_NONE,
NULL
);

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

return hres;
}

IEnumWbemClassObject *WNIExecQuery(CString strLocalUser, CString
strLocalPassword, CString strLocalWorkgroug,
CString strRemoteHost, CString strRemoteUser, CString
strRemotePassword, CString strRemoteWorkgroup,
CString strQuery)
{

// Step 3: -----------------------------------------------------
IWbemLocator *pLoc = NULL;

HRESULT hres;
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 NULL; // 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

CString strHostSpace = CString(_T("\\\\")) + strRemoteHost +
CString(_T("\\root\\cimv2"));

hres = pLoc->ConnectServer(
_bstr_t(strHostSpace.LockBuffer()),
_bstr_t(strRemoteUser.LockBuffer()), // User name
_bstr_t(strRemotePassword.LockBuffer()), // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // IWbemServices proxy
);

// When you have finished using the credentials,
// erase them from memory.
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return NULL; // Program has failed.
}
cout << "Connected to" << strRemoteHost << " ROOT\\CIMV2 WMI namespace"
<< endl;

// Step 5: --------------------------------------------------
// Set security levels on a WMI connection ------------------

SEC_WINNT_AUTH_IDENTITY_W* pAuthIdentity =
new SEC_WINNT_AUTH_IDENTITY_W;
ZeroMemory(pAuthIdentity, sizeof(SEC_WINNT_AUTH_IDENTITY_W));

pAuthIdentity->User = (unsigned short*)strRemoteUser.LockBuffer();
pAuthIdentity->UserLength = wcslen((WCHAR*)pAuthIdentity->User);

pAuthIdentity->Domain = (unsigned
short*)strRemoteWorkgroup.LockBuffer(); //(unsigned char*)L"administrator";
pAuthIdentity->DomainLength = wcslen((WCHAR*)pAuthIdentity->Domain);
//wcslen(pAuthIdentity->Domain);

pAuthIdentity->Password = (unsigned
short*)strRemotePassword.LockBuffer();
pAuthIdentity->PasswordLength =
wcslen((WCHAR*)pAuthIdentity->Password);

pAuthIdentity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;

hres = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
pAuthIdentity,
EOAC_NONE
);
if (FAILED(hres))
{
cout << "Count not set proxy blanket. Error code = 0x" << hex <<
hres << endl;
pSvc->Release();
pLoc->Release();
delete pAuthIdentity;
CoUninitialize();
return NULL;
}

// 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(strQuery),
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 NULL; // Program has failed.
}

hres = CoSetProxyBlanket(pEnumerator,
RPC_C_AUTHN_DEFAULT, //Authentication service
RPC_C_AUTHZ_DEFAULT, //Authorization service
COLE_DEFAULT_PRINCIPAL, //Server principal name used
// by authentication service
RPC_C_AUTHN_LEVEL_DEFAULT, //Authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, //Impersonation level
pAuthIdentity, //Client identity
EOAC_DEFAULT //Capability flags
);

if (FAILED(hres))
{
cout << "Call CoSetProxyBlanket failed." << " Error code = 0x" <<
hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return NULL; // Program has failed.
}

pLoc->Release();
pSvc->Release(); //???? CAUSING 0x80070005

return pEnumerator;
}


.



Relevant Pages

  • Re: Causing 0x80070005 at release pServ
    ... I try to get info from remote host via WMI, ... HRESULT WMIInterfaceInit(CString strLocalUser, CString strLocalPassword, ... CString strLocalWorkgroug) ... HRESULT hres; ...
    (microsoft.public.win32.programmer.wmi)
  • Re: Causing 0x80070005 at release pServ
    ... This could be because the IEnumWbemClassObject object (pEnumerator) is ... HRESULT WMIInterfaceInit(CString strLocalUser, CString strLocalPassword, ... CString strLocalWorkgroug) ... HRESULT hres; ...
    (microsoft.public.win32.programmer.wmi)
  • Failing to create a C# COM object from VC++
    ... public string WriteTextToConsole ... CString Write; ... HResult hRes = CoInitialize; ... BSTR * pBstrResult = new BSTR; ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Failing to create a C# COM object from VC++
    ... public string WriteTextToConsole ... CString Write; ... HResult hRes = CoInitialize; ... BSTR * pBstrResult = new BSTR; ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Access Denied with Remote WMI Connection in C++
    ... using that machine's Administrator account username/password. ... Administrator username/password for that remote machine, ... HRESULT hres; ... // Connect to the namespace ...
    (microsoft.public.win32.programmer.wmi)