C# String Pointers - AsnOctetString.str

Posted on 2006-05-11
Last Modified: 2012-05-05
Hi all,

I have played around with the code at [] 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 )
    { = 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!
Question by:OctaviaIS
    LVL 48

    Accepted Solution

    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.
    LVL 19

    Assisted Solution

    by:Daniel Van Der Werken
    Do this:

    using System.Runtime.InteropServices


    AsnOctetString only a stream which is a IntPtr so:

    string myString = Marshal.PtrToStringAuto(AsnOctetString );

    to get the string.

    Author Comment

    Thanks chaps,

    I hope the split of points is fair to you both!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now