将结构数组从c#封送到c++;具有固定大小的缓冲区
我使用以下代码将结构数组封送到c++:将结构数组从c#封送到c++;具有固定大小的缓冲区,c#,c++,struct,pinvoke,marshalling,C#,C++,Struct,Pinvoke,Marshalling,我使用以下代码将结构数组封送到c++: [DllImport("IPD.dll", EntryPoint = "process", CallingConvention = CallingConvention.Cdecl)] public static extern Pixel* process(Pixel* pixels, int numPoints, uint processingFactor); [StructLayout(LayoutKind.Sequential)] public
[DllImport("IPD.dll", EntryPoint = "process", CallingConvention = CallingConvention.Cdecl)]
public static extern Pixel* process(Pixel* pixels, int numPoints, uint processingFactor);
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Pixel
{
public fixed byte x[3];
public uint numebrOfPixels;
}
...
Pixel[] pixels = extractPixels(image);
fixed (Pixel* ptr = pixels)
{
Pixel* result = process(ptr, pixels.Length,processingFactor);
}
为了填充我的结构,我使用以下代码:
//Looping and populating the pixels
for(i=0;i<numOfPixels;i++)
{
fixed (byte* p = pixels[i].x)
{
p[0] = r;
p[1] = g;
p[2] = b;
}
}
//循环和填充像素
对于(i=0;i),可以使用指定需要从调用者封送到被调用者的参数:
[DllImport("IPD.dll", EntryPoint = "process", CallingConvention = CallingConvention.Cdecl)]
public static extern Pixel* process([In] Pixel* pixels, int numPoints, uint processingFactor);
确保封送拆收器不复制数组成员的方法是,封送拆收器不知道数组的大小。它无法封送数组内容。您只需传递固定数组的地址。不会复制该数组的内容。[In]
是参数的默认值仅供参考:如果返回的像素*
与作为输入参数传递的像素不同,那么最好将它们作为第二个像素*pixelOut
作为输入参数传递,这样C侧就可以有一个像素[]
即使对于输出像素也是如此。谢谢!正如您所看到的,我正在使用固定缓冲区,因此当我填充struct pixels数组时,我需要使用固定范围。这会导致性能下降。还有什么替代方法?使用uint
代替固定缓冲区,并使用位操作形成值。