将固定和字节*从C#转换为vb.net [英] Convert fixed and byte* from c# to vb.net
问题描述
我在c#中具有以下代码,并希望在VB.NET中对其进行转换.我不确定 fixed
和 byte *
是什么以及如何转换它们.Telerik转换器在此方面不提供任何帮助.
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);
}
有什么想法吗?
推荐答案
由于VB.NET不支持指针,因此您必须使用 IntPtr
.最简单的方法是使用 AddrOfPinnedObject
方法 以将其指针作为 IntPtr
.
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
请注意,这更多是一种快速而肮脏的解决方案.不能像这样使用 GCHandle
,但是它可以工作,并且(AFAIK)仍然可以使用.还有其他(更长)的方法可以更专门地为这类事情设计.
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.
这篇关于将固定和字节*从C#转换为vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!