Dll 的参数传递问题。帮帮忙!(100分)

  • 主题发起人 主题发起人 沧海
  • 开始时间 开始时间

沧海

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么我的返回值是错的!
请各位高手指点
代码:
unit TestUnit;

interface

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

type
  TFunc = function (MyInt:integer):integer 

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

var
  Form1: TForm1;

implementation
//  function ShowDllHello(ftStr:string):integer;stdcall;External'Dll_Hello.dll';

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoDllFunc;
end;

procedure TForm1.DoDllFunc;
var
  MyFunc:TFunc;
  MyHandle:THandle;
begin
  MyHandle:=LoadLibrary('Dll_Hello.dll');
  if MyHandle>32 then
  begin
    @MyFunc:=GetProcAddress(MyHandle,'ShowDllHello');
    if @MyFunc<>nil then
    begin
      showmessage(IntToStr(MyFunc(10)))
   //////////想不通,返回 为什么错!
    end;
    FreeLibrary(MyHandle);
  end;
end;

end.

library Dll_Hello;

uses
  SysUtils,Classes;

{$R *.RES}

function ShowDllHello(HelloStr:integer):integer;stdcall;
begin
  Result:=HelloStr * 99;
end;

exports
  ShowDllHello name 'ShowDllHello' 

begin
end.
【、】
 
帮帮忙撒
 
把参数值类型该为shortstring或是pchar试试.
string是delphi的数据类型
dll要求windows的数据类型
 
四库全书说的是对的。
Integer 是Delphi 的类型,不是Windows的 DLL 要求的类型
你把你的 integer 改成 Int64 都可以。。
 
先试一下,
TFunc = function (MyInt:integer):integer Stdcall;
 
procedure TForm1.DoDllFunc;
var
MyFunc:TFunc;
MyHandle:THandle;
begin
后边加上 MyFunc := TFunc.Create;
 
不会是参数的问题,catchan 说的可能是对的,你先试一下,应该能行
 
就是上面说的那个原因:
测试通过:
unit Unit1;

interface

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

type
TFunc = function (MyInt:integer):integer Stdcall;
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
MyFunc:TFunc;
MyHandle:THandle;
begin
MyHandle:=LoadLibrary('Dll_Hello.dll');
if MyHandle>32 then
begin
@MyFunc:=GetProcAddress(MyHandle,'ShowDllHello');
if @MyFunc<>nil then
begin
showmessage(IntToStr(MyFunc(10)))
//想不通,返回 为什么错!
end;
FreeLibrary(MyHandle);
end;
end;
end.
 

@MyFunc:=GetProcAddress(MyHandle,'ShowDllHello');
if @MyFunc<>nil then
以上的 "@" 需要吗?

另外函数声明 stdcall ,已有人提到!
 
catchan, 老兄的方法是对的!

可是 俺 想不通! 那位高手可以指点一下 。

我会按时结束的。 多谢大家了。
 
function (MyInt:integer):integer 这个函数与DLL中的function ShowDllHello(HelloStr:integer):integer;stdcall;
肯定应该完全一样的啊。当然包括参数传递的方式了。
showmessage(IntToStr(MyFunc(10)));这里出错。但你如果直接调用ShowDllHello(HelloStr:integer):integer;如:
showmessage(IntToStr(ShowDllHello(10)));就没有问题(不信你试试),因为在声明中已经声明为Stdcall,所以就和DLL
中一样了。自己看看参数传递方式吧:register,stdcall,savecall,pascal,cdecl!
 
调用约定指示字 参数传递顺序 由谁释放 是否在寄存器里传递参数
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
 
小弟生性愚钝。
那位大侠再解释详细些好吗?
 
DELPHI 中的数据类型写的DLL函数用DELPHI 来调用就可以使用delphi 本身的数据类型
没什么问题啊
在你的DLL uses引用的单元中加上sharemem,一定要加在第一个
然后在调用的工程文件单元(不是窗体单元) 中uses 的所有单元前加上sharemem
只要你的函数没问题应该就可以啦
 
没人理我只好结束了!没人对我的分配有意见把!
 
后退
顶部