鱼C论坛

 找回密码
 立即注册
查看: 2864|回复: 1

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

[复制链接]
发表于 2013-5-16 23:44:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

QQ图片20130516234221.jpg QQ截图20130516234000.png
源码如下:
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.
----------------------------------------------------------------------------------------------------------------------------------------------------
QQ图片20130516234221.jpg
QQ截图20130516234000.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2013-5-17 01:25:46 | 显示全部楼层
我靠,问题自己解决了。
原因是直接引用了函数内存,应该用指针的方式指向函数内存。
如果我误解了,请大侠给个纠正,菜鸟一个请别见笑。;P
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-22 03:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表