帮我看一下,那儿出错呀???关于动态链接库的???(100分)

  • 主题发起人 主题发起人 Imfish
  • 开始时间 开始时间
I

Imfish

Unregistered / Unconfirmed
GUEST, unregistred user!
MyDll:
library MyDll;
uses
SysUtils,
Classes,
Base in 'Base.pas';

{$R *.RES}
const
MyDoubl = 'MyDoubl';
MyTripl = 'MyTripl';
exports
Doubl index 1 name MyDoubl resident ,Tripl index 2 name MyTripl resident

begin
end.

Base单元:
unit Base;

interface
function Doubl(N:integer):integer;stdcall;
function Tripl(N:integer):integer;stdcall;
implementation
uses
windows;

function Doubl(N:integer):integer;stdcall;
begin
MessageBox(0,'加倍!','MyDll',mb_ok);
Result:=N*2;
end;
function Tripl(N:integer):integer;stdcall;
begin
MessageBox(0,'三倍!','MyDll',mb_ok);
Result:=N*3;
end;
end.

调用的:

unit UDll;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Spin;


type
TDoubl = function (N:integer):integer;
TTripl = function (N:integer):integer;
TForm1 = class(TForm)
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
procedure SpinEdit1Change(Sender: TObject);
procedure SpinEdit2Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//function MyDoubl(N:integer):integer;stdcall;external'mydll.dll';
//function MyTripl(N:integer):integer;stdcall;external'mydll.dll';
var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.SpinEdit1Change(Sender: TObject);
var
tmpint:integer;
Moudle:THandle;
PFunc:TDoubl;
begin
Moudle:= loadlibrary('MyDll.dll');
if Moudle > 32 then
begin
PFunc:=GetProcAddress(Moudle,'MyDoubl');
tmpint:=PFunc(spinedit1.value);//就是这儿和我过不去,能出来MessageBox的对话框,但是,点了确定后就不行了出错。
label1.caption := inttostr(tmpint);
end;
Freelibrary(Moudle);
//tmpint:=MyDoubl(spinedit1.value);
//label1.Caption:=inttostr(tmpint);
end;
procedure TForm1.SpinEdit2Change(Sender: TObject);
var
tmpint:integer;
Moudle:THandle;
PFunc:TFarProc;
begin
Moudle:= loadlibrary('MyDll.dll');
if Moudle > 32 then
begin
PFunc:=GetProcAddress(Moudle,'MyTripl');
tmpint:=TTripl(PFunc)(spinedit2.value);
label2.caption := inttostr(tmpint);
end;
Freelibrary(Moudle);
//tmpint:=MyTripl(spinedit2.value);
//label2.Caption:=inttostr(tmpint);
end;
end.
 
是不是我给的分太少了呀,大家兴趣不是很高呀,谁回答了我再加一百分!
豁出去了[:D]
 
PFunc:=GetProcAddress(Moudle,'MyDoubl');<<
应该是 @PFunc:=GetProcAddress(Moudle,'MyDoubl');
 

var
tmpint:integer;
Moudle:THandle;
PFunc:function (N:integer):integer stdcall
//声明必须是stdcall
i:integer;
begin
Moudle:= loadlibrary('MyDll.dll');
if Moudle > 32 then
begin
@PFunc:=GetProcAddress(Moudle,'MyDoubl')
//原来不对
i:=self.SpinEdit1.Value;
tmpint:=PFunc(i);//就是这儿和我过不去,能出来MessageBox的对话框,但是,点了确定后就不行了出错。
label1.caption := inttostr(tmpint);
end;
Freelibrary(Moudle);
//tmpint:=MyDoubl(spinedit1.value);
//label1.Caption:=int+tostr(tmpint);
end;
 
TO 52Free
不行呀,我试了,下面是我开始用的
procedure TForm1.SpinEdit1Change(Sender: TObject);
var
tmpint:integer;
Moudle:THandle;
PFunc:TFarProc;
begin
Moudle:= loadlibrary('MyDll.dll');
if Moudle > 32 then
begin
PFunc:=GetProcAddress(Moudle,'MyDoubl');
tmpint:=TDoubl(PFunc)(spinedit1.value);//就是这儿和我过不去,能出来MessageBox的对话框,但是,点了确定后就不行了出错。
label1.caption := inttostr(tmpint);
end;
Freelibrary(Moudle);
 
向下面这样应该没什么问题
procedure TForm1.SpinEdit1Change(Sender: TObject);
var
tmpint:integer;
Moudle:THandle;

PFunc:TFarProc;
begin
Moudle:= safeloadlibrary('MyDll.dll');
if Moudle > 0 then
try
begin
PFunc:=GetProcAddress(Moudle,pchar('MyDoubl'));
tmpint:=TDoubl(PFunc)(spinedit1.value);<<此处的spinedit1.value是整数值吗?你确定一下
label1.caption := inttostr(tmpint);
end;
finally
Freelibrary(Moudle);
end;
 
搞定,NND这个小问题搞了我半天[:(]
不是
PFunc:=GetProcAddress(Moudle,'MyDoubl');的问题;这句是对的
就是声明处少了stdcall
谢谢两位
 
后退
顶部