首先创建一个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();
}
}
}

C#调用Delphi接口(ITest = interface)的更多相关文章

  1. C#调用Delphi的dll之详解

    C#调用Delphi接口方法,有两种解决办法: 一.将Delphi程序编译成一个COM组件,然后在C#里引用COM组件. 二.非托管调用Dephi的DLL文件. 这里我们主要讲解一下第二种方法,讲第二 ...

  2. delphi 接口Interface

    学习 delphi 接口 一切都是纸老虎!!! 第四章          接口 前不久,有位搞软件的朋友给我出了个谜语.谜面是“相亲”,让我猜一软件术语.我大约想了一分钟,猜 出谜底是“面向对象”.我 ...

  3. delphi调用https接口

    delphi调用http接口直接使用idhttp就可以了,但是调用https接口的时候就需要和IdSSLIOHandlerSocket1控件一起使用. 截图中是两个控件的具体配置,需要注意的是IdSS ...

  4. Delphi接口的底层实现(接口在内存中仍然有其布局,它依附在对象的内存空间中,有汇编解释)——接口的内存结构图,简单清楚,深刻 good

    引言 接口是面向对象程序语言中一个很重要的元素,它被描述为一组服务的集合,对于客户端来说,我们关心的只是提供的服务,而不必关心服务是如何实现的:对于服务端的类来说,如果它想实现某种服务,实现与该服务相 ...

  5. Delphi接口的底层实现

    引言 接口是面向对象程序语言中一个很重要的元素,它被描述为一组服务的集合,对于客户端来说,我们关心的只是提供的服务,而不必关心服务是如何实现的:对于服务端的类来说,如果它想实现某种服务,实现与该服务相 ...

  6. [Delphi]接口认识

    Delphi中的接口用 interface 进行声明.接口是针对行为方法的描述,而不管他实现这种行为方法的是对象还是别的什么东西.因此,接口和类的出发点是不一样的,是在不同的角度看问题. 接口通过GU ...

  7. Delphi 接口使用中,对象生命周期管理,如何释放需要注意的问题

    网上有篇文章<Delphi接口编程的两大陷阱>,里面提到接口的生存期管理的问题.但该文章里面提到的两个问题,其实都是对 Delphi 不理解导致的.   先说该篇文章中提到的第一个问题为什 ...

  8. 07.Delphi接口的生命周期

    在Delphi的接口中,是不需要释放的,调用完之后,接口的生命周期就结束了,如下面的例子 unit mtReaper; interface type // 定义一个接口 IBase = interfa ...

  9. C#动态调用WCF接口,两种方式任你选。

    写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...

随机推荐

  1. 小朋友学C语言(2):安装Dev C++编译器

    (一)编译器 编译器是将“一种语言(通常为高级语言)”翻译为“另一种语言(通常为低级语言)”的程序.一个现代编译器的主要工作流程:源代码 (source code) -->预处理器 (prepr ...

  2. elastalert新增自定义警告推送

    举例,博主公司有自己的内部通讯工具(类似QQ),接下来用IM代称该工具.于是希望elastalert的警告推送可以支持IM的公众号群发功能. 等博主这个月知识库写了再来补充hah

  3. day9笔记整理,记忆

    函数的使用:一 定义函数的三种形式 1.1 无参函数 1.2 有参函数 1.3 空函数二 调用函数的三种形式 2.1 语句形式    def foo():   print('from foo')    ...

  4. SQL server 数据库的数据完整性

    存储在数据库中的所有数据值均正确的状态.如果数据库中存储有不正确的数据值,则该数据库称为已丧失数据完整性. 详细释义 数据库中的数据是从外界输入的,而数据的输入由于种种原因,会发生输入无效或 错误信息 ...

  5. 关于text-align和text-align-last

    很多人都用过text-align,基本上也比较熟悉这个属性. text-align: left; // 左对齐 text-align: right; // 右对齐 text-align: center ...

  6. spring揭密学习笔记(1) --spring的由来

    1.spring起源于在EJB暴露出各种严重问题的情况应运而生. Spring是于2003年兴起的一个轻量级的Java开发框架, Spring倡导一切从实际出发,以实用的态度来选择适合当前开发场景的解 ...

  7. fabric-network_setup.sh安装脚本分析

    在进行镜像安装前,提供了一个sample脚本的下载,可以使用该脚本进行容器的启停.这里对该脚本进行分析. fabric/release/linux-amd64/network_setup.sh 脚本提 ...

  8. FasDfs缩略图解决方案 -- Linux

    前面研究了fastdfs的安装部署,并且做了多机同步. 这次我们解决下FastDFS做文件服务器并处理缩略图的问题. 有两个方案,方案1,在上传过程中生成多张图片,服务器存备.方案2,只上传一张图片, ...

  9. 10.Appium驱动app报错

    1.首先手机使用usb连接电脑端接受控制 2.打开appium,配置Desired Capabilities 参数: Appium驱动打开微信App 参数配置如下: start session 报错: ...

  10. Maven私服安装

    下载安装包:nexus(https://www.sonatype.com/download-oss-sonatype) 默认用户密码字符串: adminAdministratorUseractive& ...