zoukankan      html  css  js  c++  java
  • Delphi调用C#类库.doc

    一、打开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文件 否则会报:无注册类别

  • 相关阅读:
    IDEA右键新建时没有Java Class选项
    捕获摄像头视频VC
    重叠IO与IOCP
    (八)内存管理与内存分配
    DebugView使用详解
    (六) 中断机制
    (五) proc文件系统
    bash 之备份文件
    bash 遍历目录文件
    (四) linux内核模块编程
  • 原文地址:https://www.cnblogs.com/chenghm2003/p/1147442.html
Copyright © 2011-2022 走看看