Delphi調用C#類庫

從網上找的,感覺有幫助就收藏起來。。。

 一、打開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;

delphi程序運行結果如下圖:

注:在程序運行環境必須安裝.net環境並註冊dll文件 否則會報:無註冊類別

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章