alwaysVBNET
alwaysVBNET

Reputation: 3310

Convert fixed and byte* from c# to vb.net

I have the following code in c# and would like to convert it in VB.NET. I'm not sure what fixed and byte* are and how they can be converted. The telerik converter does not provide any help on this.

fixed (byte* ptrShapeBufferPtr = pointerInfo.PtrShapeBuffer)
{
    mDeskDupl.GetFramePointerShape(
           frameInfo.PointerShapeBufferSize, 
           (IntPtr)ptrShapeBufferPtr, 
           out pointerInfo.BufferSize, 
           out pointerInfo.ShapeInfo);
}

Any ideas?

Upvotes: 3

Views: 818

Answers (1)

Visual Vincent
Visual Vincent

Reputation: 18320

Since VB.NET doesn't support pointers you have to use an IntPtr instead. The most simple way to do so is to mark the object as not getting Garbage Collected using a GCHandle. Then you use the AddrOfPinnedObject method to get its pointer as an IntPtr.

Dim handle As GCHandle

Try
    handle = GCHandle.Alloc(pointerInfo.PtrShapeBuffer, GCHandleType.Pinned)
    Dim ptrShapeBufferPtr As IntPtr = handle.AddrOfPinnedObject()

    mDeskDupl.GetFramePointerShape(frameInfo.PointerShapeBufferSize, ptrShapeBufferPtr, pointerInfo.BufferSize, pointerInfo.ShapeInfo)
Finally
    If handle.IsAllocated = True Then handle.Free()
End Try

Note that this is a more of a quick and dirty solution. GCHandle isn't expected to be used like this, but it works and is (AFAIK) still okay to use. There are other (longer) ways of doing this which were more specifically designed for these kinds of things.

Upvotes: 5

Related Questions