Computing.Net > Forums > Programming > pointer to byte array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

pointer to byte array

Reply to Message Icon

Name: Chi Happens
Date: February 21, 2005 at 10:20:07 Pacific
OS: XP Pro
CPU/Ram: n/a
Comment:

Ok, i have gotten pretty far with SNMP and C#, but now what is stumping me is how to create an IntPrt from a string.

Here is my code:
private void SendMess(string SendCode,string CodeDesc,string PsapName,string AlarmServer)
{
SNMPAPI_STATUS Result = new SNMPAPI_STATUS();

// set up the call back function
SnmpAPI.SnmpCallback SnmpCB = new SnmpAPI.SnmpCallback(OnSnmpMessage);

// get the local and remote ip addresses
string LocalAddr = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].ToString();
string RemoteAddr = System.Net.Dns.GetHostByName(AlarmServer).AddressList[0].ToString();

int major, minor, level, translate, retran;

// call SnmpStartup
Result = SnmpAPI.SnmpStartup(out major, out minor, out level, out translate, out retran);
if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS)
throw new Exception("SNMP Initialization Error");

// call snmpcreatesession
IntPtr Session = SnmpAPI.SnmpCreateSession(IntPtr.Zero, 0, SnmpCB, IntPtr.Zero);
if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE)
throw new Exception("SNMP Create Session Error");

// create entities for the source and destination addresses
IntPtr Source = SnmpAPI.SnmpStrToEntity(Session,LocalAddr);
IntPtr Destin = SnmpAPI.SnmpStrToEntity(Session,RemoteAddr);

// create the oids
// oid 1
WinSnmp.SMIOID OID1 = new SMIOID();
Result = SnmpAPI.SnmpStrToOid("1.3.6.1.4.1.9876.1",ref OID1);
if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE)
throw new Exception("SNMP StrToOid Failure");
// oid 2
WinSnmp.SMIOID OID2 = new SMIOID();
Result = SnmpAPI.SnmpStrToOid("1.3.6.1.4.1.9876.2",ref OID2);
if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE)
throw new Exception("SNMP Initialization Error");

// oid 3
WinSnmp.SMIOID OID3 = new SMIOID();
Result = SnmpAPI.SnmpStrToOid("1.3.6.1.4.1.9876.3",ref OID3);
if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE)
throw new Exception("SNMP Initialization Error");

// create te first variable list
WinSnmp.SMIVALUE Val1 = new SMIVALUE();
WinSnmp.SMIOCTETS Oct1 = new SMIOCTETS();
Oct1.size = (uint)SendCode.Length;
byte[] c1 = new byte[] {SendCode.ToCharArray()};
Oct1.octets = c1; //-- MY PROBLEM IS HERE
Val1.type = WinSnmp.SNMPAPI_SYNTAX.SNMP_SYNTAX_OCTETS;
Val1.val.str = Oct1;
IntPtr VbL1 = SnmpAPI.SnmpCreateVbl(Session,ref OID1,ref Val1);


// create second variable list
WinSnmp.SMIVALUE Val2 = new SMIVALUE();
IntPtr Code2 = SnmpAPI.SnmpStrToEntity(Session,CodeDesc);
WinSnmp.SMIOCTETS Oct2 = new SMIOCTETS();
Oct2.size = (uint)CodeDesc.Length;
Oct2.octets = Code2;
Val2.type = WinSnmp.SNMPAPI_SYNTAX.SNMP_SYNTAX_OCTETS;
Val2.val.str = Oct2;
IntPtr VbL2 = SnmpAPI.SnmpCreateVbl(Session,ref OID2,ref Val2);

// create third variable list
WinSnmp.SMIVALUE Val3 = new SMIVALUE();
IntPtr Code3 = SnmpAPI.SnmpStrToEntity(Session,PsapName);
WinSnmp.SMIOCTETS Oct3 = new SMIOCTETS();
Oct3.size = (uint)PsapName.Length;
Oct3.octets = Code3;
Val3.type = WinSnmp.SNMPAPI_SYNTAX.SNMP_SYNTAX_OCTETS;
Val3.val.str = Oct3;
IntPtr VbL3 = SnmpAPI.SnmpCreateVbl(Session,ref OID3,ref Val3);

// now create the protocol data units
IntPtr Pdu1 = SnmpAPI.SnmpCreatePdu(Session,(int)WinSnmp.SNMPAPI_PDU.SNMP_PDU_SET,0,0,0,VbL1);
IntPtr Pdu2 = SnmpAPI.SnmpCreatePdu(Session,(int)WinSnmp.SNMPAPI_PDU.SNMP_PDU_SET,0,0,0,VbL2);
IntPtr Pdu3 = SnmpAPI.SnmpCreatePdu(Session,(int)WinSnmp.SNMPAPI_PDU.SNMP_PDU_SET,0,0,0,VbL3);

// now get the destination octets
WinSnmp.SMIOCTETS dOct1 = new SMIOCTETS();
dOct1.size = (uint)SendCode.Length;
dOct1.octets = Code1;
IntPtr Context1 = SnmpAPI.SnmpStrToContext(Session,ref dOct1);

WinSnmp.SMIOCTETS dOct2 = new SMIOCTETS();
dOct2.size = (uint)CodeDesc.Length;
dOct2.octets = Code2;
IntPtr Context2 = SnmpAPI.SnmpStrToContext(Session,ref dOct2);

WinSnmp.SMIOCTETS dOct3 = new SMIOCTETS();
dOct3.size = (uint)PsapName.Length;
dOct3.octets = Code1;
IntPtr Context3 = SnmpAPI.SnmpStrToContext(Session,ref dOct3);

// now send the messages
SnmpAPI.SnmpSendMsg(Session,Source,Destin,Context1,Pdu1);
SnmpAPI.SnmpSendMsg(Session,Source,Destin,Context2,Pdu2);
SnmpAPI.SnmpSendMsg(Session,Source,Destin,Context3,Pdu3);

// now free the contexts
Result = SnmpAPI.SnmpFreeContext(Context1);
Result = SnmpAPI.SnmpFreeContext(Context2);
Result = SnmpAPI.SnmpFreeContext(Context3);

// now free the pdus
Result = SnmpAPI.SnmpFreePdu(Pdu1);
Result = SnmpAPI.SnmpFreePdu(Pdu2);
Result = SnmpAPI.SnmpFreePdu(Pdu3);

// now free the variable lists
SnmpAPI.SnmpFreeVbl(VbL1);
SnmpAPI.SnmpFreeVbl(VbL2);
SnmpAPI.SnmpFreeVbl(VbL3);

// now free the entities
SnmpAPI.SnmpFreeEntity(Code1);
SnmpAPI.SnmpFreeEntity(Code2);
SnmpAPI.SnmpFreeEntity(Code3);

// now free the oids
SnmpAPI.SnmpFreeDescriptor((int)WinSnmp.SNMPAPI_SYNTAX.SNMP_SYNTAX_OID,ref OID1);
SnmpAPI.SnmpFreeDescriptor((int)WinSnmp.SNMPAPI_SYNTAX.SNMP_SYNTAX_OID,ref OID2);
SnmpAPI.SnmpFreeDescriptor((int)WinSnmp.SNMPAPI_SYNTAX.SNMP_SYNTAX_OID,ref OID3);

// call snmpClose
Result = SnmpAPI.SnmpClose(Session);

// finally call SnmpCleanup
Result = SnmpAPI.SnmpCleanup();


}


so the octets item is an IntPtr...but all I got is a byte array.

Any help would be greatly appreciated.
Chi

They mostly come at night...mostly



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: February 22, 2005 at 09:55:42 Pacific
Reply:

i figured it out. i now have a sweet snmp interop. thanks to everyone that responded...oh yeah NO ONE DID!

lol
Chi

They mostly come at night...mostly


0

Response Number 2
Name: JamieHolloway
Date: March 5, 2005 at 12:30:27 Pacific
Reply:

willing to share your solution? (I have same problem).


0

Response Number 3
Name: JamieHolloway
Date: March 5, 2005 at 20:31:58 Pacific
Reply:

This seems to work. Marshal the string and set pointer to implicity acquired unmanaged memory. CommString is a 'regular' c# string object...lastly free unmanaged storage after snmp calls -->

SMIOCTETS oct = new SMIOCTETS();
oct.size = (uint) CommString.Length;
oct.octets = Marshal.StringToHGlobalAnsi(CommString);
ctxt = SnmpAPI.SnmpStrToContext(SessHndl, ref oct); // ctxt==context pointer
...do work...
Marshal.FreeHGlobal(oct.octets);


0

Response Number 4
Name: rohit_rangroo
Date: March 6, 2005 at 22:54:26 Pacific
Reply:

hi
I am stuck in the same position.
Could you plz... help

thanks
rohit rangroo


0

Response Number 5
Name: Chi Happens
Date: March 11, 2005 at 06:51:10 Pacific
Reply:

http://www.evolutionsoftwarellc.com/downloads/mmalo_snmp.rar

email me for the password.

Chi

They mostly come at night...mostly


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

picture property in VB6 Row Number



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: pointer to byte array

Pointer to array of character pointers? www.computing.net/answers/programming/pointer-to-array-of-character-pointers/2809.html

neither array or pointer? www.computing.net/answers/programming/neither-array-or-pointer/3268.html

pointers to arrays www.computing.net/answers/programming/pointers-to-arrays/9741.html