将结构数据类型在dll及程序中都定义一下,然后就如同普通数据类型一样可以输出了

解决方案 »

  1.   

    你可以定义个记录形的变量就可以了,但要注意delphi的数据类型。
      

  2.   

    请看我下面的代码错在何处?
    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.
      

  3.   

    是啊,如果实在不行,就输出指针试试。wanderung()说的是对的,只要DLL和程序中结构体的定义相同就可以传递了
      

  4.   

    TO newcsdn (我是一只菜鸟) :
    我把我的源代码贴给你吧,呵呵!
    *******************************************************************
    //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.
    *******************************************************************
      

  5.   

    function   GetRecordinfomation( var Pf : Pinfo ):boolean;stdcall;
    Pf是指针,为什么还有用var 声明
      

  6.   

    此外,对于结构类型又使用了string类型,请在
    dll和调用DLL的工程文件中包含sharemem这个单元
    sharemem单元必须是第一个单元
      

  7.   

    gaodaya() 说得对,如果不希望附带一个borlnmem.dll(好像是这个文件,反正差不多的),结构体中一律使用pchar来代替string
      

  8.   

    同意gaodaya,如dll中用到string类型,必须要在首位加入sharemem。
      

  9.   

    to gaodaya()
    我按照你讲的几点作了改正,如下:
    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"??
    请求帮助!
      

  10.   

    to  fwjingling(蓝精灵) 
    你直接返回record的方法我会用;
    可是我现在想
    在参数中加入一个指针,用指针来传递这个结构体数据类型
    这样比较W符合in32API的标准。
    有谁能搞定啊?
    to gaodaya()
    你能不能举一个例子给我啊
      

  11.   

    你把调用部代码发给我,我帮你看一下。
    另外,你可以试着单步调试一下,看看问题是不是出在调用dll上,
    可能你的指针使用存在问题,你贴出的源码不全,无法判断。[email protected]