惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - Sephil

[小技巧] 倍数的向上取整和向下取整 WTL汉化版 DelphiXE泛型不能用类类型做为约束的另类解决方案 百度音乐下载工具 (最后更新: 2012-7-20) 为Delphi应用增加脚本支持 插件框架Extensible Framework for Delphi DirectUI for Delphi VC CListCtrl 第一列列宽自适应 WriteFile写磁盘扇区是87错误的原因 批量更改文件名的批处理文件 替代Windows运行功能的工具FastRun 关于 API 中返回字串的一些问题 BCB/Delphi2007 隐藏任务栏图标 OGA & WGA Crack 所有小工具 迅雷/快车/旋风地址转换器 oracle ora-01033和ora-00600错误 数独游戏 SudokuPuzzle 将文件夹映射为驱动器的工具
Delphi 与 VC 共享接口和对象
Sephil · 2012-11-18 · via 博客园 - Sephil

我经常会用 Delphi 写一些工具和应用,为了扩展方便,大部分都会做成插件形式。

迫于某些原因,我的插件不得不用其他开发工具来完成,比如 VC。

于是有个大问题需要解决:如何让 D 和 VC 互相通信、互相操作。

最普遍的做法,无非是定义一些方法,VC 写 Dll 导出这些方法,D 载入 Dll 调用。

但问题是稍大点规模的应用,这种方式非常麻烦,也不够直观。

于是花了点时间研究 D 和 VC 之间共享接口和对象的一些方法,现将要点共享发布出来,希望对大家有用。

基础事项:

在 D 和 VC 中,要共享的接口、对象中的方法的调用约定必须为 stdcall

1. class 的 使用

Delphi 和 VC 中都使用抽象类,将方法都定义为纯虚方法,成员的声明顺序请保持一致。

需要注意的是 Delphi 的类方法。一般的静态类方法在 VC 中直接跳过即可,虚的类方法在 VC 定义为一般的虚函数即可。

D:

  TTestObj = class
  public
    class procedure Foo;
    procedure Update(Intf: ITestIntf); virtualstdcallabstract;
    procedure Free; virtualstdcallabstract;
  end;

VC:

class ITestObj
{
public:
    virtual void __stdcall Update(ITestIntf* pIntf) = 0;
    virtual void __stdcall Free() = 0;
};

2. 接口

D 的 IInterface / IUnknown,在 VC 中定义为 interface /*class*/ : public IUnknown,成员的声明顺序请保持一致

注意,D 中接口支持属性定义,但是 VC 不支持,因此 D 接口中的属性定义请放在声明的最后 

如果接口的实例化是在 VC 中,有点有意思的小细节要注意,详见后面下载的代码里的注释

D:

  ITestIntf = interface
  ['{781E6521-8768-4ADA-B843-445ECE548C27}']
    function  GetText: PAnsiChar; stdcall;
    procedure SetText(AValue: PAnsiChar); stdcall;

    function  GetValue: Integer; stdcall;
    procedure SetValue(AValue: Integer); stdcall;

    property Text: PAnsiChar read GetText write SetText;
    property Value: Integer read GetValue write SetValue;
  end;

VC:

interface DECLSPEC_UUID("781E6521-8768-4ADA-B843-445ECE548C27")
ITestIntf : public IUnknown
{
public:
    virtual LPCSTR __stdcall GetText() = 0
    virtual void __stdcall SetText(LPCSTR lpszMsg) = 0

    virtual int __stdcall GetValue() = 0
    virtual void __stdcall SetValue(int value) = 0
};

示例代码中,在 D 里实现了一个接口提供给 VC DLL,在 VC DLL 里实现了一个接口和一个类提供给 D 里使用。

代码下载