how to handle segmentation errors in WMI code


Joined
Dec 17, 2007
Messages
3
Reaction score
0
Hi All,

I have written a WMI Program which changes the network settings like ip address, subnet mask, gateway and dns addresses for given adapter number.

when I call that function for adapter number-1, its executing perfectly. when I immediately make the call to that function with adapter number-2, its giving me the segmentation error and says unhandled signal handler.

So its like if I make to calls to that function, everytime second call is failing. wats the reason?
Can any one explain it to me?

I am sending the code here for convinience. Please any one try to help me.
Code is as follows.

JNIEXPORT jint JNICALL Java_SysXNativeCall_ipChange
(JNIEnv *env, jobject obj, jstring new_ip, jstring new_sm , jstring new_gw, jstring new_dns, jstring new_dns1, jint index_no)
{

IWbemLocator *pLocator = NULL;
IWbemServices *pNamespace = 0;

UINT adapter_no;

IWbemClassObject * pClass = NULL;
IWbemClassObject * pOutInst = NULL;
IWbemClassObject * pInClass = NULL;
IWbemClassObject * pInInst = NULL;

//IWbemClassObject * pClass_gw = NULL;
IWbemClassObject * pOutInst_gw = NULL;
IWbemClassObject * pInClass_gw = NULL;
IWbemClassObject * pInInst_gw = NULL;

//IWbemClassObject * pClass_dns = NULL;
IWbemClassObject * pOutInst_dns = NULL;
IWbemClassObject * pInClass_dns = NULL;
IWbemClassObject * pInInst_dns = NULL;

BSTR path = SysAllocString(L"root\\cimv2");
BSTR ClassPath = SysAllocString(L"Win32_NetWorkAdapterConfiguration");
//BSTR InstancePath = SysAllocString(L"Win32_NetWorkAdapterConfiguration.Index='7'");


//strcat(InstancePath, adapter_no);
//strcat(InstancePath, "'");

BSTR MethodName = SysAllocString(L"EnableStatic");
BSTR MethodName1 = SysAllocString(L"SetGateways");
BSTR MethodName2 = SysAllocString(L"SetDNSServerSearchOrder");


BSTR ArgName1 = SysAllocString(L"IPAddress");
BSTR ArgName2 = SysAllocString(L"SubNetMask");
BSTR ArgName3 = SysAllocString(L"DefaultIPGateway");
BSTR ArgName4 = SysAllocString(L"DNSServerSearchOrder");

int error=-1;
// Initialize COM and connect up to CIMOM
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0 );
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLocator);
//__try {
//hr = pLocator->ConnectServer(path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace);
//} __except (error) {
// error = EXCEPTION_CONTINUE_EXECUTION;

//}
hr = pLocator->ConnectServer(path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace);
if(hr==WBEM_E_OUT_OF_MEMORY) cout << "out of memory error" << endl;
if(hr != WBEM_S_NO_ERROR) return 2;
cout << "Connected to WMI" << endl;
// Set the proxy so that impersonation of the client occurs.
hr = CoSetProxyBlanket(pNamespace, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );

// Get the class object
hr = pNamespace->GetObject(ClassPath, 0, NULL, &pClass, NULL);

if(hr != WBEM_S_NO_ERROR)
return 3;

// I am adding extra code for querying to get the network adapter number

// For example, query for all the running processes
IEnumWbemClassObject* pEnumerator = NULL;
HRESULT hres = pNamespace->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled = TRUE "),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
cout << "Query for network adapter inf failed. "
<< "Error code = 0x"
<< hex << hres << endl;
//pSvc->Release();
//pLoc->Release();
//CoUninitialize();
return 4; // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
int kp = 1;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);

if(0 == uReturn)
{
break;
}

VARIANT vtProp;

if(kp == index_no)
{
// Get the value of the Name property
hres = pclsObj->Get(L"Index", 0, &vtProp, 0, 0);
wcout << "Adapter Number is: " << vtProp.uintVal << endl;

adapter_no = vtProp.uintVal;
cout << "Catching first adapter and its value is:" << adapter_no <<endl;
//kp++;
break;
}
kp++;

VariantClear(&vtProp);
}

}

char indexString[10];
itoa(adapter_no, indexString, 10);

char instanceString[100];
wchar_t w_instanceString[100];
strcpy(instanceString, "Win32_NetworkAdapterConfiguration.Index='");
strcat(instanceString, indexString);
strcat(instanceString, "'");
mbstowcs(w_instanceString, instanceString, 100);
BSTR InstancePath = SysAllocString(w_instanceString);


// querying to get the network adapter number code ends here

// Get the input argument and set the property - enable static method
hr = pClass->GetMethod(MethodName, 0, &pInClass, NULL);
if(hr != WBEM_S_NO_ERROR)
return 5;
hr = pInClass->SpawnInstance(0, &pInInst);
if(hr != WBEM_S_NO_ERROR)
return 6;

// Get the input argument and set the property - setdefault gateway method
hr = pClass->GetMethod(MethodName1, 0, &pInClass_gw, NULL);
if(hr != WBEM_S_NO_ERROR)
return 7;
hr = pInClass_gw->SpawnInstance(0, &pInInst_gw);
if(hr != WBEM_S_NO_ERROR)
return 8;

// Get the input argument and set the property - setdnsservers method
hr = pClass->GetMethod(MethodName2, 0, &pInClass_dns, NULL);
if(hr != WBEM_S_NO_ERROR)
return 9;
hr = pInClass_dns->SpawnInstance(0, &pInInst_dns);
if(hr != WBEM_S_NO_ERROR)
return 10;


BSTR ip;
//ip = SysAllocString(L"10.0.88.88");
//ip = env->GetStringChars(new_ip, NULL);
const jchar* r1 = env->GetStringChars(new_ip,NULL);
ip = SysAllocStringLen((const OLECHAR *)r1,(env->GetStringLength(new_ip)));

BSTR mask;
//mask = SysAllocString(L"255.255.0.0");
//mask =(BSTR) env->GetStringChars(new_sm, NULL);
const jchar* r2 = env->GetStringChars(new_sm,NULL);
mask = SysAllocStringLen((const OLECHAR *)r2,(env->GetStringLength(new_sm)));

BSTR gw;
//gw = SysAllocString(L"10.0.45.45");
//gw = (BSTR) env->GetStringChars(new_gw, NULL);
const jchar* r3 = env->GetStringChars(new_gw,NULL);
gw = SysAllocStringLen((const OLECHAR *)r3,(env->GetStringLength(new_gw)));

BSTR dns;
//dns = SysAllocString(L"10.45.55.55");
//dns = (BSTR) env->GetStringChars(new_dns, NULL);
const jchar* r4 = env->GetStringChars(new_dns,NULL);
dns = SysAllocStringLen((const OLECHAR *)r4,(env->GetStringLength(new_dns)));

BSTR dns1;
//dns1 = SysAllocString(L"10.45.66.66");
//dns1 = (BSTR) env->GetStringChars(new_dns1, NULL);
const jchar* r5 = env->GetStringChars(new_dns1,NULL);
//wcout << "contents of dns1" << new_dns1 << endl;
//if(new_dns1!=NULL)
dns1 = SysAllocStringLen((const OLECHAR *)r5,(env->GetStringLength(new_dns1)));
cout << "length of dns1 is" << env->GetStringLength(new_dns1) << endl;

cout << "created ip, GW, DNS and mask" << endl;

long index[]={0};
long index1[]={1};

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);

SAFEARRAY *gw_list;
gw_list = SafeArrayCreateVector(VT_BSTR,0,1);
SafeArrayPutElement(gw_list,index,gw);

SAFEARRAY *dns_list;
if(env->GetStringLength(new_dns1)!=0)
dns_list = SafeArrayCreateVector(VT_BSTR,0,2);
else
dns_list = SafeArrayCreateVector(VT_BSTR,0,1);
SafeArrayPutElement(dns_list,index,dns);
if(env->GetStringLength(new_dns1)!=0)
SafeArrayPutElement(dns_list,index1,dns1);

VARIANT arg1;
arg1.vt = VT_ARRAY|VT_BSTR;
arg1.parray = ip_list;
hr = pInInst->Put(ArgName1, 0, &arg1, 0);

VARIANT arg2;
arg2.vt = VT_ARRAY|VT_BSTR;;
arg2.parray = mask_list;
hr = pInInst->Put(ArgName2, 0, &arg2, 0);

VARIANT arg3;
arg3.vt = VT_ARRAY|VT_BSTR;;
arg3.parray = gw_list;
hr = pInInst_gw->Put(ArgName3, 0, &arg3, 0);

VARIANT arg4;
arg4.vt = VT_ARRAY|VT_BSTR;;
arg4.parray =dns_list;
hr = pInInst_dns->Put(ArgName4, 0, &arg4, 0);


// Call the method - enable static
hr = pNamespace->ExecMethod(InstancePath, MethodName, 0, NULL, pInInst, &pOutInst, NULL);
if(hr != WBEM_S_NO_ERROR)
{
cout << "ExecMethod failed " << hr;
return 11;
}
cout << "coming till here also" << endl;
// Get the EnableStatic method return value
VARIANT ret_value;
BSTR strReturnValue = SysAllocString(L"ReturnValue");
hr = pOutInst->Get(strReturnValue, 0, &ret_value, 0, 0);
long ret = V_I4(&ret_value);
if(ret != 0)
{
cout << "EnableStatic return code = " << ret;
}
else
{ cout << "EnableStatic succeeded" << endl;
}

//calling setGateways Method
hr = pNamespace->ExecMethod(InstancePath, MethodName1, 0, NULL, pInInst_gw, &pOutInst_gw, NULL);
if(hr != WBEM_S_NO_ERROR)
{
cout << "ExecMethod failed " << hr;
return 12;
}
cout << "coming till here also -2" << endl;
// Get the setGateways method return value
VARIANT ret_value1;
BSTR strReturnValue1 = SysAllocString(L"ReturnValue");
hr = pOutInst_gw->Get(strReturnValue1, 0, &ret_value1, 0, 0);
long ret1 = V_I4(&ret_value1);
if(ret1 != 0)
{
cout << "SetGateways method return code = " << ret1;
}
else
{ cout << "SetGateways succeeded" << endl;
}

//calling setDNS server order Method
hr = pNamespace->ExecMethod(InstancePath, MethodName2, 0, NULL, pInInst_dns, &pOutInst_dns, NULL);
if(hr != WBEM_S_NO_ERROR)
{
cout << "ExecMethod failed " << hr;
return 13;
}
cout << "coming till here also -3" << endl;
// Get the EnableStatic method return value
VARIANT ret_value2;
BSTR strReturnValue2 = SysAllocString(L"ReturnValue");
hr = pOutInst_dns->Get(strReturnValue2, 0, &ret_value2, 0, 0);
long ret2 = V_I4(&ret_value2);
if(ret2 != 0)
{
//cout << "Set DNS Server order return code = " << ret2;
}
else
{
//cout << "Set DNS Server Order Succeeded" << endl;
}



// Free up resources
SysFreeString(strReturnValue);
VariantClear(&ret_value);
SysFreeString(strReturnValue1);
VariantClear(&ret_value1);
SysFreeString(strReturnValue2);
VariantClear(&ret_value2);
SysFreeString(ip);
SysFreeString(mask);
SysFreeString(gw);
SysFreeString(dns);
SafeArrayDestroy(ip_list);
SafeArrayDestroy(mask_list);
SafeArrayDestroy(gw_list);
SafeArrayDestroy(dns_list);

VariantClear(&arg1);
VariantClear(&arg2);
VariantClear(&arg3);
VariantClear(&arg4);

SysFreeString(path);
SysFreeString(ClassPath);
SysFreeString(InstancePath);
SysFreeString(MethodName);
SysFreeString(ArgName1);
SysFreeString(ArgName2);
SysFreeString(MethodName1);
SysFreeString(ArgName3);
SysFreeString(MethodName2);
SysFreeString(ArgName4);

pClass->Release();
pInInst->Release();
pInClass->Release();
pOutInst->Release();

//pClass_gw->Release();
pInInst_gw->Release();
pInClass_gw->Release();
pOutInst_gw->Release();

pInInst_dns->Release();
pInClass_dns->Release();
pOutInst_dns->Release();

pLocator->Release();
pNamespace->Release();
CoUninitialize();
return 0;

}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top