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

I have the following code in C # and would like to convert it to VB.NET. I'm not sure fixed

and byte*

and how they can be converted. The telerik converter does not give any help about this.

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

      

Any ideas?

+3


source to share


1 answer


Since VB.NET does not support pointers, you must use IntPtr

. The easiest way to do this is to mark the object as non-garbage collected using . Then you use method to get its pointer as . GCHandle

AddrOfPinnedObject

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

      



Please note that this is a quicker and dirtier solution. GCHandle

is not expected to be used this way, but it works and (AFAIK) is still fine. There are other (longer) ways to do this that have been specifically designed for this kind of thing.

+4


source







All Articles