动态链接库与c_

上传人:wm****3 文档编号:42042276 上传时间:2018-05-31 格式:DOC 页数:8 大小:41.50KB
返回 下载 相关 举报
动态链接库与c__第1页
第1页 / 共8页
动态链接库与c__第2页
第2页 / 共8页
动态链接库与c__第3页
第3页 / 共8页
动态链接库与c__第4页
第4页 / 共8页
动态链接库与c__第5页
第5页 / 共8页
点击查看更多>>
资源描述

《动态链接库与c_》由会员分享,可在线阅读,更多相关《动态链接库与c_(8页珍藏版)》请在金锄头文库上搜索。

1、一、发生的背景 在研发新项目中使用了新的语言研发 c#和新的技术方案 web service,不过在新项目中,一些旧的模块需要继续使用,一般是采用 c 或 c+或 delphi 编写的,怎么利用旧模块对于研发人员来说,有三种可用方法供选择:第一、将 c 或 c+函数用 c#完全改写一遍,这样整个项目代码比较统一,维护也方便一些。不过尽管微软及某些书籍说,c#和 c+怎么接近,不过改写起来还是非常痛苦的事情,特别是 c+里的指针和内存操作;第二、将 c 或 c+函数封装成 com,在 c#中调用 com 比较方便,只是在封装时需要处理 c 或 c+类型和 com 类型之间的转换,也有一些麻烦,另

2、外 com 还需要注册,注册次数多了又可能导致混乱;第三、将 c 或 c+函数封装成动态链接库,封装的过程简单,工作量不大。因此我决定采用加载动态链接库的方法实现,于是产生了在 c#中怎么调用自定义的动态链接库问题,我在网上搜索相关主题,发现一篇调用系统 api 的文章,不过没有说明怎么解决此问题,在msdn 上也没有相关周详说明。基于此,我决定自己从简单出发,逐步试验,看看能否达到自己的目标。 (说明一点:我这里改写为什么非常怕麻烦,我改写的代码是变长加密算法函数,代码有 600 多行,对算法本身不熟悉,算法中指针和内存操作太多,要想确保算法正确,最可行的方法就是少动代码,否则只要有一点点差

3、错,就不能肯定算法和以前兼容) 二、技术实现 下面看看怎么逐步实现动态库的加载,类型的匹配: 动态链接库函数导出的定义,这个不必多说,大家参考下面宏定义即可: #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=“ my

4、sum “,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)

5、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”, st

6、rdest); 运行查看结果 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)

7、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=chars

8、et.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 cla

9、ss 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

10、; 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); 运行查看结果 strtm

11、p 和 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

12、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+

13、动态库函数申明定义需要保持一致,否则会出现函数调用失败。 定义导入时,一定注意 charset 和 callingconvention 参数,否则导致调用失败或结果异常。 运行时,动态链接库放在 c#程式的目录下即可,我这里是个 c#的动态链接库,两个动态链接库就在同一个目录下运行。本来是想实现控制台程序运行时自动全屏,但是只找到 VC 下的实现方法(http:/ Win32 API 函数来存取控制台窗口,这就需要使用动态调用的方法,动态调用中使用的 Windows API 函数主要有三个,即:Loadlibrary,GetProcAddress 和 Freelibrary。步骤如下:1 Lo

14、adlibrary: 装载指定 DLL 动态库2 GetProcAddress:获得函数的入口地址3 Freelibrary: 从内存中卸载动态库但是 C#中是没有函数指针,无法直接使用 GetProcAddress 返回的入口地址。后来找到资料,其实.NET 2.0 新增了 Marshal.GetDelegateForFunctionPointer 方法可以满足这个要求,MSDN 里的解释是:将非托管函数指针转换为委托。后面的事就简单啦,我把它编成了一个类来方便调用。using System;using System.Collections.Generic;using System.Text

15、;using System.Runtime.InteropServices;namespace public class DllInvokeWin API#region Win APIDllImport(“kernel32.dll“)private extern static IntPtr LoadLibrary(string path);DllImport(“kernel32.dll“)private extern static IntPtr GetProcAddress(IntPtr lib, string funcName);DllImport(“kernel32.dll“)privat

16、e extern static bool FreeLibrary(IntPtr lib);#endregionprivate IntPtr hLib; public DllInvoke(String DLLPath)hLib = LoadLibrary(DLLPath);DllInvoke()FreeLibrary(hLib); /将要执行的函数转换为委托public Delegate Invoke (string APIName,Type t) IntPtr api = GetProcAddress(hLib, APIName);return (Delegate)Marshal.GetDelegateFo

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号