C#调用Delphi接口(ITest = interface)
来源:互联网 发布:java String添加方法 编辑:程序博客网 时间:2024/04/29 08:39
首先创建一个delphi的DLL工程
最后在c#中调用
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;beginend.
接下来声明并实现接口
unit uintf;interfaceuses 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;implementationprocedure 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(); } }}
0 0
- C#调用Delphi接口(ITest = interface)
- C#调用delphi dll接口问题
- Delphi Interface接口的定义
- C#接口Interface
- C#接口Interface
- C#接口Interface
- C#接口Interface
- C# interface 接口理解
- C# 接口使用 (interface)
- C#之接口(Interface)
- c# 接口interface 入门
- c# 接口 interface
- C# interface 接口理解
- C# Interface 接口
- C# 接口 interface
- C# 接口(Interface)
- C# 接口(Interface)
- C# interface(接口)
- PullToRefreshListView的使用
- 【浅析】JAVA内部类的实现及反编译解析
- 安装laravel后,如何运行 php artisan key:generate 这个命令
- 通过重建控制文件的方法恢复数据库
- Jquery实现多级联动
- C#调用Delphi接口(ITest = interface)
- 用C#保存IOS发送的图片
- java 字节流和字符流 【转自一直在等博客】
- iOS开发之地图定位
- JDK 安装与环境变量配置
- VMware EXSI 为虚拟机centos安装VMware Tools
- 多线程(6)等待唤醒机制Lock,condition
- STM32 基于串口RS485双机通信原理浅析
- 按钮 图片调整到右边 文字到左边