C#如何从dll获取的结构中获取图片并在本地保存 我有一个C++封装的DLL文件,这个DLL可以提取文本信息和图片。 当我用C++调用DLL中的方法时,没有问题,我可以提取信息并保存图片。但是当我用C语言调用DLL中的方法时,程序只能提取文本,图片失败。
我的代码 C++代码 .h文件C#如何从dll获取的结构中获取图片并在本地保存 我有一个C++封装的DLL文件,这个DLL可以提取文本信息和图片。 当我用C++调用DLL中的方法时,没有问题,我可以提取信息并保存图片。但是当我用C语言调用DLL中的方法时,程序只能提取文本,图片失败。,c#,c++,C#,C++,我的代码 C++代码 .h文件 //其他代码 类型定义结构 { char-cEIDType[4]; 字符cEIDNumber[20]; char-cEIDSide[2]; 字符cEIDSignCountry[4]; char-cEIDChineseName[64]; char-cEIDName[64]; char-cEIDBirth[16]; char-cEIDSex[4]; 字符接收[4]; 字符cEIDSignPlaceNo[8]; char-cEIDValidDate[16]; 伊菲托伦;
//其他代码
类型定义结构
{
char-cEIDType[4];
字符cEIDNumber[20];
char-cEIDSide[2];
字符cEIDSignCountry[4];
char-cEIDChineseName[64];
char-cEIDName[64];
char-cEIDBirth[16];
char-cEIDSex[4];
字符接收[4];
字符cEIDSignPlaceNo[8];
char-cEIDValidDate[16];
伊菲托伦;
字符cEIDPhoto[100*1024];
}cEIDCardInfo;
DLLEXPORT WORD uu stdcall EID u ReadData(无符号字符*pIn,cEIDCardInfo*p_psCardInfo);
//其他代码
cpp文件
cEIDCardInfo信息;
int re=EID_ReadData(0,&info);
如果(re==1){
无符号字符tmp[128];
memset(tmp,0128);
memcpy(tmp,info.cEIDType,4);
printf(“cEIDType=%s\n”,tmp);
memset(tmp,0128);
memcpy(tmp,信息编号20);
printf(“cEIDNumber=%s\n”,tmp);
文件*fp=fopen(“head.jpg”、“wb”);
fwrite(info.cEIDPhoto,1,info.iPhotoLen,fp);//将成功打印文本并在本地保存图片
fclose(fp);
}
C#代码
文件描述符
C#演示
固定
改变
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100 * 1024)]
public byte[] cEIDPhoto;
在c#中,
ReadData
的签名看起来是错误的,您没有在任何地方调用它。这是否回答了您的问题?
[DllImport(@"EID.dll", CharSet = CharSet.Ansi, EntryPoint = "EID_ReadData")]
public static extern int ReadData(StringBuilder pInput, IntPtr pOut);
//other code
void GetData(){
IntPtr intPtr = IntPtr.Zero;
intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PassportInfo)));
int code = ReadData(new StringBuilder(0), intPtr);
if(code == 1)
{
PassportInfo passportInfo = (PassportInfo)Marshal.PtrToStructure(intPtr , typeof(PassportInfo));
FileStream fileStream = new FileStream("clk.png", FileMode.Create);
fileStream.Write(Encoding.Default.GetBytes(passportInfo.cEIDPhoto),0, passportInfo.iPhotoLen); //System.ArgumentException => Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection
fileStream.Close();
}
}
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100 * 1024)]
public byte[] cEIDPhoto;