DLL函数不适合返回 string 类型,应该用缓冲区,下面是用整数作例子:
/// unit1.pasunit Unit1;interface
function a(s:Integer):Integer; stdcall;implementationfunction a(s:Integer):Integer;
begin
result:=s;
end;end./// project1.dpr
library Project1;uses
SysUtils,
Classes,
Unit1 in 'Unit1.pas';{$R *.res}exports a;beginend.
/// unit1.pasunit Unit1;interface
function a(s:Integer):Integer; stdcall;implementationfunction a(s:Integer):Integer;
begin
result:=s;
end;end./// project1.dpr
library Project1;uses
SysUtils,
Classes,
Unit1 in 'Unit1.pas';{$R *.res}exports a;beginend.
解决方案 »
- 有在南京三益动力电子有限公司做过的朋友吗?
- 初学者~~~~如何删除文件中指定的行
- 请问如何编写一个可以在html页面中通过script调用的对象,类似FlashGet中浏览器右键菜单中的下载全部链接的功能
- delphi +sqlserver 2000怎样备份数据库和恢复数据库????
- 调用DLL错误
- 一个关于ClientSocket的问题
- 怎么知道某个网络协议是否安装,比如IPX或TCP/IP协议!!
- 请问:delphi5嵌汇编的程序能运行在WIN2000下?
- ADOTable控件连接数据库,使用DBGRID控件,当记录移动到最后一个,再向下移动就出错??
- 老千,你在吗?偶用wise已经初装好了,不过,还有一个不明白的地方:偶想生成一个readme文件在安装文件里,不知道应该在哪里添加?另外安装文件里有一个 installl.txt 文件,他是什么?//牛虻
- 为什么我把下面这段代码复制到自己新建的delphi项目里老出错?
- 质询两个PageControl1的问题!
1、New->DLL;取名为DLL_0001,编写代码:
library dll_0001;
uses
SysUtils,
Classes;
{$R *.RES}
function max(x,y:integer):integer;stdcall;
begin
if(x>y) then
max :=x
else
max :=y;
end;
exports max;
begin
end.2、Delphl调用dll
调用dll分动态调用和静态调用2中,动态调用写起来简单,安全点,动态调用复杂很多,但很灵活;
(1)new一个Application,在Form中放入2个TEdit、1个TLabek;
(2)静态调用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function max(x,y:integer):integer;stdcall;
external 'dll_0001.dll';
procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key =vk_return then
label1.Caption :=IntToStr(max(StrToInt(Edit1.text),StrToInt(edit2.text)));
end;
end.
(3)动态调用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
type
TFunc =function(x,y:integer):integer;stdcall;
var
Th:Thandle;
Tf:TFunc;
Tp:TFarProc;
begin
if key =vk_return then
begin
Th :=LoadLibrary('dll_0001.dll'); {load dll}
if(Th >0) then
try
Tp :=GetProcAddress(Th,PChar('max'));
if(Tp <>nil) then
begin { begin 1}
Tf :=TFunc(Tp);
Label1.Caption :=IntToStr(
Tf(StrToInt(Edit1.text),StrToInt(Edit2.text)));
end { end 1}
else
ShowMessage('function max not found.');
finally
FreeLibrary(Th);
end
else
ShowMessage('dll_0001.dll not exsit.');end;
end;
end.
http://www.cnblogs.com/del/category/113550.html