首先创建一个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. 02-创建String对象

    创建一个String对象实在是太简单了,就是因为简单,所以有很多java程序员做了好几年的开发,也没有注意这些小细节问题 String字符串的本质就是char数据对象, 那么char[0]数组当中的一 ...

  2. 如何使用webpack打包项目

    webpack是前端开发中比较常用的打包工具之一,另外还有gulp,grunt.之前没有涉及过打包这块,这里介绍一下使用webpack打包的流程. Grunt和Gulp的工作方式是:在一个配置文件中, ...

  3. CS229 6.5 Neurons Networks Implements of Sparse Autoencoder

    sparse autoencoder的一个实例练习,这个例子所要实现的内容大概如下:从给定的很多张自然图片中截取出大小为8*8的小patches图片共10000张,现在需要用sparse autoen ...

  4. flask基础知识

    关于flask框架的基础知识 相关基础知识:定义路由,定义参数,获取参数,重定向 简单易懂 ---hello.py # -*- coding: utf-8 -*- # Flask hello worl ...

  5. Cache: a place for concealment and safekeeping.Cache: 一个隐藏并保存数据的场所

    原文标题:Cache: a place for concealment and safekeeping 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限, ...

  6. Linux性能优化 第四章 性能工具:特定进程CPU

    4.1进程性能统计信息 4.1.1. 内核时间VS用户时间 一个应用程序所耗时间最基本的划分是内核时间与用户时间.内核时间是消耗在Linux内核上的时间,而用户时间则是消耗在应用程序或库代码上的时间. ...

  7. oracle SQL语句取本周本月本年的数据

    --国内从周一到周日 国外是周日到周六 select to_char(sysdate-1,'D') from dual;--取国内的星期几 去掉减一取国外的星期 --取本周时间内的数据 ,)+) an ...

  8. fastclick.js解决移动端(ipad)点击事件反应慢问题

    参考http://blog.csdn.net/xjun0812/article/details/64919063 http://www.jianshu.com/p/16d3e4f9b2a9 问题的发现 ...

  9. C# 反射常见用法

    定义: 反射是.NET中的重要机制,通过反射,可以在运行时获得程序或程序集中每一个类型(包括类.结构.委托.接口和枚举等)的成员和成员的信息.有了反射,即可对每一个类型了如指掌.另外我还可以直接创建对 ...

  10. Analysis of Web.xml in Hello1project

    一下是hello1  web inf 里的  web.xml <?xml version="1.0" encoding="UTF-8"?><w ...