Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.
ChiThey mostly come at night...mostly

i figured it out. i now have a sweet snmp interop. thanks to everyone that responded...oh yeah NO ONE DID!
lol
ChiThey mostly come at night...mostly

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

http://www.evolutionsoftwarellc.com/downloads/mmalo_snmp.rar
email me for the password.
Chi
They mostly come at night...mostly

![]() |
picture property in VB6
|
Row Number
|

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