是这样的,我想写一个DLL,里面有一个数据库表的函数,调用时,传递一个记录型的数组过去,该记录型的数姐保存字段的名称、类型、长度等等信息,我该怎样定义这个函数呢,涵数里面又怎样访问这个记录数组里面的数据呢?请老鸟们指点下啊。
记录类似如下
TMyFields=record
   FieldName:string;
   FieldType:string;
   FieldLength:integer;
   ...
end;
arrFields=array of TMyFields
...
setLength(ArrFields,10)
//对记录数组进行赋值
...
/////////////////
假改我需要这样调用DLL中的函数进行建表
function CreateTable('表名',arrFields)
那DLL中这个CreateTable函数该怎么写呢,如何访问这个arrFields

解决方案 »

  1.   

    不要使用String等等之类的指针类型参数,如果需要使用String,建议使用字符数组。参数不要使用Array,换用记录指针,另外多添加一个参数,表示指针当中含有多少个结构体即可。
    type
      PMyRecord = ^TMyRecord;
      TMyRecord = record
        Name: array [0..19] of AnsiChar;
        Value: Integer;
        Tag: Integer;
      end;function CreateTable(Rec: PMyRecord; szRecCount: Integer): Boolean; stdcall;
    begin
      ...
    end;exports
      CreateTable;
      

  2.   


    //DLL
    library Project1;uses
      SysUtils,  Classes;{$R *.res}
    type
      PMyFields= ^TMyFields;
      TMyFields = record
        FieldName:array [0..100] of Char;
        FieldType:array [0..100] of Char;
        FieldLength:integer;
    end;function CreateTable( MyTest:PMyFields):boolean;stdcall;
    begin
      Result:=True;
      try
        //操作传进来的结构体   except
        Result:=False;
      end;
    end;exports
      CreateTable ;
    //主调
    type
      PMyFields= ^TMyFields;
      TMyFields = record
        FieldName:array [0..100] of Char;
        FieldType:array [0..100] of Char;
        FieldLength:integer;
      end;
      TCreateTableProc=function ( MyTest:PMyFields):boolean;stdcall;
    procedure TForm1.JustForTest;
    var
      DllHnd: THandle;
      CreateTableProc: TCreateTableProc;
      Stf:PMyFields;
    begin
      DllHnd := LoadLibrary(PChar('project1.dll'));
      try
        if (DllHnd <> 0) then
        begin
           @CreateTableProc :=GetProcAddress(DllHnd, 'CreateTable');
           if (@CreateTableProc<>nil) then
           begin
             New(Stf); //此处可用一个数组+Stf
             try
               stf.FieldName:='a';
               if not CreateTableProc(Stf) then  ShowMessage('获取值错误');
               //Showmessage('result='+Inttostr(Stf^.data1));
             finally
               Dispose(Stf);
             end;
           end;
        end
        else
        begin
          Application.MessageBox(PChar('DLL加载出错,DLL可能不存在!'), PChar('错误'),
              MB_ICONWARNING or MB_OK);
        end;
      finally
        FreeLibrary(DllHnd);
      end;
    end;