我目前正在尝试将 C++ DLL 集成到我们的 C# 应用程序中,但我无法确定调用其中一种方法的正确方法。
C++ 方法定义:
typedef struct ImageDataAndInfo
{
unsigned char* PicBuffer; //image buffer
long lPicSize; //image data length
int iPicType; // iPicType, 0: bmp, 1: tiff, 2: jpeg
} ImageDataAndInfo,*PImageDataAndInfo;
typedef ImageAndScanError (__stdcall * ReadPictureData)(PImageDataAndInfo PicF1,
PImageDataAndInfo PicR1,
PImageDataAndInfo PicF2,
PImageDataAndInfo PicR2,
PImageDataAndInfo PicF3,
PImageDataAndInfo PicR3,
BOOL bSourceBMPSave);
typedef void (__stdcall * FreeImageDataBuffer)(PImageDataAndInfo pImageDataAndInfo);
C++ 示例代码:
ImageDataAndInfo F1,F2,F3,R1,R2,R3;
F1.lPicSize=0;
F2.lPicSize=0;
F3.lPicSize=0;
R1.lPicSize=0;
R2.lPicSize=0;
R3.lPicSize=0;
int bResult;
bResult=ReadPictureData(&F1,&R1,&F2,&R2,&F3,&R3,TRUE);
f_FreeImageDataBuffer(&F1);
...
f_FreeImageDataBuffer(&R3);
我的 C# 转换:
[StructLayout(LayoutKind.Sequential)]
public struct PImageDataAndInfo
{
[MarshalAs(UnmanagedType.ByValArray)]
public byte[] PicBuffer; //image buffer
public int lPicSize; //image data length
public int iPicType; // iPicType, 0: bmp, 1: tiff, 2: jpeg
}
[DllImport("ScanDll.dll", CallingConvention = CallingConvention.Winapi)]
public static extern ImageAndScanError ReadPictureData(
out PImageDataAndInfo PicF1,
out PImageDataAndInfo PicR1,
out PImageDataAndInfo PicF2,
out PImageDataAndInfo PicR2,
out PImageDataAndInfo PicF3,
out PImageDataAndInfo PicR3,
bool bSourceBMPSave);
PImageDataAndInfo PicF1 = new PImageDataAndInfo();
...
PImageDataAndInfo PicR3 = new PImageDataAndInfo();
ReadPictureData(
out PicF1,
out PicR1,
out PicF2,
out PicR2,
out PicF3,
out PicR3,
false);
PicBuffer
如果我为, using指定默认值[MarshalAs(UnmanagedType.ByValArray, SizeConst = 99999)]
,我不会在 中收到值lPicSize
,如果我没有指定默认值,我会在 中获得一个值,lPicSize
但它不是 的正确值PicBuffer
。
我在这里想念什么?