473,797 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to handle segmentation errors in WMI code

1 New Member
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;

}
Jan 18 '08 #1
0 1588

Sign in to post your reply or Sign up for a free account.

Similar topics

16
9000
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those who don't speak french arbre means tree.
3
2125
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will be returned by gcc compiler on linux. What are the other "major" reasons to have "Segmentation Fault"? I hope this is not a stupid question and someone is will to give me some exampls(part of code) with errors a newer might esasily make and
1
4096
by: Maria | last post by:
Hi, I have read about paging, segmentation and paged segmentation and I believe I have (nearly) understood how these techniques are implemented in hardware. However, I am till confused about the some details which I'll highly appreciated your assistance on. 1- When using pure paging and for a page size equal to 4KB=2^12, each page should be located at 4KB's offset in the main memory. There is no similar restriction with segmentation...
25
3391
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. Inside each structure, I have two members: a list of strings, and a string. I have made a sample program below that exhibits the error I am having. I also read about Valgrind, and used it to tell me where I was getting the segmentation fault,...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.