C#调用Delphi接口(ITest = interface)
首先创建一个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)的更多相关文章
- C#调用Delphi的dll之详解
C#调用Delphi接口方法,有两种解决办法: 一.将Delphi程序编译成一个COM组件,然后在C#里引用COM组件. 二.非托管调用Dephi的DLL文件. 这里我们主要讲解一下第二种方法,讲第二 ...
- delphi 接口Interface
学习 delphi 接口 一切都是纸老虎!!! 第四章 接口 前不久,有位搞软件的朋友给我出了个谜语.谜面是“相亲”,让我猜一软件术语.我大约想了一分钟,猜 出谜底是“面向对象”.我 ...
- delphi调用https接口
delphi调用http接口直接使用idhttp就可以了,但是调用https接口的时候就需要和IdSSLIOHandlerSocket1控件一起使用. 截图中是两个控件的具体配置,需要注意的是IdSS ...
- Delphi接口的底层实现(接口在内存中仍然有其布局,它依附在对象的内存空间中,有汇编解释)——接口的内存结构图,简单清楚,深刻 good
引言 接口是面向对象程序语言中一个很重要的元素,它被描述为一组服务的集合,对于客户端来说,我们关心的只是提供的服务,而不必关心服务是如何实现的:对于服务端的类来说,如果它想实现某种服务,实现与该服务相 ...
- Delphi接口的底层实现
引言 接口是面向对象程序语言中一个很重要的元素,它被描述为一组服务的集合,对于客户端来说,我们关心的只是提供的服务,而不必关心服务是如何实现的:对于服务端的类来说,如果它想实现某种服务,实现与该服务相 ...
- [Delphi]接口认识
Delphi中的接口用 interface 进行声明.接口是针对行为方法的描述,而不管他实现这种行为方法的是对象还是别的什么东西.因此,接口和类的出发点是不一样的,是在不同的角度看问题. 接口通过GU ...
- Delphi 接口使用中,对象生命周期管理,如何释放需要注意的问题
网上有篇文章<Delphi接口编程的两大陷阱>,里面提到接口的生存期管理的问题.但该文章里面提到的两个问题,其实都是对 Delphi 不理解导致的. 先说该篇文章中提到的第一个问题为什 ...
- 07.Delphi接口的生命周期
在Delphi的接口中,是不需要释放的,调用完之后,接口的生命周期就结束了,如下面的例子 unit mtReaper; interface type // 定义一个接口 IBase = interfa ...
- C#动态调用WCF接口,两种方式任你选。
写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...
随机推荐
- Java基础知识_毕向东_Java基础视频教程笔记(13 字符)
13天-01-String String类适用于描述字符串事物. 常见的操作:1.获取: 1.1字符串中包含的字符数,也就是字符串的长度. int length():获取长度 1.2根据索引值获取位置 ...
- 一、Html5基础讲解以及五个标签
什么是html?html是用来描述网页的一种语言html指超文本标记语言html不是编程语言,是一种标记语言 HTML基础标签 Head.body html标题 <h1>…<h6&g ...
- Java方法通过RestTemplate调用restful接口
背景:项目A需要在代码内部调用项目B的一个restful接口,该接口是POST方式,header中 Authorization为自定义内容,主要传输的内容封装在body中,所以使用到了RestTemp ...
- websocket如何设置?
根据之前写的步骤到当前的websocket设置 一.websocket设置: 1)在线程租下右键单击选择Sampler如图: 2)进行它的设置 2.保存 3.添加查看结果数和聚合报告(此处不详细说明, ...
- vmware centos7 动态ip->静态
TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_ ...
- [java,2017-12-01] 播放音频文件
废话不多说,直接上代码 jar包 <!-- 2017-12-01音频播放jar包 --> <dependency> <groupId>javazoom</gr ...
- ES6中新增字符串方法,字符串模板
多了两个新方法 startsWith endsWith 返回的是一个Boolean值 let str='git://www.baidu.com/2123123'; if(str.startsWith( ...
- <Linux> Ubuntu error: ssh: connect to host master port 22: No route to host lost connection
iptables当找到匹配的规则时,就会执行相应的动作,而不会向下继续匹配.因为ssh端口开放的规则在all规则之后,所以永远都不会匹配到,也就是ssh永远被禁止. root下执行:iptables ...
- uva-10720-贪心
题意:对于一个简单图(不存在平行边和自旋边),输入所有的点的度,问,能不能变成一个简单图. 解题思路: 可图化定理.https://blog.csdn.net/shuangde800/article/ ...
- VideoView播放视频——Android
Android为开发人员提供了一种简单的播放视频媒体的方式,那就是VideoView,本篇博客就来讲讲VideoView如何播放视频,最后将以一个简单的Demo演示. VideoView VideoV ...