library DOCReport;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }uses
  SysUtils,
  Classes,
  ADODB,
  DB,
  ARWordReport;
var
  queryDOC:TADOQuery;
  reportDOC: TARWordReport;{$R *.res}function CreateDOCReport(strConnect :pchar;strTemplateFilePath: pchar;strTargetFilePath: pchar): pchar;  stdcall;
begin
  queryDOC := TADOQuery.Create(NIl);
  reportDOC:= TARWordReport.Create(NIL);
  queryDOC.ConnectionString := strConnect;
  reportDOC.OnNeedSQL := reportDOCNeedSQL;
  reportDOC.OnTag := reportDOCTag;

  //模板文件
  reportDOC.Filename := strTemplateFilePath;
  //It is enough to use only Preview method
  //in most cases when you work with TARWordReport.
  reportDOC.Preview;  // Closing TQuery which used in OnNeedSQL event
  queryDOC.Close;
  queryDOC.SQL.Clear;
  Result:='';
end;
procedure reportDOCNeedSQL(Sender: TObject; SQLstring: String;
  var ResultDataset: TDataSet);
begin
  // Initialy ResultDataset=nil
  // You need to point it to your dataset
  try
    //Check SQLstring here if needed
    //(like a ARWordReportPerfom.CheckQuery function)
    queryDOC.Close;
    queryDOC.SQL.Clear;
    queryDOC.SQL.Add(SQLstring);
    queryDOC.Open;
    ResultDataset:= queryDOC;
  except
    ResultDataset:= nil;
  end;
end;
procedure reportDOCTag(Sender: TObject; var TagValue: String);
begin
  // Using OnTag Event
  TagValue:= AnsiUpperCase(TagValue);
  if TagValue='NOWDATE' then TagValue:= DateToStr(now)
  else if TagValue='NOWTIME' then TagValue:= TimeToStr(now)
  else TagValue:='';
end;
exports
  CreateDOCReport;
begin
end.我写的一个dll,如何给这两个事件动态关联事件呀?

解决方案 »

  1.   

    reportDOC.OnNeedSQL := reportDOCNeedSQL;
    這不是關聯了事件了嗎??但這個事件的觸發是由本身類來維護的,還要是要由你自己寫代碼來維護
      

  2.   

    可以直接赋值也可用RTTI(TypeInfo单元)指定,SetMethodProp方法
      

  3.   

    不行呀,编译的时候提示
    Undeclared identifier:'reportDOCNeedSQL'
    Undeclared identifier:'reportDOCTag'