考虑如下几种常用情况:

- 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. 【Python】python-memory-management

    http://deeplearning.net/software/theano/tutorial/python-memory-management.html#python-memory-managem ...

  2. Object和Function谁先被创建

    http://bbs.csdn.net/topics/390772104#post-397284029

  3. leetcode 153: Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. iOS 7 SDK: 如何使用后台获取(Background Fetch)

    本文转载至 http://www.cocoachina.com/applenews/devnews/2013/1114/7350.html 本文主要教你如何使用iOS 7 SDK多任务处理API--B ...

  5. 一些VS2013的使用技巧(转载)

    1. Peek View 可以在不新建TAB的情况下快速查看.编辑一个函数的代码. 用法:在光标移至某个函数下,按下alt+F12. 然后在Peek窗口里可以继续按alt+F12.然后按ctrl+al ...

  6. 获取 js DOM元素中绑定的所有事件,模仿 chrome getEventListeners

    偶尔看到了这个问题,如何用JS获取元素某一事件上绑定的所有Listener? 突然觉得好像是有解决办法的,查了下,在 chrome 下,支持 window.getEventListeners(obj) ...

  7. 170406、用uid分库,uname(用户名)上的查询怎么办

    [缘起] 用户中心是几乎每一个公司必备的基础服务,用户注册.登录.信息查询与修改都离不开用户中心. 当数据量越来越大时,需要多用户中心进行水平切分.最常见的水平切分方式,按照uid取模分库: 通过ui ...

  8. style、currentStyle、getComputedStyle(不同浏览器获取css样式)区别介绍

    style.currentStyle.getComputedStyle区别介绍   样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效. 内部样 ...

  9. hibernate缓存,四种状态

    FlushMode.AUTO:Hibernate判断对象属性有没有改变,是默认的清理模式 FlushMode.COMMIT:在事务结束之前清理Session的缓存,其他任何时候都不清理缓存 Flush ...

  10. win10系统安装Oracle11报错不满足最低要求

    以下问题是,在安装Oracle过程中遇到的问题以及自己的解决方法: 问题一:下载好两个Oracle的安装压缩包文件,同时解压后,启动setup.exe,报错:[INS-13001]环境不满足最低要求. ...