Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# String Pointers - AsnOctetString.str

Posted on 2006-05-11
3
Medium Priority
?
661 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:OctaviaIS
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1920 total points
ID: 16657903
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.
0
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 80 total points
ID: 16658098
Do this:

using System.Runtime.InteropServices

then:

AsnOctetString only a stream which is a IntPtr so:

string myString = Marshal.PtrToStringAuto(AsnOctetString );

to get the string.
0
 

Author Comment

by:OctaviaIS
ID: 16658527
Thanks chaps,

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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

571 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