DLL调用参数传递问题--(它怎么变了也不问我同不同意呢?)(难道没有DLL高手?!) (100分)

  • 主题发起人 主题发起人 leibin79
  • 开始时间 开始时间
L

leibin79

Unregistered / Unconfirmed
GUEST, unregistred user!
----------------------------------------------------------------
Main Form:
procedure TForm1.Button1Click(Sender: TObject);
var
Module : THandle;
t, s, f : integer;
Pfunc : function(i:integer;j:integer):integer;stdcall;
begin
s := 55;
f := 44;
Module := LoadLibrary(PChar(Edit3.Text));
@Pfunc := GetProcAddress(Module, 'add');
t := Pfunc(s, f);
ShowMessage(IntToStr(t));
FreeLibrary(Module);
end;

------------------------------------------------------
DLL File:(a part of)
function add(i:integer;j:integer):integer;export;
var
d : integer;
begin
d := i+j;
Result := d;
end;

{$R *.RES}

exports
add;
 
我也遇到类似的问题,你可以看一下在“数据库C/S“专题中我的问题
 
你是什么意思。什么变了。
 
这样调用:
Main Form:
interface
uses

function add(i:integer;j:integer):integer;external 'dllname.dll';

var FormMain:TFormMain;
implementation

procedure TForm1.Button1Click(Sender: TObject);
var
t, s, f : integer;
begin
s := 55;
f := 44;
t := add(s, f);
ShowMessage(IntToStr(t));
end;
 
问题在这里:

----------------------------------------------------------------
Main Form:
procedure TForm1.Button1Click(Sender: TObject);
var
Module : THandle;
t, s, f : integer;
// Pfunc : function(i:integer;j:integer):integer;stdcall;
Pfunc : function(i:integer;j:integer):integer; //stdcall;
如果调用DELPHI 写的DLL,函数的调用模式用Default 即register非stdcall



begin
s := 55;
f := 44;
Module := LoadLibrary(PChar(Edit3.Text));
@Pfunc := GetProcAddress(Module, 'add');
t := Pfunc(s, f);
ShowMessage(IntToStr(t));
FreeLibrary(Module);
end;
 
TO Jhdandcl and 左右手:
谢谢!
还有谁有其它方法吗?
或者发表一下意见--以上各种方法,实现起来,速度、对系统的要求等等参数的比较。
 
老兄:是动态调用,还是静态调用啊!你不说明白!没法说啊!
调用非三言两语可以说明白的! 我这一段看了看这方面的内容!
静态很容易:函数与过程/窗口调用,很简单的!不太繁]!
动态调用:就有点难度了!
咯loadlibrary('**.dll');返回thandle句柄
getprocaddress(上面返回句柄;'过程名/函数名');返回tfarproc句柄;然后
需对返回函数指针进行强制类型转换
此点,请老兄仔细一点!
然后,freelibrary(thandle);
 
stdcall 比较通用一些,可以为其它程序(非Delphi)调用,
Delphi默认的就只有Borland公司的产品认
 
TO Jhdandcl and all :
请问:register 调用 与 stdcall 有什么区别?还有其他的调用方式吗?
 
在DELPHI中函数的调用约定分为:
Register,pascal,cdecl,stdcall,safecall,
其主要是决定以下方面:
1.>参数传递的顺序
2.>参数如何Clear up
3.>如何使用寄存器传递参数
4>如何处理异常.
以下CUT FROM Delphi help :

When you declare a procedure or function, you can specify a calling convention
using one of the directives register, pascal, cdecl, stdcall, and safecall.
For example,

function MyFunction(X, Y: Real): Real; cdecl;

...

Calling conventions determine the order in which parameters are passed to
the routine. They also affect the removal of parameters from the stack, t
he use of registers for passing parameters, and error and exception handling.
The default calling convention is register.

The register and pascal conventions pass parameters from left to right;
that is, the leftmost parameter is evaluated and passed first
and the rightmost parameter is evaluated and passed last. The cdecl, stdcall,
and safecall conventions pass parameters from right to left.
For all conventions except cdecl,
the procedure or function removes parameters from the stack upon returning.
With the cdecl convention, the caller removes parameters from the stack when the call returns.

The register convention uses up to three CPU registers to pass parameters,
while the other conventions pass all parameters on the stack.
The safecall convention implements exception 揻irewalls.?On Windows,
this implements interprocess COM error notification.

The table below summarizes calling conventions.

Directive Parameter order Clean-up Passes parameters in registers?
register Left-to-right Routine Yes
pascal Left-to-right Routine No
cdecl Right-to-left Caller No
stdcall Right-to-left Routine No
safecall Right-to-left Routine No
The default register convention is the most efficient,
since it usually avoids creation of a stack frame.
(Access methods for published properties must use register.)
The cdecl convention is useful when you call functions from shared libraries
written in C or C++, while stdcall
and safecall are recommended, in general, for calls to external code.
On Windows, the operating system APIs are stdcall and safecall.
Other operating systems generally use cdecl. (Note that stdcall is more
efficient than cdecl.)

The safecall convention must be used for declaring dual-interface methods.
The pascal convention is maintained for backward compatibility.
For more information on calling conventions, see Program control.
The directives near, far, and export refer to calling conventions
in 16-bit Windows programming. They have no effect in 32-bit applications and
are maintained for backward compatibility only.

 
接受答案了.
 

Similar threads

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