[笔记]Delphi 2007写DLL供VC调用实例
考虑如下几种常用情况:
- 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调用实例的更多相关文章
- 用IKVMC将jar转成dll供c#调用
用IKVMC将jar转成dll供c#调用 ikvmc c# dll jar 用IKVMC将jar转成dll供c#调用 前言 ikvmc介绍 ikvmc下载安装 下载并解压 设置环境变量 jar-> ...
- Delphi 中的DLL 封装和调用对象技术(刘艺,有截图)
Delphi 中的DLL 封装和调用对象技术本文刊登2003 年10 月份出版的Dr.Dobb's 软件研发第3 期刘 艺摘 要DLL 是一种应用最为广泛的动态链接技术但是由于在DLL 中封装和调用对 ...
- Go 程序编译成 DLL 供 C# 调用。
Go 程序编译成 DLL 供 C# 调用. C# 结合 Golang 开发 1. 实现方式与语法形式 基本方式:将 Go 程序编译成 DLL 供 C# 调用. 1.1 Go代码 注意:代码中 ex ...
- Delphi XE3写DLL,用Delphi7调用,报错!
http://bbs.csdn.net/topics/390870532 用delphi xe3写的DLL,delphi7调用,参数都是PAnsiChar,DLL里的函数接收delphi7传的入参,没 ...
- Qt532_QWebView做成DLL供VC/Delphi使用_Bug
Qt5.3.2 vs2010 OpenGL ,VC6.0,Delphi7 1.自己继承 类QWebView,制作成DLL 供 VC6/Delphi7 使用 2.测试下来,DLL供VC6使用: 加载&q ...
- c++builder调用VC的dll以及VC调用c++builder的dll
解析__cdecl,__fastcall, __stdcall 的不同:在函数调用过程中,会使用堆栈,这三个表示不同的堆栈调用方式和释放方式. 比如说__cdecl,它是标准的c方法的堆栈调用方式,就 ...
- Delphi编写DLL供C#调用的实例
Delphi中编写的Dll: library TestDLL; { Important note about DLL memory management: ShareMem must be the f ...
- 可供VC调用的QT编写的界面DLL方法
一般直接编写的QT动态库是无法被Windows下的VC6.0等调用的. 如下步骤 第一步:必须要在QT界面库源码下包含qtwinmigrate的源码包和库,网上可下载到. 第二步:在QT的proc文件 ...
- Matlab函数编译成dll供c调用
一 编译dll 在Command Window窗口中输入mbuild -setup,然后会出现语句,是否安装编译器,选择n,因为机子上已经安装了C/C++/C#的编译器,选择VS2010.
随机推荐
- Ubuntu Mysql 安装
下载 http://dev.mysql.com/downloads/mysql/ 选择 Linux- Generic 选择版本 wget http://cdn.mysql.com/Downloads/ ...
- lumen 常用辅助函数
optional 函数接收任意参数并允许你访问对象上的属性或调用其方法.如果给定的对象为空,属性或方法调用返回 null return optional($user->address)-> ...
- 运维角度浅谈:MySQL数据库优化
日志君导读: 一个成熟的数据库架构并非一開始设计就具备高可用.高伸缩等特性的.它是随着用户量的添加,基础架构才逐渐完好. 作者:zhenliang8.本文转自51CTO博客,点击原文阅读查看网页版文章 ...
- WPF进阶之接口(2):IDisposable,ICollectionView
废话不多说,进入正题,先来说说IDisposable,看例子(来自MSDN): using System; using System.ComponentModel; // 下面的例子将展示一个实施了I ...
- 第三篇:C++ 中的几种初始化
前言 阅读C++教材时,想必你听过复制初始化,直接初始化,值初始化这三个概念吧.笔者本人常将其混淆,遂在此记录下它们的具体含义以便日后查阅. 复制初始化( copy-initialization ) ...
- IOS7开发~新UI学起(四)
本文转载至 http://blog.csdn.net/lizhongfu2013/article/details/9166193 1.UITableView: UITableViewDelegate ...
- 虚拟机VMWare安装苹果系统MacOS详细教程(联网设置,全屏插件、文件互传)
运行环境: VMware® Workstation 12 Pro(自行安装,或者用这个) 推荐(下面以10.11.6版本做的教程,但是安装时推荐使用此版本安装然后升级到10.11.6):MacOS X ...
- Java中Integer类的方法和request的setAttribute方法的使用与理解
一.Integer类的使用方法 Interger:整数类型 1.属性. static int MAX_VALUE:返回最大的整型数: static int MIN_VALUE:返回最小的整型数: st ...
- java中的 final 关键字 修饰引用时的问题
final使得被修饰的变量”不变”,但是由于对象型变量的本质是“引用”,使得“不变”也有了两种含义:引用本身的不变,和引用指向的对象不变. 引用本身的不变: final StringBuffer a= ...
- JUnit4 测试示例
1. JUnit4 测试示例 // Calculator.java public class Calculator{ public int add(int a, int b){ return a + ...