繁体   English   中英

如何将IntPtr转换为结构?

[英]How to get an IntPtr to a struct?

我有签名的方法

public int Copy(Texture texture, Rect? srcrect, Rect? dstrect)

Rect是一个结构体,但我还需要允许调用者将null (或IntPtr.Zero )也传递给该方法。

我想然后将其传递给具有签名的DLL

[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_RenderCopy")]
internal static extern int RenderCopy(IntPtr renderer, IntPtr texture, IntPtr srcrect, IntPtr dstrect);

我希望可以做以下事情:

return SDL.RenderCopy(_ptr, texture._ptr, srcrect.HasValue ? (IntPtr)srcrect.Value : IntPtr.Zero, dstrect.HasValue ? (IntPtr)dstrect.Value : IntPtr.Zero);

但是我不能像这样强制转换结构。 我还有其他方法可以获取IntPtr吗?


替代方法是创建4个重载:

  • ref Rect, ref Rect
  • IntPtr, IntPtr
  • ref Rect, IntPtr
  • IntPtr, ref Rect

如果我需要传递两个以上的struct指针,可能会变得更加混乱。


我想出了一个解决方案,但对此有一些疑问:

public int Copy(Texture texture, Rect? srcrect=null, Rect? dstrect=null)
{
    return SDL.RenderCopy(_ptr, texture._ptr, srcrect.HasValue ? StructToPtr(srcrect) : IntPtr.Zero, dstrect.HasValue ? StructToPtr(dstrect) : IntPtr.Zero);
}

private static IntPtr StructToPtr(object obj)
{
    var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj));
    Marshal.StructureToPtr(obj, ptr, false);
    return ptr;
}

如果使用ref Rect ,就不必为该结构分配内存了-这样做有什么不同?


我做了一些实验。 ref Rect解决方案的运行速度与 Rect转换为IntPtr Rect生成IntPtr ,这使我怀疑使用ref时C#在后台执行的操作非常相似。 一旦我使它成Rect? 并将条件逻辑添加到运行速度降低50%的方法中...因此,4重载路径可能是最快的。 但是,对于100K迭代,我们正在讨论100-150ms,这意味着该方法本身非常便宜,这可能就是为什么条件产生如此显着影响的原因。 因此,我坚持使用自定义的StructToPtr解决方案,因为它是最简单的解决方案。

您想使用Marshal.StructureToPtr

您还必须为该结构分配和取消分配内存。

可以在http://www.developerfusion.com/article/84519/mastering-structs-in-c/中找到有关该主题的好博客

这就是我所拥有的,我正在使用M $ DLL中的API,原型如下所示:

HRESULT C_API(LPCWSTR name, PSTRUCT_WHATEVER *ppStruct);

结构定义如下:

typedef struct whatever {
    LPCWSTR x;
    LPCWSTR y;
}

在C#中,我定义了以下内容:

[StructLayout(LayoutKind.Sequential)]
public class WHATEVER {
    public IntPtr x;
    public IntPtr y;
}

[DllImport("msdll.dll", SetLastError=false, CharSet=CharSet.Unicode)]
    public static extern long C_API(String name, out IntPtr ppStruct);

要使用它:

IntPtr s;           
long HR = C_API("myname", out s);

WHATEVER pInfo = (WHATEVER) Marshal.PtrToStructure(s, typeof(WHATEVER));

String mystring = Marshal.PtrToStringAuto(pInfo.x);

此时,mystring =“这是一个字符串”;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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