Delphi调用C#类库

以前用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 BindingSource bindingSource1 = new BindingSource();

        private SqlDataAdapter dataAdapter = new SqlDataAdapter();

 

        public Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        /// 显示窗口

        /// </summary>

        public void ShchildFrm()

        {

            Form1 frm = new Form1();

            frm.Show();

        }

        /// <summary>

        /// 按钮事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)

        {

            dataGridView1.DataSource = bindingSource1;

            GetData("select * from Customers");

           

        }

        private void GetData(string selectCommand)

        {

            try

            {

         

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

                dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

 

                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

 

                DataTable table = new DataTable();

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

                dataAdapter.Fill(table);

                bindingSource1.DataSource = table;

                dataGridView1.AutoResizeColumns(

                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

 

            }

            catch (SqlException)

            {

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

                    "connectionString variable with a connection string that is " +

                    "valid for your system.");

            }

        }

    }

}

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

 

使程序集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: _Form1 read GetDefaultInterface;

 

五.Delphi 中使用

 

uses

 SFrm_TLB;

 

{$R *.dfm}

 

procedure TForm1.Button1Click(Sender: TObject);

var

Frm:TForm1;

begin

Frm:=TForm1.Create(self);

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

end;

 

delhi程序运行结果如下图:

 

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

posted @ 2008-06-25 23:01  hemingchen  阅读(8466)  评论(8编辑  收藏  举报