尝试从作为IntPtr传递的托管内存中复制和复制时使用memcpy进行Accessviolation

时间:2019-06-24 09:38:57

标签: c# c++ dllimport access-violation memcpy

我试图通过将两个指针传递给C ++函数(由C#代码调用)来简单地将一个缓冲区复制到另一个缓冲区。

也尝试过:

[DllImport(dllName, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr ConvertYUV420pToRGB24(byte[] src, byte[] dst);

C ++函数:

extern "C" __declspec(dllexport) uint8_t* __stdcall ConvertYUV420pToRGB24(uint8_t* src, uint8_t* dst) {
    errno_t err;

    err = memcpy_s(dst, 3110400, src, 3110400);

    if (err)
        return NULL;
    else
        return dst;
}

DLLImport

[DllImport(dllName, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr ConvertYUV420pToRGB24(IntPtr src, IntPtr dst);

调用C#代码

GCHandle srcPtr;
GCHandle dstPtr;

byte[] result = new byte[3110400];
byte[] srcFrame = new byte[3110400];


srcPtr = GCHandle.Alloc(srcFrame);
dstPtr = GCHandle.Alloc(result);

FFmpegInterface.ConvertYUV420pToRGB24(GCHandle.ToIntPtr(srcPtr), GCHandle.ToIntPtr(dstPtr));

结果:违反访问权限

预期:只需复制

0 个答案:

没有答案