• Delphi中的数据库插件


    type
      TfrmMain = class(TForm)

    ……

    private:

        procedure LoadPlugin(sr: TSearchRec);
        procedure LoadPlugins;
        procedure PlugInClick(Sender: TObject);
      public
        { Public declarations }
      end;

    type
      TPluginInit = procedure(AHandle: THandle;vConn: TADOConnection); stdcall;
      TPluginDescribe = procedure(var Desc: PChar); stdcall;

    var
      frmMain: TfrmMain;
      LibHandle:THandle;

    const
      cPLUGIN_DESCRIBE:PAnsiChar='MyPlugInDescript';
      cPLUGIN_INIT = 'PluginInit';
      cPLUGIN_MASK='*.dll';

     

    { 在应用程序目录下查找插件文件 }
    procedure TfrmMain.LoadPlugins;
    var
      sr:     TSearchRec;
      path:   string;
      Found: Integer;
    begin
      lstPlugin.Items.Clear;
      path := ExtractFilePath(Application.Exename)+'\PlugIn\';
       try
        Found := FindFirst(path + cPLUGIN_MASK, 0, sr);
         while Found = 0 do begin
          LoadPlugin(sr);
          Found := FindNext(sr);
         end;
       finally
        FindClose(sr);
       end;
    end;

    // {加载指定的插件DLL. }
    procedure TfrmMain.LoadPlugin(sr: TSearchRec);
    var
      Description:   PChar;
      DescribeProc: TPluginDescribe;
      InitProc:      TPluginInit;
      LItem:TListItem;
      MItem:TMenuItem;
      s:String;
    begin
      s:=ExtractFilePath(Application.Exename)+'\PlugIn\'+sr.Name;
      LibHandle := LoadLibrary(PChar(s));
    //  FreeLibrary(LibHandle);
       if LibHandle <> 0 then
       begin
         // 查找 DescribePlugin.
         @DescribeProc := GetProcAddress(LibHandle,cPLUGIN_DESCRIBE);
         //if Assigned(DescribeProc) then
         if @DescribeProc<>nil then
         if true then
         begin
           // 调用 DescribePlugin.
           DescribeProc(Description);
           LItem:=lstPlugin.Items.Add ;
           LItem.Caption:=Description;
           LItem.SubItems.Add(sr.Name);

           MItem:=TMenuItem.Create(self);
           MItem.Caption := Description+'('+sr.Name+')';
           MItem.OnClick := PlugInClick;
           MItem.Tag:=lstPlugin.Items.Count ;
           MainMenu2.Items[3].Add(MItem);

           //FreeLibrary(LibHandle);
           //查找InitPlugin.

         end
         else
         begin
          MessageDlg('文件 "' + sr.Name +'" 不是插件.', mtInformation, [mbOK], 0);
          //FreeLibrary(LibHandle);
         end;
       end
       else
       begin
        MessageDlg('装入插件时发生错误! "' +sr.Name + '".', mtInformation, [mbOK], 0);
       end;
    end;

    procedure TfrmMain.PlugInClick(Sender: TObject);
    var
      MyInitProc:TPluginInit;
      FName:String;
    begin
      if FunPass('26',UPwd) then
      begin

        FName:=ExtractFilePath(Application.Exename)+'\PlugIn\'+lstPlugIn.Items[(Sender as TMenuItem).tag-1].SubItems[0];
        try
          LibHandle := LoadLibrary(PAnsiChar(FName));
          if LibHandle <> 0 then
          begin
             MyInitProc := GetProcAddress(LibHandle, cPLUGIN_INIT);
             if Assigned(InitProc) then
             begin
              //调用InitPlugin.
              MyInitProc(Application.Handle,dmMain.conn);
              //FreeLibrary(LibHandle);
             end;
          end
          else
           // FreeLibrary(LibHandle);
          begin
          end;
        except
        end;
      end;
    end;

     

    在窗口创建时调用:

    procedure TfrmMain.FormCreate(Sender: TObject);
    begin
      LoadPlugins;
    end;

    //**********************************************************************

    //下面是一个Plugin的示例:

    //**********************************************************************

    工程文件:

    library ImpUFArc;

     

    uses
      SysUtils,
      Classes,
      FfrmImportUFData in 'FfrmImportUFData.pas' {frmImportUFData};

    {$R *.res}

    exports
            PluginInit,
            MyPlugInDescript;

    begin
    end.

     

    unit FfrmImportUFData;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Buttons, StdCtrls, ExtCtrls, ADODB, DB, ComCtrls,registry;

    type
      TfrmImportUFData = class(TForm)

    ……
      private
        { Private declarations }
        procedure ImportUF(sFields, dFields: TStrings;Over:Boolean);
        procedure ImportClass(Over: Boolean);
      public
        { Public declarations }
      end;

    procedure PluginInit(AHandle: THandle;vConn: TADOConnection);export;  stdcall;
    procedure MyPlugInDescript(var Desc: PChar);export;  stdcall;

    var
      frmImportUFData: TfrmImportUFData;

    const
      RegSession:String='ImpUFArc';

    implementation

    {$R *.dfm}

    procedure MyPlugInDescript(var Desc: PChar);export;  stdcall;
    begin
      Desc:='导入用友基础档案';
    end;

    procedure PluginInit(AHandle: THandle;vConn: TADOConnection);export;  stdcall;
    begin
       Application.Handle := AHandle;
       frmImportUFData:=TfrmImportUFData.Create(Application);
       frmImportUFData.qryTemp.Connection:=vConn;
       frmImportUFData.tblDes.Connection:=vConn;
       frmImportUFData.ShowModal ;
       frmImportUFData.Free;
    end;

  • 相关阅读:
    Java学习笔记(4)
    Idea常用功能汇总
    Java学习笔记(3)
    Java学习笔记(2)
    Java学习笔记(1)
    如何开发NPM包
    c#抓屏功能在DPI缩放后,截到的图片不完整的问题
    支持续传功能的ASP.NET WEB API文件下载服务
    ASP.NET MVC 阻止通过Url直接访问服务器上的静态文件
    VS2013/VS2015/VS2017通过oschina托管代码
  • 原文地址:https://www.cnblogs.com/GarfieldTom/p/1496678.html
Copyright © 2020-2023  润新知