Question Pointer to array (IntPtr & Marshal)

Nicte

New member
Joined
Oct 22, 2009
Messages
2
Programming Experience
1-3
Hello all,

This is my first post. Maybe someone here could help me with this.

I have to use a function that ask me for the pointer to the memory block where an array is (let's say that ask me the pointer to the array)

So my objective is to do this C#:

fixed (void* pbuffer = buffer)

In VB.NET

Searching I found the Marshal class and try this:

PHP:
buffer = New Double(143) {}
//Here code to fill the array...

Dim pbuffer As IntPtr = Marshal.AllocHGlobal(144)
Marshal.Copy(buffer, 0, pbuffer, 144)

But even that this works, after this sentence anything I want to do crash my application. So I tried the following:

PHP:
Marshal.FreeHGlobal(pbuffer)

And this prevent my application from crash. At least that seems, because after all this is executed, when I call any other method it crash my application too.

I'm new using the Marshal class, and don't know if I left something. Don't know if I have to releaseHGlobal or something.

Someone could throw a bit of light into here?

Thanks in advance for any help,

Nicte
 
Can I use:

VB.NET:
Dim gh As GCHandle = GCHandle.Alloc (SomeReferenceType, GCHandleType.Pinned)
Dim AddrOfMyString As IntPtr = gh.AddrOfPinnedObject()

To alloc an array?
 
Last edited:
Back
Top