我想写一个DLL,并且在输出函数中要传递记录或数组参数而且要有对话框。我只知道需要用什么sharemem单元。求高手给完整源代码演示。非常感谢。

解决方案 »

  1.   

    library MyDLL;uses
      SysUtils,
      Classes,
      uDialog in 'uDialog.pas' {FDialog};{$R *.res}exports
      GetData;begin
    end.
      

  2.   

    unit uDialog;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TFDialog = class(TForm)
        RadioGroup1: TRadioGroup;
        LabeledEdit1: TLabeledEdit;
        BtnOK: TButton;
        BtnCancel: TButton;
      private
        { Private declarations }
      public
        { Public declarations }
      end;  TData = array [1..1000] of Extended;function  GetData(Par: Integer): TData; cdecl;implementation{$R *.dfm}procedure InitData(var D: TData);
    var
      I: Integer;
    begin
      for I := 1 to 1000 do D[I] := 0;
    end;procedure SetData(Rand: Boolean; var D: TData);//加入其他参数,根据需要设置数据
    var
      I: Integer;
    begin
      if Rand then begin
        for I := 1 to 1000 do
          D[I] := Random(100)/100;//假定只需要0.00到0.99,小数点两位。
      end else begin
      end;
    end;function  GetData(Par: Integer): TData;
    var
      Data: TData;
    begin
      InitData(Data);
      with TFDialog.Create(Application) do try
        case ShowModal of
          mrOK:     case RadioGroup1.ItemIndex of
                      0: begin//Default
                         end;
                      1: begin//Input
                         end;
                    else
                    end;
          mrCancel: case RadioGroup1.ItemIndex of
                      0: begin//Default
                         end;
                      1: begin//Input
                         end;
                    else
                    end;
        else
          SetData(True, Data);
        end;
      finally
        Free;
      end;
      Result := Data;
    end;end.
      

  3.   

    program Call;uses
      Forms,
      uCall in 'uCall.pas' {FCall};{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TFCall, FCall);
      Application.Run;
    end.
      

  4.   

    我已经调试成功,就是那个该死的cdecl。
    顺便再问小可,为什么没有使用sharemem呢?
      

  5.   

    unit uCall;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TFCall = class(TForm)
        Label1: TLabel;
        EditParam: TEdit;
        BtnCall: TButton;
        ResultMemo: TMemo;
        procedure BtnCallClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      FCall: TFCall;implementation{$R *.dfm}type
      TData = array [1..1000] of Extended;function  GetData(Par: Integer): TData; cdecl; external 'MyDLL.dll' name 'GetData';procedure TFCall.BtnCallClick(Sender: TObject);
    var
      Data: TData;
      I: Integer;
    begin
      Data := GetData(StrToInt(EditParam.Text));
      for I := 1 to 1000 do
        ResultMemo.Lines.Append(FormatFloat('0.00', Data[I]));
    end;end.
      

  6.   

    type
      TData = array [1..1000] of Extended;function  GetData(Par: Integer): TData; cdecl; external 'MyDLL.dll' name 'GetData';ShareMem不是必须使用的单元。只有不同的应用程序实例需要通过公共的内存区域传递数据时,才需要使用该单元。