求助:c#调用c++的dll时,数据类型对应的问题
c++中的代码:
结构体:
typedef struct
{
unsigned char strChkFlg; /* 验证类型 3-TAC校验 4-TAC生成 等*/
unsigned char strTac[14]; /* 验证码 */
} enc_def;
函数:
int SoftGenTac(enc_def *pstrInMsg)
{
~~~~~~~~~~~
}
----------------------------------------------
c#对应的
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct enc_def
{
public byte strChkFlg; /* 验证类型 3-TAC校验 4-TAC生成 等*/
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)]
public Byte[] strTac; /* 验证码 */
}
[DllImport("fepEnc.dll", EntryPoint = "?SoftGenTac@@YAHPAUkey_info_def@@PAUenc_ipc_def@@@Z", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int SoftGenTac(ref enc_def pstrInMsg);
调用方法里赋值
enc_def encIpc = new enc_def();
encIpc.strChkFlg = Convert.ToByte("4", 16);
encIpc.strTac = Encoding.Default.GetBytes("12312313212325");//加了这句,下面调用时候就报"未能封送类型,因为嵌入数组实例的长度与布局中声明的长度不匹配"
int result = SoftGenTac(ref encIpc);
请哪位达人,遇到并处理同样问题的?