c#调c++dll,ref结构体,报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”
c++中:typedefstructtagINVOICEDATA{charszFpCryptograph[116];charszFpEnVersion[8];charsz...
c++中:
typedef struct tagINVOICEDATA {
char szFpCryptograph[116];
char szFpEnVersion[8];
char szFpCode[16];
char szFpSeriaoNo[16];
char szFpDate[8];
char szFpBuyerTaxNo[22];
char szFpSellerTaxNo[22];
char szFpSum[32];
char szFpTaxRate[16];
char szFpTax[32];
char szFpTotal[32];
char szFpTaxDiskNo[16];
char szFpImage[256];
char szBarCodePic[252];
char *BarCodeData;
int nBarCode;
} INVOICEDATA, *PINVOICEDATA;
typedef int (WINAPI *lpFpGetNewPage)(PINVOICEDATA lpFpBuffer);
-------------------------------------------------------------------------------------
c#中:
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct tagINVOICEDATA//string
{
#region
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 116)]
public string szFpCryptograph;
/// <summary>
/// 加密版本号
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string szFpEnVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpCode;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpSeriaoNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string szFpDate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
public string szFpBuyerTaxNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
public string szFpSellerTaxNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szFpSum;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpTaxRate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szFpTax;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szFpTotal;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpTaxDiskNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szFpImage;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 252)]
public string szBarCodePic;
[MarshalAs(UnmanagedType.LPStr)]
public string BarCodeData;
public int nBarCode;
#endregion
}
[DllImport("Scanreco.dll", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "FpGetNewPage", CallingConvention = CallingConvention.StdCall)]
public static extern int FpGetNewPage(ref tagINVOICEDATA FpBuffer);
tagINVOICEDATA invoicedata = new tagINVOICEDATA();
int rst = ScanrecoC.FpGetNewPage(ref invoicedata);这个就报内存错呢? 展开
typedef struct tagINVOICEDATA {
char szFpCryptograph[116];
char szFpEnVersion[8];
char szFpCode[16];
char szFpSeriaoNo[16];
char szFpDate[8];
char szFpBuyerTaxNo[22];
char szFpSellerTaxNo[22];
char szFpSum[32];
char szFpTaxRate[16];
char szFpTax[32];
char szFpTotal[32];
char szFpTaxDiskNo[16];
char szFpImage[256];
char szBarCodePic[252];
char *BarCodeData;
int nBarCode;
} INVOICEDATA, *PINVOICEDATA;
typedef int (WINAPI *lpFpGetNewPage)(PINVOICEDATA lpFpBuffer);
-------------------------------------------------------------------------------------
c#中:
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct tagINVOICEDATA//string
{
#region
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 116)]
public string szFpCryptograph;
/// <summary>
/// 加密版本号
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string szFpEnVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpCode;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpSeriaoNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string szFpDate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
public string szFpBuyerTaxNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
public string szFpSellerTaxNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szFpSum;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpTaxRate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szFpTax;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szFpTotal;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string szFpTaxDiskNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szFpImage;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 252)]
public string szBarCodePic;
[MarshalAs(UnmanagedType.LPStr)]
public string BarCodeData;
public int nBarCode;
#endregion
}
[DllImport("Scanreco.dll", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "FpGetNewPage", CallingConvention = CallingConvention.StdCall)]
public static extern int FpGetNewPage(ref tagINVOICEDATA FpBuffer);
tagINVOICEDATA invoicedata = new tagINVOICEDATA();
int rst = ScanrecoC.FpGetNewPage(ref invoicedata);这个就报内存错呢? 展开
2018-03-26
展开全部
关于结构体传参,给你个链接,看看第四个。
另外,涉及回调有点麻烦。要用c++的回调,需要在c#里声明一个对应的委托。
内存方面,c#的new和c++new出来的内存好像不一样。容易出错。
结构体里倒数第二个,关于char*,c#里获取这个char*要用stringbuilder拿到。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |