第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函数名),返回结果为字符串:
LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a); return a;}
C# 导入定义:
public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, string b); } 在C#中调用测试: string strDest=""; string strTmp= RefComm.mySum("12345", strDest); 运行查看结果 strTmp 为"12345",但是strDest为空。我修改动态链接库实现,返回结果为串b: LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a) return b;} 修改 C# 导入定义,将串b修改为ref方式:public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, ref string b); } 在C#中再调用测试: string strDest=""; string strTmp= RefComm.mySum("12345", ref strDest); 运行查看结果 strTmp 和 strDest 均不对,含不可见字符。再修改 C# 导入定义,将CharSet从Auto修改为Ansi:public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, string b); } |
正在阅读:C#中简单调用c/c++旧模块C#中简单调用c/c++旧模块
2005-06-14 10:18
出处:
责任编辑:moningfeng
键盘也能翻页,试试“← →”键