+ -
当前位置:首页 → 问答吧 → delphi调用vc dll问题

delphi调用vc dll问题

时间:2011-12-13

来源:互联网

我想在delphi里调用一个用vc编译的dll里的一个函数,动态的。

我对这个封装好了的dll的所知全部来自一个read me.txt文件,内容如下:

bool hiddata(BYTE* pWriteData, BYTE* pReadData ) ;

返回true成功 返回false失败

pWriteData指针为要下发的数据缓冲区指针,Dll会发送这个指针指向的8个字节数据。

pReadData指针为接收USB IC的数据缓冲区指针,Dll会把接收的8个字节数据放到指针指向的数据缓冲区中。注意,该缓冲区由应用程序初始化空间。Dll不检查指针的有效性。



好了,对dll我就只知道这么多了。现在我的代码如下,很简答的一个程序:


unit Unit1;

interface

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

type
  p_byte = ^Byte;
   
type
  TForm1 = class(TForm)
  Panel1: TPanel;
  Panel2: TPanel;
  Panel3: TPanel;
  Button1: TButton;
  Memo1: TMemo;
  procedure Button1Click(Sender: TObject);
  private
  { Private declarations }
  public
  { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
  //Taddc=function(pWriteData,pReadData:p_byte):Boolean;stdcall;
  Taddc=function(pWriteData,pReadData:p_byte):Boolean;cdecl;
var
  H: THandle;
  addc:Taddc;
  i,j:byte;
begin
  H:=LoadLibrary('Byte8HID.dll');
   
  if h<>0 then
  begin
  @addc:=GetProcAddress(h,'hiddata');
  if @addc<>nil then
  begin
  ShowMessage('not null');
  i:=26;
  j:=26; //随便初始化下。
  try
  begin
  if addc(@i,@j) then
  begin
  ShowMessage('apply successfully');
  memo1.Lines.Add(inttostr(Integer(@j)));
  end
  else
  begin
  ShowMessage(IntToStr(GetLastError));
  end;
  end;
  except on e:exception do
  begin
  Memo1.Lines.Add(e.Message);
  end;
  end;
  end
  else
  begin
  ShowMessage('it''s null');
  ShowMessage(IntToStr(GetLastError));
  end;
  end;

  FreeLibrary(H);
end;

end.

现在我遇到的问题就是,做到ShowMessage('not null');这步之后,程序就完全没反应了,怎么点都不响应。估计是一直在内存里读啊读的,但是不知道读的是什么地址的……

有哪位大侠可以告诉我,我的问题到底出在哪儿?我总觉得是我什么小地方没注意到,做错了。跪求指点。

作者: blue_afreet   发布时间: 2011-12-13

function hiddata(pWriteData : Pchar;pReadData :Pchar):boolean;stdcall;Stdcall;external 'XXX.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
 sc1 : array[0..10] of char;
 sc2 : array[0..10] of char;
begin
 hiddata(sc1,sc2);
end;

作者: napsoft   发布时间: 2011-12-13

function hiddata(pWriteData : Pbyte;pReadData :Pbyte):boolean;stdcall;Stdcall;external 'XXX.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
 sc1 : byte;
 sc2 : byte;
begin
 hiddata(sc1,sc2);
end;


没有测试,LZ参考下。如果出错,见意将byte分配内存空间

作者: napsoft   发布时间: 2011-12-13

var
 sc1 : array[0..100] of byte;
 sc2 : array[0..100] of byte;
begin
 hiddata(sc1,sc2);
end;

作者: napsoft   发布时间: 2011-12-13

楼上OK,Byte*对应的是DELPHI里的PByte也就是arry of byte;

作者: nm_wyh   发布时间: 2011-12-13

热门下载

更多