关于在dll中传递参数的问题(100分)

  • 主题发起人 主题发起人 han2000
  • 开始时间 开始时间
H

han2000

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:
我用delphi编了一个dll(projDLL)模块,然后与应用程序间创建了一个接口单元UnitFace
同时在dll中这样实现函数:
function GetIntFromBuf(ABuf:array of byte):integer;
begin
result := ABuf[0];
end;
在接口单元中对其进行声明,后在应用程序中这样调用
procedure TForm1.Button1Click(Sender: TObject);
var
iBuf :array[0..1] of byte;
iInt :integer;
begin
iBuf[0] :=1;
iBuf[1] :=2;
iInt :=GetIntFromBuf(iBuf);
edit1.text :=inttostr(iInt);
end;
结果出现一个地址错误。
我的dll模块的uses和应用程序的uses的第一个单元都引用了ShareMem.
那么,为何会出现这种错误呢?
究竟怎样在dll中通过参数传递数组类型的数据呢?
望各位高手不吝赐教。
我的email地址是cti_cao@263.net;
 
动态链接库连接调用使用TList传递数据比较好,所有的东西都可以添加到TList中,使用前
需要将Tlist创建,用完后删除
 
补充:
如果你的DLL中没有到String参数,也没必要用ShareMem了吧?
 
好象没有你的问题
代码如下:
dll
library tst;

{ 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 dllTmp(pBuf : array of byte ): integer ;stdcall ;export ;
begin
result := pBuf[0] ;
end ;

exports
dllTmp;

begin
end.

host:
unit tstdll;

interface

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

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

var
Form1: TForm1;

implementation
type
TmpDll = function(pBuf : array of byte ):integer ;stdcall;
{$R *.DFM}
var
tmpDll1 : TmpDll ;
h : integer ;

procedure TForm1.FormCreate(Sender: TObject);
begin
h := LoadLibrary( 'tst.dll' ) ;
tmpDll1 := GetProcAddress( h,'dllTmp' ) ;
if @tmpDll1 <> nil then showmessage('Ok') ;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
x : array[0..1] of byte ;
iRslt : integer ;
begin
//
x[0] := ord('a') ;
iRslt := tmpDll1( x ) ;
showmessage(intTostr(iRslt ) ) ;
end;

 
接受答案了.
 
后退
顶部