[英]Convert fixed and byte* from c# to vb.net
我在 c# 中有以下代碼,並想在 VB.NET 中轉換它。 我不確定什么是fixed
和byte*
以及如何轉換它們。 Telerik 轉換器對此不提供任何幫助。
fixed (byte* ptrShapeBufferPtr = pointerInfo.PtrShapeBuffer)
{
mDeskDupl.GetFramePointerShape(
frameInfo.PointerShapeBufferSize,
(IntPtr)ptrShapeBufferPtr,
out pointerInfo.BufferSize,
out pointerInfo.ShapeInfo);
}
有任何想法嗎?
由於 VB.NET 不支持指針,因此您必須改用IntPtr
。 最簡單的方法是使用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
請注意,這更像是一種快速而骯臟的解決方案。 GCHandle
預計不會像這樣使用,但它可以工作並且(AFAIK)仍然可以使用。 還有其他(更長的)方法可以做到這一點,這些方法更專為此類事情而設計。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.