• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#调用Delphi的dll详解

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

C#调用Delphi接口方法,有两种解决办法:   

一、将Delphi程序编译成一个COM组件,然后在C#里引用COM组件。

二、非托管调用Dephi的DLL文件。

 

这里我们主要讲解一下第二种方法,讲第二种方法之前首先讲解下DllImport。

     DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息。

     DllImport属性应用于方法,要求最少要提供包含入口点的dll的名称。

DllImport的定义如下:

 [AttributeUsage(AttributeTargets.Method)]

publicclass DllImportAttribute: System.Attribute
 {
         public DllImportAttribute(string dllName) {…} //定位参数为dllName
         public CallingConvention CallingConvention; //入口点调用约定
       public CharSet CharSet;                                   //入口点采用的字符接
       publicstring EntryPoint;                                  //入口点名称
       publicbool ExactSpelling;                               //是否必须与指示的入口点拼写完全一致,默认false
     publicbool PreserveSig;                                  //方法的签名是被保留还是被转换
       publicbool SetLastError; //FindLastError方法的返回值保存在这里
       publicstring Value { get {…} }
}
上面DLL的名字有时需要写上路径的如[DllImport(@"C:\OJ\Bin\Judge.dll")]这样指定DLL的绝对路径就可以正常装载。

      假如没有路径的话,DllImport会按照顺序自动去寻找的地方:
     1、exe所在目录
     2、System32目录
     3、环境变量目录      所以只需要你把引用的DLL 拷贝到这三个目录下, 就可以不用写路径了。

 

说明:       

1、DllImport只能放置在方法声明上。      

2、DllImport具有单个定位参数:指定包含被导入方法的 dll 名称的 dllName 参数。      

3、DllImport具有五个命名参数:           

  a、CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。           

  b、CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。          

  c、EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。           

  d、ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。         

  e、PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT返回值和该返回值的一个名为 retval 的附加

  输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。         

  f、SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。       

4、它是一次性属性类。       

5、此外,用 DllImport 属性修饰的方法必须具有 extern 修饰符。

 

下面代码样例

Delfhi DLL接口

接口方法1:

         方法原型:function  ShowCustScreen(sMsg: string = ''):Boolean; export;stdcall;

接口方法2:

         方法原型:function  CloseCustScreen:Boolean; export;stdcall;

接口方法3:

         方法原型:function UpBillLs(aBillData : array of string): boolean;  export;stdcall;

 

C# 调用

 

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport(@"C:\Users\ll\Desktop\Lottery\Lottery.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern Boolean ShowCustScreen(string sMsg);

        [DllImport(@"C:\Users\ll\Desktop\Lottery\Lottery.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern Boolean CloseCustScreen();

        [DllImport(@"C:\Users\ll\Desktop\Lottery\Lottery.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern Boolean UpBillLs(StringBuilder [] array);

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(ShowCustScreen("aa").ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(CloseCustScreen().ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(UpBillLs(new StringBuilder[] { new StringBuilder("1"), new StringBuilder("1"), new StringBuilder("1") }).ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}

 

  

 

 

 

 

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
UltraEdit支持delphi语法高亮发布时间:2022-07-18
下一篇:
Delphi中的钩子函数HOOK详解发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap