考虑如下几种常用情况:

- 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. 根据List<SqlParameter>返回sql条件(where后)

    /// <summary> /// 根据参数列表返回sql条件(where后) /// </summary> /// <param name="list&quo ...

  2. JQuery------实现点击左右按钮,切换图片功能

    如图: 代码: html @*商品主图片*@ <div class="product-img" style="display:table-cell;width:40 ...

  3. 编程之美 set 16 拈游戏分析(1)

    题目 N 个石头排成一行, 每块石头有固定的位置和编号, 两个玩家依次取石头, 每个玩家可以取其中的任一块石头, 或者相邻的两个石头. 石头在游戏过程中不能移位, 最后将剩下的石头依次取光的玩家获胜 ...

  4. aar

    aar是一个类似于jar的文件格式.但是他们之间是有区别的.jar:仅仅包含class和清单文件,没有资源文件.aar:包含了class文件和资源文件.说白了就是Android的专属“jar” 将代码 ...

  5. vue2.0非父子间进行通讯

    在vue中,父组件向之组件通讯使用的是props,子组件向父组件通讯使用的是$emit+事件,那非父子间的通讯呢,在官方文档上只有寥寥数笔, 概念很模糊,这个空的vue实例应该放在哪里呢,光放文档并没 ...

  6. keepalive和脑裂问题

    keepalive keepalive起初专门为lvs负载均衡软件设计的,用来管理监控lvs集群系统中各个服务节点的状态,后来又加入了可以实现高可用的vrrp功能. keepalive软件通过vrrp ...

  7. post 与get

    GET:从服务器上获取数据,也就是所谓的查,仅仅是获取服务器资源,不进行修改. POST:向服务器提交数据,这就涉及到了数据的更新,也就是更改服务器的数据. 补充: PUT:PUT的英文含义是放置,也 ...

  8. LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  9. debian卸载旧内核

    debian卸载旧内核要先看看有哪些旧的内核,用命令: uname -a dpkg --get-selections |grep linux 如果你的内核是以kernel开头的就把上面的linux改成 ...

  10. CodeForces 651 A Joysticks

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...