在工程文件的加入ShareMem我用楼主的函数测试没有发现问题,我的开发环境是delphi6+xp我用ADO+SQL Server2000做测试,建了个test表,只有一个字段bmbh char(4)library DLLCreateField;uses ShareMem, DB, Dialogs, SysUtils, Classes;function AddCalcStringFieldDLL(_FieldName: string; _DataSet: TDataSet; _Size: Integer): Boolean;stdcall;begin if (_DataSet = nil) or (Trim(_FieldName) = '') or (_DataSet.FindField(_FieldName) <> nil) then begin Result := False; Exit; end; _DataSet.Close; if _Size = 0 then _Size := 20; //设置Size 默认值为20; with TStringField.Create(_DataSet) do begin FieldName := _FieldName; DisplayLabel := _FieldName; Size := _size; FieldKind := fkCalculated; DataSet := _DataSet; end; _DataSet.Open; Result := True;end;exports AddCalcStringFieldDLL;beginend.测试程序里也要加入ShareMem静态字段在设计阶段就加入到数据集里面unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids;type TForm1 = class(TForm) ADOQuery1: TADOQuery; ADOConnection1: TADOConnection; Button1: TButton; DataSource1: TDataSource; DBGrid1: TDBGrid; Button2: TButton; ADOQuery2: TADOQuery; DBGrid2: TDBGrid; DataSource2: TDataSource; ADOQuery1bmbh: TStringField; ADOQuery2bmbh: TStringField; procedure ADOQuery1CalcFields(DataSet: TDataSet); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ADOQuery2CalcFields(DataSet: TDataSet); private function AddCalcStringField(_FieldName: String; _DataSet: TDataSet; _Size: Integer): Boolean; { Private declarations } public { Public declarations } end;var Form1: TForm1; function AddCalcStringFieldDLL(_FieldName: string; _DataSet: TDataSet; _Size: Integer): Boolean;stdcall; external 'DLLCreateField.dll';implementation{$R *.dfm}function TForm1.AddCalcStringField(_FieldName: string; _DataSet: TDataSet; _Size: Integer): Boolean;begin if (_DataSet = nil) or (Trim(_FieldName) = '') or (_DataSet.FindField(_FieldName) <> nil) then begin Result := False; Exit; end; _DataSet.Close; if _Size = 0 then _Size := 20; //设置Size 默认值为20; with TStringField.Create(_DataSet) do begin FieldName := _FieldName; DisplayLabel := _FieldName; Size := _size; FieldKind := fkCalculated; DataSet := _DataSet; end; _DataSet.Open; Result := True;end;procedure TForm1.ADOQuery1CalcFields(DataSet: TDataSet);begin if DataSet.FieldByName('bmmc')<>nil then DataSet.FieldByName('bmmc').AsString:='TEST';end;procedure TForm1.Button1Click(Sender: TObject);begin //在exe里直接创建计算字段 AddCalcStringField('bmmc',ADOQuery1,20);end;procedure TForm1.Button2Click(Sender: TObject);begin //在dll里创建计算字段 AddCalcStringFieldDLL('bmmc',ADOQuery2,20);end;procedure TForm1.ADOQuery2CalcFields(DataSet: TDataSet);begin if DataSet.FieldByName('bmmc')<>nil then DataSet.FieldByName('bmmc').AsString:=IntToStr(DataSet.RecNo);end;end.