C#调用Delphi接口(ITest = interface)

本文介绍如何使用Delphi创建一个包含接口的DLL,并通过C#进行调用。具体步骤包括:定义DLL工程及接口实现,然后在C#中声明并使用该接口。此方法适用于需要在不同编程语言间共享代码的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先创建一个delphi的DLL工程
library testintfdll;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  uintf in 'uintf.pas';

{$R *.res}

exports
  GetImpl;

begin
end.

接下来声明并实现接口

unit uintf;

interface

uses Windows;

type
  ITest = interface
    ['{000A0299-A27A-4D35-9721-419AE6E83869}']
    procedure ShowMessage(msg: PAnsiChar); stdcall;
    function Add(a, b: Integer): Integer; stdcall;
  end;

  TTest = class(TInterfacedObject, ITest)
  protected
    procedure ShowMessage(msg: PAnsiChar); stdcall;
    function Add(a, b: Integer): Integer; stdcall;
  end;

procedure GetImpl(out instance: ITest); stdcall;

implementation

procedure GetImpl(out instance: ITest);
begin
  instance := TTest.Create;
end;

{ TTest }

function TTest.Add(a, b: Integer): Integer;
begin
  Result := a + b;
end;

procedure TTest.ShowMessage(msg: PAnsiChar);
begin
  MessageBox(0, msg, 'title', ID_OK);
end;

end.

最后在c#中调用

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CShapeTestClient
{
    [ComVisible(true)]
    [ComImport, Guid("000A0299-A27A-4D35-9721-419AE6E83869"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface ITest
    {
        [MethodImplAttribute(MethodImplOptions.PreserveSig)]
        void ShowMessage([MarshalAs(UnmanagedType.AnsiBStr)] string msg);
        [MethodImplAttribute(MethodImplOptions.PreserveSig)]
        int Add(int a, int b);
    }
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        [DllImport("testintfdll.dll", EntryPoint = "GetImpl",CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        private static extern void GetImpl([MarshalAs(UnmanagedType.Interface)] out ITest instance);

        private void button1_Click(object sender, EventArgs e)
        {
            ITest tester;
            GetImpl(out tester);
            tester.ShowMessage(textBox1.Text);
            this.Text = tester.Add(100, 50).ToString();
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值