//MyInt.pas
unit MyInt; interface {$IFNDEF MYLIB}
function MyAdd(a,b:integer):integer ;stdcall;
{$ENDIF} implementation {$IFNDEF MYLIB}
function MyAdd; external 'MyLib.dll' name 'MyAdd';
{$ENDIF}
end.
//MyLib.dpr
library MyLib; { Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. } uses
SysUtils,
Classes,
MyInt in 'MyInt.pas'; {$R *.res}
function MyAdd(a,b:integer):Integer ; stdcall;
begin
result := (a + b);
end;
exports
MyAdd;
end.

 //使用:

unit UnitMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
btnTestDll: TButton;
btnDynamicCall: TButton;
procedure btnTestDllClick(Sender: TObject);
procedure btnDynamicCallClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TFuncMyAdd = function (a,b:Integer) : integer;stdcall; var
Form1: TForm1; implementation
uses
MyInt;
{$R *.dfm} procedure TForm1.btnTestDllClick(Sender: TObject);
begin
ShowMessageFmt('结果=%d',[MyAdd(1,2)]);
end; procedure TForm1.btnDynamicCallClick(Sender: TObject);
var
libHandle : THandle;
funcAdd : TFuncMyAdd;
begin
libHandle := LoadLibrary('MyLib.dll');
try
if libHandle = 0 then
begin
//raise error;
ShowMessage('加载DLL失败!');
end;
@funcAdd := GetProcAddress(libHandle,'MyAdd');
if not (@funcAdd = nil) then
begin
ShowMessageFmt('动态加载结果=%d',[funcAdd(1,2)]);
end;
finally
FreeLibrary(libHandle)
end;
end;
end.

  

Delphi Dll示例的更多相关文章

  1. C# 调用Delphi dll

    delphi dll 源码: library dllres; type char10 = ..] of char; TMydata = packed record id: Integer; name: ...

  2. 在.net中调用Delphi dll的Pchar转换

    Pchar是非托管代码,要在.net中调用Delphi dll中的功能,请使用MarshalAs属性告知.net调用PInvoke去转换.net中标准的string类型.如果Delphi dll是De ...

  3. Delphi Dll 动态调用例子(3)-仔细看一下

    http://blog.163.com/bxf_0011/blog/static/35420330200952075114318/ Delphi 动态链接库的动态和静态调用 为了让人能快速的理解 静态 ...

  4. Borland.Delphi.dll

    Borland.Delphi.dll Borland Delphi Runtime for .NET Imports Borland.DelphiImports Borland.Delphi.Unit ...

  5. vb6如何调用delphi DLL中的函数并返回字符串?

    1,问题描述 最近发现vb6调用delphi DLL中的函数并返回字符串时出现问题,有时正常,有时出现?号,有时干脆导致VB程序退出 -- :: 将金额数字转化为可读的语音文字:1转化为1元 ???? ...

  6. delphi dll调用问题

    dll传递string实现方法 delphi中dll传递string的实现方法: dll项目uses第一个引用sharemem单元; 调用的项目uses第一个引用sharemem单元; 调用的单元us ...

  7. Delphi静态加载DLL和动态加载DLL示例

    下面以Delphi调用触摸屏动态库xtkutility.dll为例子,说明如何静态加载DLL和动态加载DLL. 直接上代码. 1.静态加载示例 unit Unit1; interface uses W ...

  8. Delphi调用C++写的dll示例

    最近做一个读市民卡的项目,读卡器公司提供的读市民卡dll是用C++写的. 下面记录一些自己的心得,供需要的朋友参考. 声明dll函数要加上stdcall关键字,否则可能会报地址非法的错误. 代码: u ...

  9. C#调用Delphi Dll返回字符串的示例(使用Move才能拷贝字符串)

    //----------------------Delphi------------------- procedure GetSqlData(ASource: PChar; ADest: PChar; ...

随机推荐

  1. LCS(滚动数组) POJ 1159 Palindrome

    题目传送门 题意:一个字符串要变成回文串至少要插入多少个字符 分析:LCS,长度 - 原串和反串的最大相同长度就是要插入的个数.解释一下,当和反串相同时,在原串中已经是回文的部分了,那么减去LCS长度 ...

  2. SCU3185 Black and white(二分图最大点权独立集)

    题目大概说有几个黑色.白色矩阵,问能选出黑白不相交的矩形面积和的最大值. 建二分图,黑色矩阵为X部的点,白色为Y部,XY的点权都为其矩阵面积,如果有个黑白矩阵相交则它们之间有一条边,那样问题就是要从这 ...

  3. BZOJ3641 : 货车运输

    若一条边的v小于等于u,则贡献为l*w/v,否则贡献为l*w/u 将边按v从小到大排序,将询问按u从小到大排序 用树链剖分维护链上和,val[0]表示第一种情况下的贡献,val[1]表示第二种情况下的 ...

  4. java Android SDK安装与环境变量配置以及开发第一个Android程序

    JAVA的安装与环境变量的配置 1.先下载JAVA,并且安装. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u3 ...

  5. POJ 3335 Rotating Scoreboard(多边形的核)

    题目链接 我看的这里:http://www.cnblogs.com/ka200812/archive/2012/01/20/2328316.html 然后整理一下当做模版.0换成eps,会wa,应该要 ...

  6. ueditor使用总结——前端篇.md

    首先吐槽下,百度的API太坑,谁让人家都是大牛呢. 说明:本文所涉猎代码均依托于seajs,本文所用ueditor版本为1.4.3.3 JSP UTF-8版 编辑器前端部署: 1.把ueditor引入 ...

  7. Ajax注册验证用户名是否存在 ——引自百度经验

    Ajax注册验证用户名是否存在 http://jingyan.baidu.com/article/a948d6515fdf870a2dcd2e85.html

  8. 网站地图sitemap.xml的格式

    URL列表—XML格式及规范说明: 标签名称  属性  标签说明  标签类型  标签限制  可选/必选  urlset  / urlset用来标记整个文档的开头  /  /  必选  url  / u ...

  9. iOS学习—JSON数据解析

      关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSer ...

  10. ArcEngine 异常:field is not editable

    字段不可编辑. Access数据库默认第一个字段为ID字段,不可修改.所以,在新建字段时,第一个字段为ObjectID字段,如果没有建立该字段,则把另外的字段作为 不可修改的ID字段,造成field ...