473,700 Members | 2,649 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 1583

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

Similar topics

16
8984
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
2121
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
4088
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
3367
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
9214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9075
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
8973
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
8925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7810
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4404
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3089
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
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2028
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.