I'm currently trying to integrate a C++ DLL into our C# application, but I'm not able to identify the correct way to call one of their methods.
C++ methods definition:
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++ example code:
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);
My C# conversion:
[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);
If I specify a default value for PicBuffer, using [MarshalAs(UnmanagedType.ByValArray, SizeConst = 99999)], I don't receive a value in lPicSize, and if I don't specify a default value I get a value in lPicSize but it isn't the correct value for PicBuffer.
What am I missing here?