qq502984975 发表于 2013-5-16 23:44:02

delphi7 dll动态调用时遇到的问题求解释!还有给点建议

本帖最后由 qq502984975 于 2013-5-16 23:48 编辑


源码如下:
library mydll;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes;

{$R *.res}
function test(x,y:Integer):Integer ; stdcall;
begin

Result:=x+y;
end;
exports
test;

begin
end.

------------------------------------------------------------------------------------------------------------------------------------
unit dll;

interface

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

type
TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    edt1: TEdit;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;
type
       Tdlltest=function (x,y:Integer):Integer;stdcall;
var
Form1: TForm1;
implementation

{$R *.dfm}

//function test(x,y:Integer):Integer;stdcall;external'mydll.dll';
procedure TForm1.btn1Click(Sender: TObject);
var
dllHandle:THandle;
test:TdllTest;
begin
//ShowMessage('111111');
dllHandle:=LoadLibrary('mydll.dll');
GetProcAddress(dllHandle,'test');
   edt1.Text:=IntToStr(test(2,2));
    FreeLibrary(dllHandle);
{n:=test(3,9) ;
ShowMessage(IntToStr(n)); }
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
Close;
end;

end.
----------------------------------------------------------------------------------------------------------------------------------------------------

qq502984975 发表于 2013-5-17 01:25:46

我靠,问题自己解决了。
原因是直接引用了函数内存,应该用指针的方式指向函数内存。
如果我误解了,请大侠给个纠正,菜鸟一个请别见笑。;P
页: [1]
查看完整版本: delphi7 dll动态调用时遇到的问题求解释!还有给点建议