打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Delphi调用C#类库 - chenghm2003 - 博客园

以前用delphi写的CS程序 今天客户要加几个模块上去 ,刚好会点C#,这后面模块就用C#写的编译成dll文件, 在用delphi调用C#写的dll文件时折腾好阵子 就有了这些经历写下来。

 一、打开vs2005

 新建windows应用程序项目命名为SFrm,删除应用程序自动生成的Program.cs

(因为我们是要生成dll文件)

在窗体类新建一接口(interface SHFRM) 让窗体类实现接口 代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace SFrm

{

    public interface SHFRM  //此接口用在delphi下调用必须用到

    {

        void ShchildFrm();

    }

 

    public partial class Form1 : Form,SHFRM

    {

        private BindingSourcebindingSource1 = new BindingSource();

        private SqlDataAdapter dataAdapter =new SqlDataAdapter();

 

        public Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 显示窗口

        /// </summary>

        public void ShchildFrm()

        {

            Form1 frm = new Form1();

            frm.Show();

        }

        /// <summary>

        /// 按钮事件

        /// </summary>

        /// <paramname="sender"></param>

        /// <paramname="e"></param>

        private void button1_Click(objectsender, EventArgs e)

        {

            dataGridView1.DataSource =bindingSource1;

            GetData("select * fromCustomers");

           

        }

        private void GetData(stringselectCommand)

        {

            try

            {

         

                String connectionString ="Data Source=.;initial catalog=Northwind;user id =sa;pwd=";

                dataAdapter = newSqlDataAdapter(selectCommand, connectionString);

 

                SqlCommandBuildercommandBuilder = new SqlCommandBuilder(dataAdapter);

 

                DataTable table = newDataTable();

                table.Locale =System.Globalization.CultureInfo.InvariantCulture;

                dataAdapter.Fill(table);

                bindingSource1.DataSource =table;

               dataGridView1.AutoResizeColumns(

                   DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

 

            }

            catch (SqlException)

            {

                MessageBox.Show("To runthis example, replace the value of the " +

                    "connectionStringvariable with a connection string that is " +

                    "valid for yoursystem.");

            }

        }

    }

}

右击项目名在属性对话框中更改输出类型为”类库” 在界面点击程序集信息 按钮 如下图:

 

使程序集com可见必须选中

完成dll文件生成

 

 

二.DotNet 类库打包成COM类型库(在vs命令行执行如下操作)

Tlbexp.exe SFrm.dll /out: SFrm.tlb

三.注册COM类型库

Regasm.exe SFrm.dll

四.Delphi导入类型库

Delpi 中, Project -> Import Type Library,选中类型库:dotnet2com.tlb,

生成 DotNet2Com_TLB 单元文件。单元文件中有接口 SHFRM。

  SHFRM = interface(IDispatch)

   ['{D8400C54-E4B2-36BD-B970-45CD204F319A}']

    procedure ShchildFrm; safecall;

  end;和代理类声明 TForm1及获得 SHFRM 接口的属性:

      property DefaultInterface: _Form1read GetDefaultInterface;

 

五.Delphi 中使用

 

uses

 SFrm_TLB;

 

{$R *.dfm}

 

procedure TForm1.Button1Click(Sender:TObject);

var

Frm:TForm1;

begin

Frm:=TForm1.Create(self);

   (Frm.DefaultInterface asSHFRM).ShchildFrm();//显示dll文件里窗体

end;

 

delhi程序运行结果如下图:

 

注:在程序运行环境必须安装。net环境并注册dll文件 否则会报:无注册类别

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
delphi如何调用C#的dll
Delphi捕捉DLL执行所抛出的异常。
Delphi中导出TeeChart图像方法汇总(代码示例)
怎样在VB中声明和使用API函数
Delphi实现HTMLWebBrowser实现HTML界面
com程序编写入门(全文-1)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服