Problem with Unjoin Domain method of win32_ComputerSystem

  • Thread starter Thread starter Manoj Nair
  • Start date Start date
M

Manoj Nair

Hi,

I am trying to create an application which connects to WMI on a windows XP
machine and disjoin it from the domain.The code i am working with was
created in Visual Studio 6 .
Having a problem with the way the parameters are passed .
This WMI class work perfectly in vb or .net .

Thanks in advance for any help.....................

Manoj
----------------------------------------------------


#define _WIN32_DCOM
#include <iostream.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <windows.h>
#include <stdio.h>
#include<conio.h>
#include <atlbase.h>
# pragma comment(lib, "wbemuuid.lib")


int main()
{

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 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 negotiates service
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))
{
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))
{
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 local root\cimv2 namespace
// and obtain pointer pSvc to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);

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

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


// Step 5: --------------------------------------------------
// Set security levels for the proxy ------------------------

hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
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.
}

// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----

// set up to call the Win32_ComputerSystem::UnjoinDomainOrWorkgroup
method
BSTR MethodName = SysAllocString(L"UnjoinDomainOrWorkgroup");
BSTR ClassName = SysAllocString(L"Win32_ComputerSystem");

IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);

IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

// Create the values for the in parameters

hres = pClassInstance->Put(L"Password",0,NULL,0);

hres = pClassInstance->Put(L"UserName",0,NULL,0);
int nOptions =0;
VARIANT var;
V_VT(&var) = VT_INT;
V_INT(&var) =(short)nOptions;
hres = pClassInstance->Put(L"FUnjoinOptions",0,&var,0);





// Execute Method
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0L,
NULL, pClassInstance, NULL, NULL);

if (FAILED(hres))
{
cout << "Could not execute method. Error code = 0x"
<< hex << hres << endl;
// VariantClear(&varCommand);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pInParamsDefinition->Release();

pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}

// if you want to see what the method returned,
// use the following code. The return value will
// be in &varReturnValue
VARIANT varReturnValue;



// Clean up
//--------------------------
//VariantClear(&varCommand);
VariantClear(&varReturnValue);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pInParamsDefinition->Release();

pLoc->Release();
pSvc->Release();
CoUninitialize();
return 0;

//
}//check
 
I would probably do something more like this after step 6.

CComPtr<IEnumWbemClassObject> pEnum;

pSvc->ExecQuery(L"WQL",L"SELECT * FROM
Win32_ComputerSystem",0,NULL,&pEnum);

CComPtr<IWbemClassObject> pObject;

ULONG uReturned = 0;

BSTR MethodName = SysAllocString(L"UnjoinDomainOrWorkgroup");
BSTR ClassName = SysAllocString(L"Win32_ComputerSystem");

while ( pEnum->Next ( WBEM_INFINITE, 1, &pObject, &uReturned) !=
WBEM_S_NO_MORE_DATA )
{

IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);

IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

// Create the values for the in parameters

_variant_t v;
v.ChangeType(VT_NULL);

hres = pClassInstance->Put(L"Password",0,&v,CIM_STRING);

hres = pClassInstance->Put(L"UserName",0,&v,CIM_STRING);

/*fUjoinoptions defaults to 0 so don't add the param*/

IWbemClassObject* pOutParams = NULL;

CIMTYPE pType;
LONG pFlavor;

pObject->Get(L"__PATH",0,&var,&pType,&pFlavor);

hres = pSvc->ExecMethod(var.bstrVal, MethodName, 0L,
NULL, pClassInstance, NULL, NULL);

}

Ryan Ackerman,

SofTulz.Net, Giving administrators more time with their families through
automated computer management.
http://www.SofTulz.Net
 
Back
Top