第一:char[n]别随便转为string,string会自动给你截取‘\0’,结果还是不对,还是老老实实的把c++的char[n]转为c#的char[n]
// PST数据头
struct SQPstHeader
{
char szVer[5];
char szExt[3];
int nWorkStep;
int
bManuBW;
int
nAutoThres;
int nManuThres;
};
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct SQPstHeader {
/// char[5]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=5)]
public char[] szVer;
/// char[3]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3)]
public char[] szExt;
/// int
public int nWorkStep;
/// int
public int bManuBW;
/// int
public int nAutoThres;
/// int
public int nManuThres;
}
第二,传给c++中的字符串LPCTSTR 得注意编码,要不然用c#的string dllimport方式传过去就是乱码。下面这个加了CharSet = CharSet.Unicode就对了。
c++:DLLEXPORT SQPstData* STDCALL LoadPst70( LPCTSTR lpszFile );
c#: [DllImportAttribute("xxxx.dll", EntryPoint = "LoadPst70", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr LoadPst70(string lpszFile);
先记这么两个,我觉得肯定不只这么两个的,以后再补充。