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_SysXNative Call_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;
IWbemClassObjec t * pClass = NULL;
IWbemClassObjec t * pOutInst = NULL;
IWbemClassObjec t * pInClass = NULL;
IWbemClassObjec t * pInInst = NULL;
//IWbemClassObjec t * pClass_gw = NULL;
IWbemClassObjec t * pOutInst_gw = NULL;
IWbemClassObjec t * pInClass_gw = NULL;
IWbemClassObjec t * pInInst_gw = NULL;
//IWbemClassObjec t * pClass_dns = NULL;
IWbemClassObjec t * pOutInst_dns = NULL;
IWbemClassObjec t * pInClass_dns = NULL;
IWbemClassObjec t * pInInst_dns = NULL;
BSTR path = SysAllocString( L"root\\cimv2") ;
BSTR ClassPath = SysAllocString( L"Win32_NetWork AdapterConfigur ation");
//BSTR InstancePath = SysAllocString( L"Win32_NetWork AdapterConfigur ation.Index='7' ");
//strcat(Instance Path, adapter_no);
//strcat(Instance Path, "'");
BSTR MethodName = SysAllocString( L"EnableStatic" );
BSTR MethodName1 = SysAllocString( L"SetGateways") ;
BSTR MethodName2 = SysAllocString( L"SetDNSServerS earchOrder");
BSTR ArgName1 = SysAllocString( L"IPAddress" );
BSTR ArgName2 = SysAllocString( L"SubNetMask ");
BSTR ArgName3 = SysAllocString( L"DefaultIPGate way");
BSTR ArgName4 = SysAllocString( L"DNSServerSear chOrder");
int error=-1;
// Initialize COM and connect up to CIMOM
HRESULT hr = CoInitializeEx( 0, COINIT_MULTITHR EADED);
hr = CoInitializeSec urity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEV EL_CONNECT, RPC_C_IMP_LEVEL _IMPERSONATE, NULL, EOAC_NONE, 0 );
hr = CoCreateInstanc e(CLSID_WbemLoc ator, 0, CLSCTX_INPROC_S ERVER, IID_IWbemLocato r, (LPVOID *) &pLocator);
//__try {
//hr = pLocator->ConnectServer( path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace) ;
//} __except (error) {
// error = EXCEPTION_CONTI NUE_EXECUTION;
//}
hr = pLocator->ConnectServer( path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace) ;
if(hr==WBEM_E_O UT_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 = CoSetProxyBlank et(pNamespace, RPC_C_AUTHN_WIN NT, RPC_C_AUTHZ_NON E, NULL, RPC_C_AUTHN_LEV EL_CALL, RPC_C_IMP_LEVEL _IMPERSONATE, NULL, EOAC_NONE );
// Get the class object
hr = pNamespace->GetObject(Clas sPath, 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
IEnumWbemClassO bject* pEnumerator = NULL;
HRESULT hres = pNamespace->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_NetworkAd apterConfigurat ion where IPEnabled = TRUE "),
WBEM_FLAG_FORWA RD_ONLY | WBEM_FLAG_RETUR N_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
{
IWbemClassObjec t *pclsObj;
ULONG uReturn = 0;
int kp = 1;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFI NITE, 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(&v tProp);
}
}
char indexString[10];
itoa(adapter_no , indexString, 10);
char instanceString[100];
wchar_t w_instanceStrin g[100];
strcpy(instance String, "Win32_NetworkA dapterConfigura tion.Index='");
strcat(instance String, indexString);
strcat(instance String, "'");
mbstowcs(w_inst anceString, instanceString, 100);
BSTR InstancePath = SysAllocString( w_instanceStrin g);
// querying to get the network adapter number code ends here
// Get the input argument and set the property - enable static method
hr = pClass->GetMethod(Meth odName, 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(Meth odName1, 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(Meth odName2, 0, &pInClass_dn s, 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 = SysAllocStringL en((const OLECHAR *)r1,(env->GetStringLengt h(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 = SysAllocStringL en((const OLECHAR *)r2,(env->GetStringLengt h(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 = SysAllocStringL en((const OLECHAR *)r3,(env->GetStringLengt h(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 = SysAllocStringL en((const OLECHAR *)r4,(env->GetStringLengt h(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!=NU LL)
dns1 = SysAllocStringL en((const OLECHAR *)r5,(env->GetStringLengt h(new_dns1)));
cout << "length of dns1 is" << env->GetStringLengt h(new_dns1) << endl;
cout << "created ip, GW, DNS and mask" << endl;
long index[]={0};
long index1[]={1};
SAFEARRAY *ip_list;
ip_list = SafeArrayCreate Vector(VT_BSTR, 0,1);
SafeArrayPutEle ment(ip_list,in dex,ip);
SAFEARRAY *mask_list;
mask_list = SafeArrayCreate Vector(VT_BSTR, 0,1);
SafeArrayPutEle ment(mask_list, index,mask);
SAFEARRAY *gw_list;
gw_list = SafeArrayCreate Vector(VT_BSTR, 0,1);
SafeArrayPutEle ment(gw_list,in dex,gw);
SAFEARRAY *dns_list;
if(env->GetStringLengt h(new_dns1)!=0)
dns_list = SafeArrayCreate Vector(VT_BSTR, 0,2);
else
dns_list = SafeArrayCreate Vector(VT_BSTR, 0,1);
SafeArrayPutEle ment(dns_list,i ndex,dns);
if(env->GetStringLengt h(new_dns1)!=0)
SafeArrayPutEle ment(dns_list,i ndex1,dns1);
VARIANT arg1;
arg1.vt = VT_ARRAY|VT_BST R;
arg1.parray = ip_list;
hr = pInInst->Put(ArgName1 , 0, &arg1, 0);
VARIANT arg2;
arg2.vt = VT_ARRAY|VT_BST R;;
arg2.parray = mask_list;
hr = pInInst->Put(ArgName2 , 0, &arg2, 0);
VARIANT arg3;
arg3.vt = VT_ARRAY|VT_BST R;;
arg3.parray = gw_list;
hr = pInInst_gw->Put(ArgName3 , 0, &arg3, 0);
VARIANT arg4;
arg4.vt = VT_ARRAY|VT_BST R;;
arg4.parray =dns_list;
hr = pInInst_dns->Put(ArgName4 , 0, &arg4, 0);
// Call the method - enable static
hr = pNamespace->ExecMethod(Ins tancePath, 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(strReturnV alue, 0, &ret_value, 0, 0);
long ret = V_I4(&ret_value );
if(ret != 0)
{
cout << "EnableStat ic return code = " << ret;
}
else
{ cout << "EnableStat ic succeeded" << endl;
}
//calling setGateways Method
hr = pNamespace->ExecMethod(Ins tancePath, 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(strReturnV alue1, 0, &ret_value1, 0, 0);
long ret1 = V_I4(&ret_value 1);
if(ret1 != 0)
{
cout << "SetGateway s method return code = " << ret1;
}
else
{ cout << "SetGateway s succeeded" << endl;
}
//calling setDNS server order Method
hr = pNamespace->ExecMethod(Ins tancePath, MethodName2, 0, NULL, pInInst_dns, &pOutInst_dn s, 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(strReturnV alue2, 0, &ret_value2, 0, 0);
long ret2 = V_I4(&ret_value 2);
if(ret2 != 0)
{
//cout << "Set DNS Server order return code = " << ret2;
}
else
{
//cout << "Set DNS Server Order Succeeded" << endl;
}
// Free up resources
SysFreeString(s trReturnValue);
VariantClear(&r et_value);
SysFreeString(s trReturnValue1) ;
VariantClear(&r et_value1);
SysFreeString(s trReturnValue2) ;
VariantClear(&r et_value2);
SysFreeString(i p);
SysFreeString(m ask);
SysFreeString(g w);
SysFreeString(d ns);
SafeArrayDestro y(ip_list);
SafeArrayDestro y(mask_list);
SafeArrayDestro y(gw_list);
SafeArrayDestro y(dns_list);
VariantClear(&a rg1);
VariantClear(&a rg2);
VariantClear(&a rg3);
VariantClear(&a rg4);
SysFreeString(p ath);
SysFreeString(C lassPath);
SysFreeString(I nstancePath);
SysFreeString(M ethodName);
SysFreeString(A rgName1);
SysFreeString(A rgName2);
SysFreeString(M ethodName1);
SysFreeString(A rgName3);
SysFreeString(M ethodName2);
SysFreeString(A rgName4);
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;
}