将结构数据类型在dll及程序中都定义一下,然后就如同普通数据类型一样可以输出了
解决方案 »
- delphi7.0运行常报错
- [access]为什么在delphi中出错?
- [高分送]哪位大虾用过Audio Components Suite(BASS组件?)有分要送!
- IdTCP问题....
- 错误:File not found: 'Shell32_TLB.dcu'怎么办?
- 如何播放AVI,VOB文件
- 调查:深圳有多少DELPHI程序员 ?
- 请教一个关于dblookupcombobox用法的问题!
- 如何判断一数组中a: array[1..10] of string没有重复值。
- 用ZPL II语言编写过斑马打印程序的请进!!!帮小弟看看这个问题如何解决
- 大家有什么更好的办法解决在C/S中相互传递变量吗?
- 怎样显示这样的格式
library recorddll;
uses
SysUtils,
Classes;type
Pinfo=^Tinfo;
Tinfo=record
j : integer;
inform :string;
end;{$R *.RES}function GetRecordinfomation( var Pf : Pinfo ):boolean;stdcall;
begin
fillchar(pf^,sizeof(Pf^),0);
pf^.j:=1;
pf^.inform:='this is info'; Result:=true ;
end;exports
GetRecordinfomation;
begin
end.引用dll的单元
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type Pinfo=^Tinfo;
Tinfo=record
j : integer;
inform :string;
end; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
pif : Pinfo;function GetRecordinfomation(var Pf : Pinfo ):boolean;stdcall; external 'recorddll.dll';
implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
fillchar(pif^,sizeof(Pif^),0);
if GetRecordinfomation(pif) then
begin
showmessage(inttostr(pif^.j)+pif^.inform);
end
end;end.
我把我的源代码贴给你吧,呵呵!
*******************************************************************
//DLL代码
{结构变量作为参数传递的动态库的窗体}
unit Unitdll;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Treturnrec=record //记录为返回值的结构声明
ReturnFlag:Boolean;
AddCount:Integer;
end;
type
TDateRec = record //记录为输入参数的结构声明
Year: Integer;
Month: PChar;
Day:integer;
end;
type
TfrmShow = class(TForm)
edtYear: TEdit;
edtMonth: TEdit;
edtDay: TEdit;
btnShow: TButton;
btnExit: TButton;
procedure btnShowClick(Sender: TObject);
procedure btnExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
frmShow: TfrmShow;
ReturnRec:Treturnrec; //返回值结构变量
DateRec:Tdaterec; //输入参数结构变量
implementation{$R *.dfm}procedure TfrmShow.btnShowClick(Sender: TObject);
begin
edtYear.Text:=IntToStr(DateRec.Year);
edtMonth.Text:=DateRec.Month;
edtDay.Text:=IntToStr(DateRec.Day);
end;procedure TfrmShow.btnExitClick(Sender: TObject);
begin
Close;
end;end.
*******************************************************************
//调用DLL的代码
{将一个记录类型的结构变量作为参数调用动态库,并返回另一不同记录的结构变量}
unit Unitmain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TReturnRec=record //记录为返回值的结构声明
ReturnFlag:Boolean;
AddCount:Integer;
end;
type
TDateRec = record //记录为输入参数的结构声明
Year: Integer;
Month: PChar;
Day:integer;
end;
type
TfrmMain = class(TForm)
edtYear: TEdit;
btnShow: TButton;
edtMonth: TEdit;
edtDay: TEdit;
edtRetrunBool: TEdit;
edtReturnInt: TEdit;
procedure btnShowClick(Sender: TObject);
private
recDate:TDateRec; //输入参数结构的变量
recReturn:TReturnRec; //返回值结构的变量
{ Private declarations }
public
{ Public declarations }
end;var
frmMain: TfrmMain;implementation{$R *.dfm}
//动态库函数声明
function changedate(RecordX:TDateRec;iCount:Integer):TReturnRec;stdcall;external 'Prodll' name 'changedate';procedure TfrmMain.btnShowClick(Sender: TObject);
begin with recDate do //将输入值赋给记录为输入参数的结构
begin
Year:=strtoint(edtYear.Text);
Month:=PChar(edtMonth.Text);
Day:=strtoint(edtDay.Text);
end;
edtRetrunBool.Enabled:=True;
edtReturnInt.Enabled:=True;
recReturn:=changedate(recDate,10); //将返回值赋给变量
edtRetrunBool.Text:=booltostr(recReturn.ReturnFlag); //将返回值分别输出
edtReturnInt.Text:=inttostr(recReturn.AddCount);
end;end.
*******************************************************************
Pf是指针,为什么还有用var 声明
dll和调用DLL的工程文件中包含sharemem这个单元
sharemem单元必须是第一个单元
我按照你讲的几点作了改正,如下:
uses
sharemem,SysUtils,
Classes;
{$E dll}
type
Pinfo=^Tinfo;
Tinfo=record
j : integer;
inform : Pchar;
end;{$R *.RES}
function GetRecordinfomation( Pf : Pinfo ):boolean;stdcall;
var
ch : Pchar;
begin
fillchar(pf^,sizeof(Pf^),0);
pf^.j:=1;
pf^.inform:=strcopy(ch,'this is info');
Result:=true ;
end;
exports
GetRecordinfomation;
begin
end.
调用时,改为如下:
procedure TForm1.Button1Click(Sender: TObject);
begin
fillchar(pif^,sizeof(Pif^),0);
if GetRecordinfomation(pif) then
begin
showmessage(inttostr(pif^.j)+strpas(pif^.inform));
end
end;
可是为什么还是出现这个错误:
access violation at address 004029 BC in module"project1.exe.
Read of address FFFFFFFF"??
请求帮助!
你直接返回record的方法我会用;
可是我现在想
在参数中加入一个指针,用指针来传递这个结构体数据类型
这样比较W符合in32API的标准。
有谁能搞定啊?
to gaodaya()
你能不能举一个例子给我啊
另外,你可以试着单步调试一下,看看问题是不是出在调用dll上,
可能你的指针使用存在问题,你贴出的源码不全,无法判断。[email protected]