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. 从这 ...
随机推荐
- windows下安装、卸载mysql服务
将下载下来的mysql解压到指定目录下(如:d:\mysql)安装服务在命令行输入d:\mysql\bin\mysqld -installnet start mysql卸载服务在命令行输入net st ...
- spark streaming检查点使用
import org.apache.spark._ import org.apache.spark.streaming._ /** * Created by code-pc on 16/3/14. * ...
- Mysql 中获取刚插入的自增长id的值
insert into user (username,password) VALUES ('); //获取刚插入的自增长id的值 select last_insert_id(); 在MySQL中,使用 ...
- c语言数据类型(一)
(强数据类型) 1.常量 常量是程序中不可变的量 10为常量 两种常量 #define 定义宏常量 const #对于#define 类型的常量,c语言的习惯是常量名称大写, 对于普通const常量以 ...
- Apached+resin服务搭建
一.前言 Resin是CAUCHO公司的产品,是一个非常流行的支持servlets 和jsp的引擎,速度非常快.对servlet和JSP提供了良好的支持,性能也比较优良,resin自身采用JAVA语言 ...
- java第一次考试
这是我们开学的第一次Java课的考试,考的我有点害怕. 老师说这是给我们在正式上课之前提个醒,确实,我明白了我在学习方面还有多大的差距,确实,就如我高中同学所说的那样,没事就应该往机房跑了. 在上个学 ...
- 类似openDialog的弹窗
html <modal title="这里是标题" hidden="{{modalHidden}}" bindconfirm="modalCon ...
- spring boot使用配置文件内容
配置文件如下所示: server: port: 8081 context-path: /demo tag: 12 user: name1: mist-dev password: 123 然后可以通过以 ...
- 搭建eclipse开发环境
eclipse-jee配置 基本配置: 快捷查找:window->perferences->搜索框搜索 utf8: window->perferences->general-& ...
- std::ios_base::fmtflags orig std::streamsize prec