您好,为何DLL句柄可以找到函数地址却是空的 ? 代码如内 ,谢谢(20分)

  • 主题发起人 主题发起人 驿路的梦
  • 开始时间 开始时间
驿

驿路的梦

Unregistered / Unconfirmed
GUEST, unregistred user!
您好,如下 DLL :
-------------------------------
library MyOne;
{...}
uses
SysUtils,
Classes;

{$R *.res}

Function One( i : Integer) : Integer ; stdcall ;
begin
Result := I + 100 ;
end;

Exports
One ;
begin
end.
----------------------------
静态调用没有错 ,可在动态调用时,LoadLibrary 可以取得Dll句柄,GetProcAddress却不能
得到函数地址 ,调用如下 :
unit Unit1;
interface
uses
...
type
TFuncOne = Function (I : Integer) : Integer ;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
...
end ;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
mh : THandle ;
TF : TFarProc ;
begin
mh := LoadLibrary(Pchar('D:/Dll/MyOne.dll')) ; // mh 得到
if mh <> 0 then
begin
TF := GetProcAddress(mh,Pchar('one')) ; // TF = Nil
ShowMessage(IntToStr(TFuncOne(TF)(0))) ;
end
else
begin
ShowMessage('The Path Wrong') ;
end;
FreeLibrary(mh) ;
end;
end.

请问为什么会有这种情况 ?
还有,在DLL中是否可以操作数据库 ? 是否可以自定义控件 ? 也就是说,DLL
可以使用在哪些场合 ?

谢谢您
 
@TF := GetProcAddress(mh,Pchar('one'))
 
谢谢您,看到过直接用的例子,不过改了后还是不对
if mh <> 0 then
begin
@TF := GetProcAddress(mh,Pchar('one')) ;
ShowMessage(IntToStr(TFuncOne(TF)(0))) ;
编译提示 :
[Error] Unit1.pas(38): Left side cannot be assigned to

哪儿错了 ? 应该怎么改正? 谢谢
 
@TF := GetProcAddress(mh,Pchar('one')) ;
ShowMessage(IntToStr(TF(0))) ;
 
procedure TForm1.Button1Click(Sender: TObject);
var
mh : THandle ;
TF : TFuncOne ; //不是TFarProc
 
我这样改了也不对
var
PF : TFuncOne ;
begin
mh := LoadLibrary(Pchar('F:/changshiF/DLL/动态调用/MyOne.dll')) ;
if mh <> 0 then
begin
@PF := GetProcAddress(mh,Pchar('one')) ;
ShowMessage(IntToStr(TFuncOne(@PF)(0))) ;
end

 
你把DLL发给我, 我看看 tseug@263.net
 
张无忌 : 您好,
@TF := GetProcAddress(mh,Pchar('one'))
编译提示 :
[Error] Unit1.pas(38): Left side cannot be assigned to
-------------------------------------------------------------
tseug 您好,DLL 很短的 :
------------------------
library MyOne;
{...}
uses
SysUtils,
Classes;

{$R *.res}

Function One( i : Integer) : Integer ; stdcall ;
begin
Result := I + 100 ;
end;

Exports
One ;
begin
end.
-----------------

我按您说得改一下看看 ,谢谢您
 
TFuncOne(@PF)(0))) ???、
直接PF(0)就可以了,为什么要TFuncOne???
 
Faint.
'One'<>'one',DLL中函数名是大小写敏感的!
 
procedure TForm1.Button1Click(Sender: TObject);
var
pf : TFuncOne ;
begin
mh := LoadLibrary(Pchar('F:/changshiF/DLL/动态调用/MyOne.dll')) ;
if mh <> 0 then
begin
Pf := GetProcAddress(mh,'one') ;
ShowMessage(IntToStr(TFuncOne(pf)(0))) ;
end;
也不对 。

:张无忌, : 我也不知道:),书上的,我想是不是指针强制类型转换 ?


 
楼上说得对....不过编译错误不是这个原因....
 
哪个指针就是TFuncOne 为什么要转换?
 
改为 'One' ,大小写一致时可以ShowMessage(),但结果不对,应该为 100,但实际是
134... 几个数字,相当于乱码 ,而且ShowMessage()后还出错 。

但我静态调用是对的 。 为什么 ?

谢谢您
 
To : 张无忌
哪个指针就是TFuncOne 为什么要转换?
------------------------------------------
照您这样说就可以不定义那个 TFuncOne ,但书上都是这样写的,都定义的 。

不明白为什么
 
TFuncOne = Function (I : Integer) : Integer ;
改为:
TFuncOne = Function (I : Integer) : Integer ;stdcall;
这样参数传递的顺序才与约定的一致.否则默认为Register调用方式.
 
procedure TForm1.Button1Click(Sender: TObject);
var
pf : TFuncOne ;
mh : THandle ;
TF : TFarProc ;
begin
mh := LoadLibrary(Pchar('MyOne.dll')) ;
if mh <> 0 then
begin
a: @Pf := GetProcAddress(mh,'One') ;
ShowMessage(IntToStr(TFuncOne(@pf)(0))) ;

b: @PF := GetProcAddress(mh,Pchar('One')) ;
ShowMessage(IntToStr(TFuncOne(@PF)(0))) ;

c: TF := GetProcAddress(mh,Pchar('One')) ;
ShowMessage(IntToStr(TFuncOne(TF)(0))) ;
end ;

a,b,c 三组的ShowMessage()竟然不同,a,b两组应该完全相同的呀,为什么 ? Delphi怀掉了??
 
ok,谢谢您,我试试
 
hehe,Delphi哪那么容易坏掉.你的参数根本没传递进去,传进了的是寄存器里
的一个随机值,就因为你没采用约定的调用方式.
 
unit ttt;

interface

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

type
TFuncOne = Function (I : Integer) : Integer; [red]stdcall;[/red] //注意这个

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
pf : TFuncOne ;
mh : THandle;
begin
mh := LoadLibrary(PChar('TTTD.dll')) ;
if mh <> 0 then
begin
@Pf := GetProcAddress(mh,'One') ;
ShowMessage(IntToStr(pf(0))) ;
FreeLibrary(mh);
end;
end;

end.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
666
import
I
后退
顶部