列位师哥:
我刚刚接触DELPHI的DLL,看了一些资料但是还是很不明白。
我写了一个很简单的DLL,如下:
library test_dll;
uses
SysUtils,
Dialogs,
Classes;function c_sum(r:integer):integer;stdcall;
begin
result:=r*3;
end;{$R *.res}
exports
c_sum;
begin
end.并在主程序里调用
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private { Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function c_sum(r:integer):integer;stdcall;external'test_dll.dll';procedure TForm1.Button1Click(Sender: TObject);
var
s:integer;
begin
s:=strtoint(edit1.text);
edit2.Text:=inttostr(c_sum(s));
end;end.
这里用的是静态调用一切OK。(当edit1.text为3时,edit2.Text显示9)可是换成动态调用
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
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
s:integer;
t_handle:Thandle;
test_sum:function(r:integer):integer;
begin
s:=strtoint(edit1.text); t_handle:=loadlibrary('test_dll.dll');
if t_handle<32 then
begin
application.MessageBox('动态连接库掉失!','提示',64);
exit;
end
else begin
@test_sum:=Getprocaddress(t_handle,MakeIntResource(1));
edit2.Text:=inttostr(test_sum(s));
FreeLibrary(t_handle);
end;
end;end.
却发生了错误,(当edit1.text为3时,edit2.Text居然显示3731172)
这是为何呢?
还有就是动态调用中
为何@test_sum:=Getprocaddress(t_handle,MakeIntResource(1));
Getprocaddress函数不能根据句柄t_handle,直接取函数名吗?一定要取
函数序号?
请各位师哥多赐教,先谢过了.
我刚刚接触DELPHI的DLL,看了一些资料但是还是很不明白。
我写了一个很简单的DLL,如下:
library test_dll;
uses
SysUtils,
Dialogs,
Classes;function c_sum(r:integer):integer;stdcall;
begin
result:=r*3;
end;{$R *.res}
exports
c_sum;
begin
end.并在主程序里调用
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private { Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function c_sum(r:integer):integer;stdcall;external'test_dll.dll';procedure TForm1.Button1Click(Sender: TObject);
var
s:integer;
begin
s:=strtoint(edit1.text);
edit2.Text:=inttostr(c_sum(s));
end;end.
这里用的是静态调用一切OK。(当edit1.text为3时,edit2.Text显示9)可是换成动态调用
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
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
s:integer;
t_handle:Thandle;
test_sum:function(r:integer):integer;
begin
s:=strtoint(edit1.text); t_handle:=loadlibrary('test_dll.dll');
if t_handle<32 then
begin
application.MessageBox('动态连接库掉失!','提示',64);
exit;
end
else begin
@test_sum:=Getprocaddress(t_handle,MakeIntResource(1));
edit2.Text:=inttostr(test_sum(s));
FreeLibrary(t_handle);
end;
end;end.
却发生了错误,(当edit1.text为3时,edit2.Text居然显示3731172)
这是为何呢?
还有就是动态调用中
为何@test_sum:=Getprocaddress(t_handle,MakeIntResource(1));
Getprocaddress函数不能根据句柄t_handle,直接取函数名吗?一定要取
函数序号?
请各位师哥多赐教,先谢过了.
解决方案 »
- 疑問
- WPS office 2007非常不错,建议大家使用。与Office 2003完全兼容。个人免费。
- 在多线程中打开clientdataset时如何传参数?
- 如何控制pagecontrol页面
- 软件调用windows下的tracert仅能看到ping多少,我现在想得到丢包率多少应如何处理呢?
- 在修改别人的程序,但老是提示一个错误,selected collating sequence not supported by the operating system
- 如何在helphi实现特殊效果
- 算法问题研究+请教!
- 痛何如哉???!!!
- 在DELPHI中有没有象JAVA的静态类的概念?
- 问一个很容易的算法问题,有四个数a,b,c,d..........
- 关于treeview的问题?
LPCSTR lpProcName // name of function
);
ParametershModuleIdentifies the DLL module that contains the function. The LoadLibrary or GetModuleHandle
function returns this handle. lpProcNamePoints to a null-terminated string containing the function name, or specifies the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero.
GetProcAddress(DLL_Handle, '函数名称')
让我知道此用法。
不过问题依旧,
动态调用仍然出错即(当edit1.text为3时,edit2.Text居然显示3731172)
为何呢?
谢谢两位的答案问题解决了。马上结帐,并请到小M的另一个贴子上再指教谢谢。
http://expert.csdn.net/Expert/topic/1775/1775482.xml?temp=7.564723E-03