考虑如下几种常用情况:

- VC传入int,返回int
- VC传入char *,返回int
- VC传入char *,返回char *及int

为简化问题,传递的字符串参数只考虑ANSI格式,不考虑UNICODE。
如果需要UNICODE,可以自行使用字符串格式转换函数。

Delphi 2007的代码如下:

library DemoDll;

uses
SysUtils, Classes, Windows, StrUtils; {$R *.res} // Write to log file
procedure AddLog(const AFormat: string; Args: array of const); overload;
var
LogFile: TextFile;
FileName: string;
begin
FileName := GetEnvironmentVariable('TEMP') + '\DemoDll.log';
try
AssignFile(LogFile, FileName); if FileExists(FileName) then
Append(LogFile)
else
ReWrite(LogFile); Writeln(LogFile, Format('[%s] ', [DateTimeToStr(Now)]) + Format(AFormat, Args));
finally
CloseFile(LogFile);
end;
end; // Write to log file
procedure AddLog(const AFormat: string); overload;
begin
AddLog(AFormat, []);
end; // return a+b
function TestAdd(a, b: Integer): Integer; stdcall;
begin
AddLog('TestAdd(%d,%d)', [a, b]);
Result := a + b;
end; // do nothing, just log input string
function TestInputStr(PInStr: PChar): Integer; stdcall;
var
str: string;
begin
str := StrPas(PInStr);
AddLog('TestInputStr(%s)', [str]);
Result := 0;
end; // reverse first string and write to second string
function TestOutputStr(PInStr, POutStr: PChar): Integer; stdcall;
var
str: string;
begin
str := StrPas(PInStr);
str := ReverseString(str);
StrCopy(POutStr, PChar(str));
AddLog('TestOutputStr(%s)', [str]);
Result := 0;
end; exports
TestAdd,
TestInputStr,
TestOutputStr; begin
AddLog('BEGIN');
end.

  

VC2013的代码如下:

// Put DemoDll.dll in the same dir
// DemoDll.dll is developed by Delphi #include "windows.h"
#include <iostream> typedef int(__stdcall *fTestAdd)(int, int);
typedef int(__stdcall *fTestInputStr)(char *);
typedef int(__stdcall *fTestOutputStr)(char *, char *); using namespace std; int main(int argc, char* argv[])
{
HINSTANCE hDllLibrary = NULL; fTestAdd TestAdd = NULL;
fTestInputStr TestInputStr = NULL;
fTestOutputStr TestOutputStr = NULL; hDllLibrary = LoadLibraryA("DemoDll.dll");
if (hDllLibrary){
TestAdd = (fTestAdd)GetProcAddress(hDllLibrary, "TestAdd");
TestInputStr = (fTestInputStr)GetProcAddress(hDllLibrary, "TestInputStr");
TestOutputStr = (fTestOutputStr)GetProcAddress(hDllLibrary, "TestOutputStr"); // check if a == 3
int a = TestAdd(1, 2);
cout << "TestAdd(1,2)=" << a << endl; // check if input string has output to log file
char b[] = "nothing";
TestInputStr(b);
cout << "TestInputStr(" << b << ")" << endl; // check if output string is reversed
char c[] = "nothing";
char d[256] = { 0 };
TestOutputStr(c, d);
cout << "TestOutputStr(" << c << ")=" << d << endl;
} getchar();
return 0;
} // Console output:
//
// TestAdd(1, 2) = 3
// TestInputStr(nothing)
// TestOutputStr(nothing) = gnihton
//
// %Temp%/DemoDll.log
//[2017 / 6 / 4 14:48 : 46] BEGIN
//[2017 / 6 / 4 14:48 : 46] TestAdd(1, 2)
//[2017 / 6 / 4 14:48 : 46] TestInputStr(nothing)
//[2017 / 6 / 4 14:48 : 46] TestOutputStr(gnihton)

  

[笔记]Delphi 2007写DLL供VC调用实例的更多相关文章

  1. 用IKVMC将jar转成dll供c#调用

    用IKVMC将jar转成dll供c#调用 ikvmc c# dll jar 用IKVMC将jar转成dll供c#调用 前言 ikvmc介绍 ikvmc下载安装 下载并解压 设置环境变量 jar-> ...

  2. Delphi 中的DLL 封装和调用对象技术(刘艺,有截图)

    Delphi 中的DLL 封装和调用对象技术本文刊登2003 年10 月份出版的Dr.Dobb's 软件研发第3 期刘 艺摘 要DLL 是一种应用最为广泛的动态链接技术但是由于在DLL 中封装和调用对 ...

  3. Go 程序编译成 DLL 供 C# 调用。

    Go 程序编译成 DLL 供 C# 调用. C# 结合 Golang 开发   1. 实现方式与语法形式 基本方式:将 Go 程序编译成 DLL 供 C# 调用. 1.1 Go代码 注意:代码中 ex ...

  4. Delphi XE3写DLL,用Delphi7调用,报错!

    http://bbs.csdn.net/topics/390870532 用delphi xe3写的DLL,delphi7调用,参数都是PAnsiChar,DLL里的函数接收delphi7传的入参,没 ...

  5. Qt532_QWebView做成DLL供VC/Delphi使用_Bug

    Qt5.3.2 vs2010 OpenGL ,VC6.0,Delphi7 1.自己继承 类QWebView,制作成DLL 供 VC6/Delphi7 使用 2.测试下来,DLL供VC6使用: 加载&q ...

  6. c++builder调用VC的dll以及VC调用c++builder的dll

    解析__cdecl,__fastcall, __stdcall 的不同:在函数调用过程中,会使用堆栈,这三个表示不同的堆栈调用方式和释放方式. 比如说__cdecl,它是标准的c方法的堆栈调用方式,就 ...

  7. Delphi编写DLL供C#调用的实例

    Delphi中编写的Dll: library TestDLL; { Important note about DLL memory management: ShareMem must be the f ...

  8. 可供VC调用的QT编写的界面DLL方法

    一般直接编写的QT动态库是无法被Windows下的VC6.0等调用的. 如下步骤 第一步:必须要在QT界面库源码下包含qtwinmigrate的源码包和库,网上可下载到. 第二步:在QT的proc文件 ...

  9. Matlab函数编译成dll供c调用

    一 编译dll 在Command Window窗口中输入mbuild -setup,然后会出现语句,是否安装编译器,选择n,因为机子上已经安装了C/C++/C#的编译器,选择VS2010.

随机推荐

  1. C++继承具体解释之二——派生类成员函数具体解释(函数隐藏、构造函数与兼容覆盖规则)

    在这一篇文章開始之前.我先解决一个问题. 在上一篇C++继承详解之中的一个--初探继承中,我提到了在派生类中能够定义一个与基类成员函数同名的函数,这样派生类中的函数就会覆盖掉基类的成员函数. 在谭浩强 ...

  2. android classloader双亲托付模式

    概述 ClassLoader的双亲托付模式:classloader 按级别分为三个级别:最上级 : bootstrap classLoader(根类载入器) : 中间级:extension class ...

  3. 经验总结20--C#模拟WEB请求

    非常多语言能够使用代码进行WEB请求,获取到须要的数据. 方便调用别人的接口,自己进行处理. HttpWebRequest request = WebRequest.Create(url) as Ht ...

  4. HDU 5306 Gorgeous Sequence[线段树区间最值操作]

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  5. 【BZOJ4883】[Lydsy2017年5月月赛]棋盘上的守卫 KM算法

    [BZOJ4883][Lydsy2017年5月月赛]棋盘上的守卫 Description 在一个n*m的棋盘上要放置若干个守卫.对于n行来说,每行必须恰好放置一个横向守卫:同理对于m列来说,每列 必须 ...

  6. Elasticsearch 监控插件安装(elasticsearch-head与Kibana)

    摘要 安装Elasticsearch插件Head与Kibana 版本 elasticsearch版本: elasticsearch-2.3.4 elasticsearch-head版本: 2.x(支持 ...

  7. Storm-源码分析- metric

    首先定义一系列metric相关的interface, IMetric, IReducer, ICombiner (backtype.storm.metric.api) 在task中, 创建一系列bui ...

  8. Android studio 使用技巧和问题

    最近更新Android studio版本到1.2.1.1后 出现了一些问题,首先一个就是创建一个项目后,布局文件会提示 找不到类. 网上找了下答案,原来是这个版本的bug. 其实解决起来很简单,找到 ...

  9. SqlServer SqlBulkCopy批量插入 -- 多张表同时插入(事务)

    这段时间在解决一个多个表需要同时插入大量数据的问题,于是在网上找了下,查到说用SqlBulkCopy效率很高,实验后确实很快,10万条数据只要4秒钟,用ef要用40秒.但是我的还需两张表同时插入,且需 ...

  10. Flask之视图函数

    视图示例 @app.route('/hello') def hello(): return 'Hello World' if __name__ == '__main__': app.run() 特殊的 ...