c++ dll 中方法:
int __stdcall Comm_Send(const data_Head struHead, const wchar_t* pTLV, const int nCount)
c#调用:
[DllImport("xxx.dll", EntryPoint = "xxx", CharSet = CharSet.Auto)]
public static extern int Comm_Send(data_Head struHead, string pTLV, int nCount);
EntryPoint 中是对应c++ dll的方法名,用vs自带的命令提示行dumpbin -exports xxx.dll可以看到
CharSet 一般可不写
这里const wchar_t* -----string ,wchar_t是unicode,可用string对应,CharSet.Auto可以;
但是如果
c++ dll 中方法:
int __stdcall Comm_Send(const data_Head struHead, wchar_t* pTLV, const int nCount)
c#调用必须:
[DllImport("xxx.dll", EntryPoint = "xxx")]
public static extern int Comm_Send(data_Head struHead, StringBuilder pTLV, int nCount);
wchar_t* -----StringBuilder StringBuilder 初始化要设置一个合适的长度,大于收到的长度,不然会出现indexoutofrangeException;
如对应string会没有值,这里CharSet = CharSet.Auto,那么对StringBuilder取长度会发现和wchar_t* 的不一致,;
如果
c++ dll 中参数是struct* c#是struct[]
先写到这里。。。。。。。。。