




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、在 c#调用 c+编写的 com dll 封装库时会出现两个问题:1数据类型转换问题2指针或地址参数传送问题首先是数据类型转换问题。因为c#是.net 语言,利用的是 .net 的基本数据类型,所以实际上是将c+的数据类型与 .net 的基本数据类型进行对应。例如 c+的原有函数是:int _stdcall functionname(unsigned char param1, unsigned short param2) 其中的参数数据类型在c#中,必须转为对应的数据类型。如:dllimport(“ com dll path/file ”)extern static int functionn
2、ame(byte param1, ushort param2) 因为调用的是 _stdcall 函数,所以使用了p/invoke 的调用方法。其中的方法functionname必须声明为静态外部函数,即加上extern static声明头。我们可以看到,在调用的过程中, unsigned char变为了 byte,unsigned short变为了 ushort。变换后,参数的数据类型不变,只是声明方式必须改为.net 语言的规范。我们可以通过下表来进行这种转换:win32 types clr type char, int8, sbyte, char system.sbyte short, s
3、hort int, int16, short system.int16 int, long, long int, int32, long32, bool , int system.int32 _int64, int64, longlong system.int64 unsigned char, uint8, uchar , byte system.byte unsigned short, uint16, ushort, word, atom, wchar , _wchar_t system.uint16 unsigned, unsigned int, uint32, ulong32, dwor
4、d32, ulong, dword, uint system.uint32 unsigned _int64, uint64, dwordlong, ulonglong system.uint64 float, float system.single double, long double, double system.double 之后再将 clr 的数据类型表示方式转换为c#的表示方式。这样一来,函数的参数类型问题就可以解决了。现在,我们再来考虑下一个问题,如果要调用的函数参数是指针或是地址变量,怎么办?对于这种情况可以使用c#提供的非安全代码来进行解决,但是,毕竟是非托管代码,垃圾资源处理
5、不好的话对应用程序是很不利的。所以还是使用c#提供的 ref 以及 out 修饰字比较好。同上面一样,我们也举一个例子:int _stdcall functionname(unsigned char ¶m1, unsigned char *param2) 在 c#中对其进行调用的方法是:dllimport(“ com dll path/file ”)extern static int functionname(ref byte param1, ref byte param2) 看到这,可能有人会问, &是取地址, *是传送指针,为何都只用ref 就可以了呢?一种可能的解
6、释是ref 是一个具有重载特性的修饰符,会自动识别是取地址还是传送指针。在实际的情况中,我们利用参数传递地址更多还是用在传送数组首地址上。如:byte param1 = new param1(6); 在这里我们声明了一个数组,现在要将其的首地址传送过去,只要将param1数组的第一个元素用ref 修饰。具体如下:dllimport(“ com dll path/file ”)extern static int functionname(ref byte param11, ref byte param2) 一、发生的背景在开发新项目中使用了新的语言开发 c# 和新的技术方案 web servic
7、e ,但是在新项目中,一些旧的模块需要继续使用,一般是采用 c 或 c+ 或 delphi 编写的,如何利用旧模块对于开发人员来说,有三种可用方法供选择:第一、将 c 或 c+ 函数用 c# 彻底改写一遍,这样整个项目代码比较统一,维护也方便一些。但是尽管微软以及某些书籍说,c# 和 c+ 如何接近,但是改写起来还是很痛苦的事情, 特别是 c+ 里的指针和内存操作;第二、将 c 或 c+ 函数封装成 com ,在 c# 中调用 com 比较方便,只是在封装时需要处理 c 或 c+ 类型和 com 类型之间的转换,也有一些麻烦,另外 com 还需要注册,注册次数多了又可能导致混乱;第三、将 c
8、或 c+ 函数封装成动态链接库,封装的过程简单,工作量不大。因此我决定采用加载动态链接库的方法实现,于是产生了在 c# 中如何调用自定义的动态链接库问题,我在网上搜索相关主题,发现一篇调用系统 api 的文章, 但是没有说明如何解决此问题,在 msdn 上也没有相关详细说明。基于此,我决定自己从简单出发,逐步试验,看看能否达到自己的目标。(说明一点:我这里改写为什么很怕麻烦,我改写的代码是变长加密算法函数,代码有 600 多行,对算法本身不熟悉,算法中指针和内存操作太多,要想保证算法正确,最可行的方法就是少动代码,否则只要有一点点差错,就不能肯定算法与以前兼容)二、技术实现下面看看如何逐步实现
9、动态库的加载,类型的匹配,动态链接库函数导出的定义,这个不需要多说,大家参考下面宏定义即可:#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,callingconvent
10、ion=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
11、 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为空。我修改动态链接库实现,返回结
12、果为串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#中再调用
13、测试: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 str
14、ing 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,call
15、ingconvention=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=chars
16、et.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
17、 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); 运行查看结果 is
18、um 和 c 均为 5,调用正确。经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 c# 定义导入,有此基础,很快我实现了变长加密函数在 c# 中的调用,至此目标实现。三、结论在 c# 中调用 c+ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 c# 的导入定义,则需要使用引用(ref )定义。对于函数返回值,c# 导入定义和 c+ 动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意 charset 和 callingconvention 参数,否则导致调用失败或结果异常。运行时,动态链接库放在 c#
19、程序的目录下即可,我这里是一个 c# 的动态链接库,两个动态链接库就在同一个目录下运行。c#(.net )中的 dllimport 2008-10-26 10:32 大家在实际工作学习c#的时候,可能会问:为什么我们要为一些已经存在的功能(比如windows 中的一些功能,c+ 中已经编写好的一些方法)要重新编写代码,c# 有没有方法可以直接都用这些原本已经存在的功能呢?答案是肯定的,大家可以通过c#中的 dllimport直接调用这些功能。dllimport所在的名字空间复制保存using system.runtime.interopservices; msdn 中对 dllimportat
20、tribute的解释是这样的:可将该属性应用于方法。dllimportattribute 属性提供对从非托管 dll 导出的函数进行调用所必需的信息。作为最低要求,必须提供包含入口点的 dll 的名称。dllimport 属性定义如下:复制保存namespace system.runtime.interopservices attributeusage(attributetargets.method) publicclass dllimportattribute : system.attribute public dllimportattribute(string dllname) /*.*/
21、 public callingconvention callingconvention; public charset charset; publicstring entrypoint; publicbool exactspelling; publicbool preservesig; publicbool setlasterror; publicstring value get /*.*/ 说明:1、dllimport只能放置在方法声明上。2、dllimport具有单个定位参数:指定包含被导入方法的 dll 名称的 dllname 参数。3、dllimport具有五个命名参数:a、calli
22、ngconvention 参数指示入口点的调用约定。如果未指定 callingconvention,则使用默认值 callingconvention.winapi。b、charset 参数指示用在入口点中的字符集。如果未指定 charset ,则使用默认值charset.auto 。c、entrypoint 参数给出 dll 中入口点的名称。如果未指定 entrypoint,则使用方法本身的名称。d、exactspelling 参数指示 entrypoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 exactspelling,则使用默认值 false。e、preservesig 参数
23、指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 hresult 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 preservesig,则使用默认值 true 。f 、setlasterror 参数指示方法是否保留 win32 上一错误 。如果未指定 setlasterror,则使用默认值 false。4、它是一次性属性类。5、此外,用 dllimport 属性修饰的方法必须具有 extern 修饰符。dllimport的用法:复制保存dllimport(mydllimport.dll) privatestaticexternint mysu
24、m(int a, int b); 一 在 c#程序设计中使用win32 类库常用对应类型:1、dword 是 4 字节的整数,因此我们可以使用 int 或 uint 作为 c# 对应类型。2、bool 类型与 bool 对应。示例一:调用 beep() api 来发出声音beep() 是在 kernel32.lib 中定义的,在msdn 中的定义, beep 具有以下原型:复制保存bool beep(dword dwfreq, / 声音频率 dword dwduration / 声音持续时间); 用 c# 编写以下原型:复制保存dllimport(kernel32.dll) publicsta
25、ticexternbool beep( int frequency, int duration); 示例二:枚举类型和常量messagebeep() 是在 user32.lib 中定义的,在msdn 中的定义, messagebeep具有以下原型:复制保存bool messagebeep(uint utype / 声音类型); 用 c#编写一下原型:复制保存publicenum beeptype simplebeep = -1, iconasterisk = 0 x00000040, iconexclamation = 0 x00000030, iconhand = 0 x00000010,
26、iconquestion = 0 x00000020, ok = 0 x00000000, utype 参数实际上接受一组预先定义的常量,对于 utype 参数,使用 enum 类型是合乎情理的。复制保存dllimport(user32.dll) publicstaticexternbool messagebeep(beeptype beeptype); 示例三:处理结构有时我需要确定我笔记本的电池状况。win32 为此提供了电源管理函数,搜索 msdn 可以找到 getsystempowerstatus() 函数。复制保存bool getsystempowerstatus( lpsystem
27、_power_status lpsystempowerstatus ); 此函数包含指向某个结构的指针,我们尚未对此进行过处理。要处理结构, 我们需要用 c# 定义结构。我们从非托管的定义开始:复制保存typedef struct _system_power_status byte aclinestatus; byte batteryflag; byte batterylifepercent; byte reserved1; dword batterylifetime; dword batteryfulllifetime; system_power_status, *lpsystem_powe
28、r_status; 然后,通过用 c# 类型代替 c 类型来得到 c# 版本。复制保存struct systempowerstatus byte aclinestatus; byte batteryflag; byte batterylifepercent; byte reserved1; int batterylifetime; int batteryfulllifetime; 这样,就可以方便地编写出 c# 原型:复制保存dllimport(kernel32.dll) publicstaticexternbool getsystempowerstatus( ref systempowers
29、tatus systempowerstatus); 在此原型中,我们用“ ref ”指明将传递结构指针而不是结构值。这是处理通过指针传递的结构的一般方法。此函数运行良好,但是最好将 aclinestatus 和 batteryflag 字段定义为 enum:复制保存enum aclinestatus : byte offline = 0, online = 1, unknown = 255, enum batteryflag : byte high = 1, low = 2, critical = 4, charging = 8, nosystembattery = 128, unknown
30、= 255, 请注意,由于结构的字段是一些字节,因此我们使用 byte 作为该 enum 的基本类型示例四:处理字符串二 c# 中调用 c+代码int 类型复制保存dllimport(mydll.dll) / 返回个 int 类型publicstaticexternint mysum ( int a1, int b1); 复制保存/dll 中申明externc _declspec(dllexport) int winapi mysum(int a2, int b2) /a2 b2不能改变a1 b1 /a2=. /b2=. return a+b; 参数传递int 类型复制保存publicstat
31、icexternint mysum ( refint a1, refint b1); 复制保存/dll 中申明externc _declspec(dllexport) int winapi mysum(int *a2, int *b2) / 可以改变 a1, b1 *a2=. *b2=. return a+b; dll 需传出 char *类型复制保存dllimport(mydll.dll) / 传出值publicstaticexternint mysum (stringbuilder abuf, stringbuilder bbuf ); 复制保存/dll 中申明externc _declspec(dllexport) int winapi mysu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 体验地理的研学旅行合同3篇
- 师德心灵演讲稿(11篇)
- 2024年宁波市鄞州区交通运输局下属事业单位招聘考试真题
- 2024年嘉兴市秀洲区新城街道社区卫生服务中心招聘考试真题
- 2024年桂林市资源县中峰镇中心卫生院招聘专业技术人员考试真题
- 2024年定西市安定区第二人民医院招聘村卫生所工作人员考试真题
- 生物质转化技术原理考核试卷
- 美德少年事迹材料500字(18篇)
- 买卖二手房合同范本(19篇)
- 羽绒服设计风格与创新考核试卷
- 外科感染-有芽孢厌氧菌感染(外科课件)
- 统编版语文三年级上册第七单元口语交际身边的“小事”核心素养公开课一等奖创新教学设计
- 美国制造业经济2024年度报告-2024-12-宏观大势
- 脐灸个案护理案例分享
- 《瑞幸咖啡企业财务造假问题探究》5800字(论文)
- 2024年山东省公务员录用考试《行测》真题及答案解析
- 2024年贵州省公务员考试《行测》真题及答案解析
- 2022-2024北京初二一模生物汇编:实验探究题
- 2024年肿瘤放射治疗学(中级343)专业知识卫生专业技术资格考试试题与参考答案
- 产品召回程序合同
- 职业心理健康课件
评论
0/150
提交评论