C# 调用 Delphi DLL

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 } 18C#中调用测试: 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 } 37C#中调用测试: 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 } 54C#中再调用测试: 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 } 67C#中再调用测试: 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 } 95C#中调用测试: 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 } 114C#中调用测试: 115 116 int c=0; 117 int iSum= RefComm. mySum(,, ref c); 118 119 运行查看结果iSum 和c均为5,调用正确。 120 121 经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 C# 定义导入,有此基础,很快我实现了变长加密函数在 C# 中的调用,至此目标实现。 122 123 三、结论 124 125C# 中调用 C++ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用(ref)定义。 126 127 对于函数返回值,C# 导入定义和 C++ 动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意 CharSetCallingConvention 参数,否则导致调用失败或结果异常。运行时,动态链接库放在 C# 程序的目录下即可,我这里是一个 C# 的动态链接库,两个动态链接库就在同一个目录下运行。
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )