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');这步之后,程序就完全没反应了,怎么点都不响应。估计是一直在内存里读啊读的,但是不知道读的是什么地址的……
有哪位大侠可以告诉我,我的问题到底出在哪儿?我总觉得是我什么小地方没注意到,做错了。跪求指点。
我对这个封装好了的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;
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分配内存空间
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;
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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28