下面文件的内容:
unit Unit1;interface
function load_text(messages: string):string;stdcall;
implementation
function load_text(messages: string):string;
begin
load_text := messages + '中华人民共和国';
end;end.
///////////////////////////////////
一面是调用上面DLL文件的EXE文件内容:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//function load_text(messages: string):string;stdcall;external '..\dll_test\project1.dll';
var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
type tstrfunc = function (messages: string):string;stdcall;
var dll_file: Thandle;
load_text: Tstrfunc;
//var messages: string;
begin
dll_file := loadlibrary('..\dll_test\project1.dll');
if dll_file <> 0 then
load_text := getprocaddress(dll_file,'load_text');
showmessage(load_text('动态载入DLL'));
freelibrary(dll_file);{messages := load_text('动态载入DLL文件');
showmessage(messages);}end;
///////////////
运行后,先是出现SHOWMESSAGE 显示的内容,后来就是出现一个错误提示框,,,不知是那里错了,大家帮我找一下,行吗,谢谢了!!!
unit Unit1;interface
function load_text(messages: string):string;stdcall;
implementation
function load_text(messages: string):string;
begin
load_text := messages + '中华人民共和国';
end;end.
///////////////////////////////////
一面是调用上面DLL文件的EXE文件内容:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//function load_text(messages: string):string;stdcall;external '..\dll_test\project1.dll';
var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
type tstrfunc = function (messages: string):string;stdcall;
var dll_file: Thandle;
load_text: Tstrfunc;
//var messages: string;
begin
dll_file := loadlibrary('..\dll_test\project1.dll');
if dll_file <> 0 then
load_text := getprocaddress(dll_file,'load_text');
showmessage(load_text('动态载入DLL'));
freelibrary(dll_file);{messages := load_text('动态载入DLL文件');
showmessage(messages);}end;
///////////////
运行后,先是出现SHOWMESSAGE 显示的内容,后来就是出现一个错误提示框,,,不知是那里错了,大家帮我找一下,行吗,谢谢了!!!
implementation function load_text(messages: string):string;stdcall;
begin
load_text := messages + '中华人民共和国';
end;exports
load_text name 'loadtext';begin
end.
uses
SysUtils,
Classes,
Unit1 in 'Unit1.pas';{$R *.res}
exports
load_text;
begin
end.
///////////////////
对不起啊,我把dpr工程文件组忘了,上面是DPR文件的内容.
delphi自带的ShareMem有bug,要报错,实在要用去下载一个ShareMemRep来用!
我们都是传pchar!
我换成Pchar就没问题了。unit Unit1;interface
uses Dialogs;Function load_text(messages: string):pchar;stdcall;
implementation
Function load_text( messages: string):pchar;stdcall;
begin
result := pchar(messages + '中华人民共和国');
end;end.
////////////////////////////////////procedure TForm1.Button1Click(Sender: TObject);
type tstrfunc = Function (messages:string):pchar;stdcall;
var dll_file: Thandle;
load_text: Tstrfunc;
var messages: string;
begin
dll_file := loadlibrary('project1.dll');
if dll_file <> 0 then
load_text := getprocaddress(dll_file,'load_text');showmessage(load_text('动态载入DLL'));
freelibrary(dll_file);{messages := load_text('动态载入DLL文件');
showmessage(messages);}end;
在delphi里面String是一个特殊的数据类型,最好不要在Dll里面用
你在创建Dll工程文件的时候,应该在顶部一段被注释了的话吧
是这样的{ 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. }他告诉你用PChar或者ShortString来代替String.
在函数内返回值为String的没问题,当把函数做接口函数从DLL中发布出来时,将String改为Pchar