C# String Pointers - AsnOctetString.str

Hi all,

I have played around with the code at [http://www.righto.com/pc/snmp.html] to get values from an SNMP device.  At the moment that seems to be working OK.

My problem comes in attempting to update the device. The code sample came with a SNMPset method that can update an integer value on the device. I need to update integers and octstrings. I've modified the code from :

      asnValue.asnType = ASN_INTEGER;
      asnValue.asnValue.number = value;

To :

      asnValue.asnType = ASN_OCTETSTRING;
      asnValue.asnValue.str = value;

However the .str data type is AsnOctetString. The number data type was integer so I could just supply this a int variable.

There is no Text property of the AsnOctetString only a stream which is a IntPtr. I guess I need to set this to the pointer of the string that has the data I want to send to the device.

How can I do this? I've tried the fixed statement but I get compiler errors with the following code :

    AsnOctetString asnOctetString = new AsnOctetString();
    string setValue = "Sample String Value" ;

    fixed( char* p = setValue )
        asnOctetString.stream = p ;

    asnValue.asnValue.str = asnOctetString;

Points will be awarded to the person who crack this, perhaps extra points for someone who can give the a polished method!
Who is Participating?
Link is not working, I try to guess. If variable is defined as IntPtr, try to allocate string using Marshal.StringToHGlobalAnsi or Marshal.StringToHGlobalUni method - depending on required string type. It allocates unmanaged string and returns IntPtr which points to this string.
Daniel Van Der WerkenIndependent ConsultantCommented:
Do this:

using System.Runtime.InteropServices


AsnOctetString only a stream which is a IntPtr so:

string myString = Marshal.PtrToStringAuto(AsnOctetString );

to get the string.
OctaviaISAuthor Commented:
Thanks chaps,

I hope the split of points is fair to you both!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.