首先创建一个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. 关于jQuery点击事件叠加问题

    先来看个例子: html: <body> <button id="btn">按钮</button> <button id="bt ...

  2. Python工程化小结

    对如何写一个工业级的Python项目作一个top-down小结. 一.项目结构 顶层结构: 文件夹: model可以是项目中的自定义类: utils是一些工程工具,比如log,tracker log存 ...

  3. Linux下用node-inspector实现NodeJS远程调试开发

    1.首先安装 node-inspector npm install -g node-inspector -g表示全局安装,如果像我一样安装失败,再试几次,npm偶尔就会这样抽风... 这一步是关键的, ...

  4. 初级安全入门——XSS注入的原理与利用

    XSS的简单介绍 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets,CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者 ...

  5. [Unity插件]Lua行为树(十三):装饰节点完善

    之前介绍了组合节点中三大常用的节点:BTSequence.BTSelector和BTParallel,一般来说,这三种就够用了,可以满足很多的需求. 接下来可以完善一下装饰节点,增加几种新的节点. 1 ...

  6. [Unity插件]AI行为树使用总结

    参考链接: https://blog.csdn.net/linxinfa/article/details/72937709 https://blog.csdn.net/wanghaodiablo/ar ...

  7. 轻松解决oracle11g 空表不能exp导出的问题。

    解决方法: 1插入一条数据(或者再删除),浪费时间,有时几百张表会累死的.2创建数据库之前使用代码: Sql代码 alter system set  deferred_segment_creation ...

  8. Centos 7 下创建LVM流程

    https://www.cnblogs.com/ssslinppp/p/5853312.html

  9. xsync

    shell  小工具,用于集群搭建: xsync脚本基于rsync工具,rsync 远程同步工具,主要用于备份和镜像.具有速度快.避免复制相同内容和支持符号链接的优点,它只是拷贝文件不同的部分,因而减 ...

  10. JS 异步分段上传文件

    为了解决大文件上传 (PHP上传最大限制2GB) 同时为了解决文件上传是对服务器造成的压力 可以通过分段上传解决这个问题,这得益于HTML5开发的file API 前台代码: 引用了进度条插件myPr ...