|
bool __fastcall TFrmMain::SetNAC( IWbemServices * pIWbemServices )
{
IWbemClassObject * pClass = NULL;
IWbemClassObject * pOutInst = NULL;
IWbemClassObject * pInClass = NULL;
IWbemClassObject * pInInst = NULL;
BSTR path = SysAllocString(L"sample");
BSTR ClassPath = SysAllocString(L"Win32_NetWorkAdapterConfiguration");
BSTR InstancePath = SysAllocString(L"Win32_NetWorkAdapterConfiguration.Index='1'");
BSTR MethodName = SysAllocString(L"EnableStatic");
BSTR ArgName1 = SysAllocString(L"IPAddress");
BSTR ArgName2 = SysAllocString(L"SubNetMask");
// Initialize COM and connect up to CIMOM
/////////////////////////////////////////////////////////////////
/* HRESULT hres;
hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
check_init(hres);
if (FAILED(hres))
{
LogMsg("Failed to initialize COM library. Error code = 0x");
return false; // Program has failed.
}*/
/* HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (hr)
{
LogMsg("Failed to initialize COM library. Error code = 0x");
// return false; // Program has failed.
}*/
IEnumWbemClassObject* pEnum = GetEnumNAC();
if (pEnum == NULL) {
LogMsg( "Can't get enumerate instances!" );
return false;
}
IWbemClassObject *pInst = NULL;
ULONG uReturned;
VARIANT vVal;
VariantInit(&vVal);
while (pEnum->Next(INFINITE, 1, &pInst, &uReturned) == WBEM_NO_ERROR) {
// Get the Instance Name string
if ((pInst->Get(L"IPEnabled", 0L, &vVal, NULL, NULL)) == WBEM_NO_ERROR) {
if (vVal.boolVal == false) {
continue;
}
VariantClear(&vVal);
}
}
pEnum->Release();
BSTR Class;
HRESULT hr;
// Alloc class name string
Class = SysAllocString(L"Win32_NetworkAdapterConfiguration");
if (!Class) {
// Out of memory
return false;
}
hr = pIWbemServicesCIMV2->GetObject(Class, 0L, NULL, &pInst, NULL);
SysFreeString(Class);
Class = NULL;
if ( hr != WBEM_NO_ERROR ) {
return false;
}
BSTR MethName = SysAllocString(L"EnableStatic");
if (!MethName)
{
// Out of memory
return false;
}
/////////////////////////////////////////////////////////////////
LogMsg("Connected to WMI");
// Set the proxy so that impersonation of the client occurs.
hr = CoSetProxyBlanket(pIWbemServicesCIMV2,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
check(hr);
// Get the class object
hr = pIWbemServicesCIMV2->GetObject(ClassPath, 0, NULL, &pClass, NULL);
if(hr != WBEM_S_NO_ERROR)
{
LogMsg("Error");
return false;
}
// Get the input argument and set the property
hr = pClass->GetMethod(MethodName, 0, &pInClass, NULL);
check(hr);
if(hr != WBEM_S_NO_ERROR)
{
return false;
}
hr = pInClass->SpawnInstance(0, &pInInst);
if(hr != WBEM_S_NO_ERROR)
{
LogMsg("Error");
return false;
}
BSTR ip;
ip = SysAllocString(L"127.0.0.1");
BSTR mask;
mask = SysAllocString(L"255.255.255.0");
long index[]={0};
SAFEARRAY *ip_list;
ip_list = SafeArrayCreateVector(VT_BSTR,0,1);
SafeArrayPutElement(ip_list,index,ip);
SAFEARRAY *mask_list;
mask_list = SafeArrayCreateVector(VT_BSTR,0,1);
SafeArrayPutElement(mask_list,index,mask);
VARIANT arg1;
arg1.vt = VT_ARRAY|VT_BSTR;
arg1.parray = ip_list;
hr = pInInst->Put(ArgName1, 0, &arg1, 0);
if(hr != WBEM_S_NO_ERROR){
LogMsg("(X) Put IP failed ");
return false;
}
else
{
LogMsg("(O) Put IP succeeded");
}
VARIANT arg2;
arg2.vt = VT_ARRAY|VT_BSTR;;
arg2.parray = mask_list;
hr = pInInst->Put(ArgName2, 0, &arg2, 0);
if(hr != WBEM_S_NO_ERROR){
LogMsg("(X) Put Subnet failed ");
return false;
}
else
{
LogMsg("(O) Put Subnet succeeded");
}
// Call the method
// hr = pIWbemServicesCIMV2->ExecMethod(InstancePath, MethodName, 0, NULL, pInInst,
//&pOutInst, NULL);
hr = pIWbemServicesCIMV2->ExecMethod(InstancePath, MethodName, 0, NULL, pInInst,
&pOutInst, NULL);
check(hr);
if(hr != WBEM_S_NO_ERROR){
LogMsg("(X) ExecMethod failed ");
return false;
}
else
{
LogMsg("(O) ExecMethod succeeded");
}
// Get the EnableStatic method return value
/* VARIANT var[1];
V_VT(&var[0]) = VT_I4;
V_I4(&var[0]) = iEvt;
*/
VARIANT ret_value;
VariantInit(&ret_value);
BSTR strReturnValue = SysAllocString(L"ReturnValue");
hr = pOutInst->Get(strReturnValue, 0, &ret_value, 0, 0);
check_get(hr);
if(hr != WBEM_S_NO_ERROR){
LogMsg("(X) Get failed ");
return false;
}
else
{
LogMsg("(O) Get succeeded");
}
long ret = V_I4(&ret_value);
VariantClear(&ret_value);
if(ret!=0) {
LogMsg("(X) EnableStatic failed with return code ");
LogMsg(printf(" %ld",ret));
return false;
}else{
LogMsg("(O) EnableStatic succeeded");
}
// Free up resources
SysFreeString(strReturnValue);
VariantClear(&ret_value);
SysFreeString(ip);
SysFreeString(mask);
SafeArrayDestroy(ip_list);
SafeArrayDestroy(mask_list);
VariantClear(&arg1);
VariantClear(&arg2);
SysFreeString(path);
SysFreeString(ClassPath);
SysFreeString(InstancePath);
SysFreeString(MethodName);
SysFreeString(ArgName1);
SysFreeString(ArgName2);
pClass->Release();
pInInst->Release();
pInClass->Release();
pOutInst->Release();
pIWbemServicesCIMV2->Release();
CoUninitialize();
return true;
}
_________________________________________________________________________________
아이피와 서브넷마스크를 변경하는 프로그램입니다.
결과는
Connected to WMI
(O) The call succeeded.
(O) The call succeeded.
(O) Put IP succeeded
(O) Put Subnet succeeded
(O) The call succeeded.
(O) ExecMethod succeeded
(GET) The call succeeds.
(O) Get succeeded
(X) EnableStatic failed with return code
-1
(X) Set failed.
이렇게 나옵니다. get부터 잘 모르겠거든요. 부탁드려요
|