简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM