【发布时间】:2015-10-24 21:42:57
【问题描述】:
我需要将一个字节数组传递给 memset,由于 P/Invoke 笨拙而需要 IntPtr。手工测试,它有效,但我正在寻求理论上的确认。这个方法对吗?
[DllImport("msvcrt.dll", EntryPoint = "memset", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr MemSet(IntPtr dest, int c, int count);
static unsafe void ZeroMemset (byte[] data)
{
fixed (byte* bytes = data) {
MemSet ((IntPtr)bytes, 0, data.Length);
}
}
【问题讨论】:
-
没有“矮胖”。您的错误是没有以合乎逻辑的方式声明 dest 参数。应该是
byte[]。通过以正常方式声明第二个参数byte来删除额外的块状。不笨重的 pinvoke marshaller 会处理其他所有事情。 -
我的意思是笨拙,一个错误。对不起。
-
@Hans 第二个参数是
int:void *memset( void *dest, int c, size_t count ); -
不,只使用 int 的低字节。 pinvoke marshaller 不需要帮助将字节提升为 int。
-
@Hans 我以为你在暗示
int是错误的