l
1技术实现 2 3 如何逐步实现动态库的加载,类型的匹配,动态链接库函数导出的定义,参考下面宏定义即可: 4 #define LIBEXPORT_API extern "C" __declspec(dllexport) 5 6 第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和: 7 8 LIBEXPORT_API int mySum(int a,int b){ return a+b;} 9 C# 导入定义: 10 11 public class RefComm 12 { 13 [DllImport("LibEncrypt.dll", 14 EntryPoint=" mySum ", 15 CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] 16 public static extern int mySum (int a,int b); 17 } 18 在C#中调用测试: 19 20 int iSum = RefComm.mySum(,); 21 22 运行查看结果iSum为5,调用正确。第一步试验完成,说明在C#中能够调用自定义的动态链接库函数。 23 24 第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函数名),返回结果为字符串: 25 26 LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a); return a;} 27 C# 导入定义: 28 29 public class RefComm 30 { 31 [DllImport("LibEncrypt.dll", 32 EntryPoint=" mySum ", 33 CharSet=CharSet.Auto, 34 CallingConvention=CallingConvention.StdCall)] 35 public static extern string mySum (string a, string b); 36 } 37 在C#中调用测试: 38 39 string strDest=""; 40 string strTmp= RefComm.mySum("45", strDest); 41 42 运行查看结果 strTmp 为"45",但是strDest为空。我修改动态链接库实现,返回结果为串b: 43 44 LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a) return b;} 45 修改 C# 导入定义,将串b修改为ref方式: 46 47 public class RefComm 48 { 49 [DllImport("LibEncrypt.dll", 50 EntryPoint=" mySum ", 51 CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] 52 public static extern string mySum (string a, ref string b); 53 } 54 在C#中再调用测试: 55 56 string strDest=""; 57 string strTmp= RefComm.mySum("45", ref strDest); 58 运行查看结果 strTmp 和 strDest 均不对,含不可见字符。再修改 C# 导入定义,将CharSet从Auto修改为Ansi: 59 60 public class RefComm 61 { 62 [DllImport("LibEncrypt.dll", 63 EntryPoint=" mySum ", 64 CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] 65 public static extern string mySum (string a, string b); 66 } 67 在C#中再调用测试: 68 69 string strDest=""; 70 string strTmp= RefComm. mySum("45", ref strDest); 71 运行查看结果 strTmp 为"45",但是串 strDest 没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。再次修改 C# 导入定义,将串b修改为引用(ref): 72 73 public class RefComm 74 { 75 [DllImport("LibEncrypt.dll", 76 EntryPoint=" mySum ", 77 CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] 78 public static extern string mySum (string a, ref string b); 79 } 80 81 运行时调用失败,不能继续执行。 82 83 第三步,修改动态链接库实现,将b修改为双重指针: 84 85 LIBEXPORT_API char *mySum(char *a,char **b){sprintf((*b),"%s",a); return *b;} 86 C#导入定义: 87 88 public class RefComm 89 { 90 [DllImport("LibEncrypt.dll", 91 EntryPoint=" mySum ", 92 CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] 93 public static extern string mySum (string a, ref string b); 94 } 95 在C#中调用测试: 96 97 string strDest=""; 98 string strTmp= RefComm. mySum("45", ref strDest); 99 100 运行查看结果 strTmp 和 strDest 均为"45",调用正确。第三步实现了函数出口参数正确输出结果。 101 102 第四步,修改动态链接库实现,实现整数参数的输出: 103 104 LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;} 105 C#导入的定义: 106 107 public class RefComm 108 { 109 [DllImport("LibEncrypt.dll", 110 EntryPoint=" mySum ", 111 CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] 112 public static extern int mySum (int a, int b,ref int c); 113 } 114 在C#中调用测试: 115 116 int c=0; 117 int iSum= RefComm. mySum(,, ref c); 118 119 运行查看结果iSum 和c均为5,调用正确。 120 121 经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 C# 定义导入,有此基础,很快我实现了变长加密函数在 C# 中的调用,至此目标实现。 122 123 三、结论 124 125 在 C# 中调用 C++ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用(ref)定义。 126 127 对于函数返回值,C# 导入定义和 C++ 动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意 CharSet 和 CallingConvention 参数,否则导致调用失败或结果异常。运行时,动态链接库放在 C# 程序的目录下即可,我这里是一个 C# 的动态链接库,两个动态链接库就在同一个目录下运行。