如何在 C# 中加载自己编写的动态链接库( DLL )
李伟华 msn : [email protected]
摘要 本文主要讲述如何在 C# 中逐步实现加载自己用 C++ 语言编写的动态链接库,以及在导入时如何进行 C# 和 C++ 语言的数据类型匹配
关键词 C# C++ 动态链接库 加载 数据类型匹配
一、 发生的背景
在开发新项目中使用了新的语言开发 C# 和新的技术方案 WEB Service ,但是在新项目中,一些旧的模块需要继续使用,一般是采用 C 或 C++ 或 Delphi 编写的,如何利用旧模块对于开发人员来说,有三种可用方法供选择:第一、将 C 或 C++ 函数用 C# 彻底改写一遍,这样整个项目代码比较统一,维护也方便一些。但是尽管微软以及某些书籍说, C# 和 C++ 如何接近,但是改写起来还是很痛苦的事情,特别是 C++ 里的指针和内存操作;第二、将 C 或 C++ 函数封装成 COM ,在 C# 中调用 COM 比较方便,只是在封装时需要处理 C 或 C++ 类型和 COM 类型之间的转换,也有一些麻烦,另外 COM 还需要注册,注册次数多了又可能导致混乱;第三、将 C 或 C++ 函数封装成动态链接库,封装的过程简单,工作量不大。因此我决定采用加载动态链接库的方法实现,于是产生了在 C# 中如何调用自定义的动态链接库问题,我在网上搜索相关主题,发现一篇调用系统 API 的文章,但是没有说明如何解决此问题,在 MSDN 上也没有相关详细说明。基于此,我决定自己从简单出发,逐步试验,看看能否达到自己的目标。
(说明一点:我这里改写为什么很怕麻烦,我改写的代码是变长加密算法函数,代码有 600 多行,对算法本身不熟悉,算法中指针和内存操作太多,要想保证算法正确,最可行的方法就是少动代码,否则只要有一点点差错,就不能肯定算法与以前兼容)
二、 技术实现
下面看看如何逐步实现动态库的加载,类型的匹配:
动态链接库函数导出的定义,这个不需要多说,大家参考下面宏定义即可:
#define LIBEXPORT_API extern "C" __declspec(dllexport)
第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和:
LIBEXPORT_API int mySum(int a,int b){ return a+b;}
C# 定义导入定义:
public class RefComm
{
[DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet. Auto ,CallingConvention=CallingConvention. StdCall )] public static extern int mySum ( int a,int b );
}
在 C# 中调用测试:
int iSum= RefComm. mySum(2,3);
运行查看结果 iSum 为 5 ,调用正确。第一步试验完成,说明在 C# 中能够调用自定义的动态链接库函数。
第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函数名),返回结果为字符串:
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# 中再调用测试:
string strDest=””;
string strTmp= RefComm. mySum(“12345”, ref strDest);
运行查看结果 strTmp 为 “12345” ,但是串 strDest 没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。
再次修改 C# 导入定义,将串 b 修改为引用( ref ):
public class RefComm
{
[DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet. Ansi ,CallingConvention=CallingConvention. StdCall )] public static extern string mySum (string a, ref string b );
}
运行时调用失败,不能继续执行。
第三步,修改动态链接库实现,将 b 修改为双重指针:
LIBEXPORT_API char *mySum(char *a,char **b ){sprintf((*b),”%s”,a) return *b ;}
C# 导入定义:
public class RefComm
{
[DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet. Ansi ,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 均为 “12345” ,调用正确。第三步实现了函数出口参数正确输出结果。
第四步,修改动态链接库实现,实现整数参数的输出:
LIBEXPORT_API int mySum(int a,int b,int *c ){ *c=a+b; return *c ;}
C# 导入的定义:
public class RefComm
{
[DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet. Ansi ,CallingConvention=CallingConvention. StdCall )] public static extern int mySum (int a, int b, ref int c );
}
在 C# 中调用测试:
int c=0;
int iSum= RefComm. mySum(2,3, ref c);
运行查看结果 iSum 和 c 均为 5 ,调用正确。
经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 C# 定义导入,有此基础,很快我实现了变长加密函数在 C# 中的调用,至此目标实现。
三、 结论
在 C# 中,调用 C++ 编写动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用( ref )定义。
对于函数返回值, C# 导入定义和 C++ 动态库函数申明定义需要保持一致,否则会出现函数调用失败。
定义导入时,一定注意 CharSet和CallingConvention参数,否则导致调用失败或结果异常。
运行时,动态链接库放在 C#程序的目录下即可,我这里是一个C#的动态链接库,两个动态链接库就在同一个目录下运行。