在库的 C++ 标头中有以下代码
#define STR_DATE 24+1
#define STR_SIZE 32+1
#define STR_SSIZE 64+1
#define STR_MSIZE 128+1
#define STR_LSIZE 1024+1
#define STR_IPSIZE 15+1
#define STR_MOD_SIZE 20+1
#define STR_AGESIZE 4+1
#define STR_GENDERSIZE 1+1
typedef struct ADO_PINFO{
char P_ID[STR_SSIZE];
char F_Name[STR_SSIZE];
char M_Name[STR_SSIZE];
char L_Name[STR_SSIZE];
char Reg_Num[STR_SSIZE];
UINT nGender;
UINT nAge;
COleDateTime BirthDay;
char csBirthDay[STR_SIZE];
COleDateTime V_Date;
char csV_Date[STR_SIZE];
char Address[_MAX_PATH];
char SubAddress[_MAX_PATH];
char Telephone[STR_SIZE];
char H_Phone[STR_SIZE];
char csMail[STR_SSIZE];
char csPicName[_MAX_PATH];
COleDateTime InDate;
char csInDate[STR_SIZE];
}*PADO_PINFO;
_ADODLL long ADO_AddPatientData(const ADO_PINFO &pPatientInfo);
我正在尝试将 dll 导入我的 C# 应用程序:
[StructLayout(LayoutKind.Sequential)]
public struct ADO_PINFO
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 65)] public string P_ID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 65)] public string F_Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 65)] public string M_Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 65)] public string L_Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 65)] public string Reg_Num;
public uint nGender;
public uint nAge;
public DateTime BirthDay;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)] public string csBirthDay;
public DateTime V_Date;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)] public string csV_Date;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string Address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string SubAddress;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)] public string Telephone;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)] public string H_Phone;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 65)] public string csMail;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string csPicName;
public DateTime InDate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)] public string csInDate;
}
public class VatechLibrary
{
[DllImport("AdodllE.dll")]
public static extern long ADO_AddPatientData(ref ADO_PINFO patientInfo);
}
但是当我尝试共同调用它时:
var pInfo = new ADO_PINFO();
pInfo.P_ID = "77";
pInfo.F_Name = "name";
var res = VatechLibrary.ADO_AddPatientData(ref pInfo);
我得到 AccessViolationException。我究竟做错了什么?