zoukankan      html  css  js  c++  java
  • 用DLL实现插件的简单演示

    这是DLL的代码
    
    library MyDll;
    
    uses
      SysUtils,
      Dialogs,
      Classes;
    
    procedure ShowInfo(info:PChar);stdcall;
    begin
      ShowMessage('您选择了【'+info+'');
    end;
    
    function GetCaption:Pchar;
    begin
      Result := '中国';
    end;
    
    exports ShowInfo,        
            GetCaption;
    
    {$R *.res}
    
    begin
    end.
    View Code
    这是调用窗体的代码
    本例只使用了一个DLL,所以当有多个DLL时,需要循环DLL所在目录,依次加载DLL
    
    unit Main;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Menus, ExtCtrls;
    
    type
      TShowInfo = procedure (info:PChar);stdcall;
      TGetCaption = function : PChar;stdcall;
    
    
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        MainMenu1: TMainMenu;
        Image1: TImage;
        procedure Button2Click(Sender: TObject);  private
        { Private declarations }
        FHandel : THandle;     //DLL句柄
        FProAddress: Pointer;  //DLL中ShowInfo的地址
        showinfo: TShowInfo;   //为动态加载DLL而设
        procedure LoadPlusIn;  //加载插件(DLL)
        procedure ItemClick(Sender: TObject);   //自定义菜单点击事件
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      LoadPlusIn;
    end;
    
    procedure TForm1.ItemClick(Sender: TObject);
    begin
      @showinfo := FProAddress;     //取地址
      if @showinfo <> nil then
        showinfo(PWideChar(TMenuItem(Sender).Caption));  //执行DLL中的ShowInfo
    end;
    
    procedure TForm1.LoadPlusIn;
    var
      getcaption: TGetCaption;
      item : TMenuItem;
    begin
      FHandel := LoadLibrary('MyDll.dll');   //加载
      if FHandel = 0 then
      begin
        ShowMessage('加载失败!');
        Exit;
      end
      else
      begin
        @getcaption := GetProcAddress(FHandel,'GetCaption');    //取DLL中GetCaption地址
        if @getcaption <> nil then
        begin
          item := TMenuItem.Create(MainMenu1);    //创建一个菜单
          item.Caption := getcaption;             //取Caption,即调用DLL中的GetCaption
          FProAddress := GetProcAddress(FHandel,'ShowInfo');  //取得DLL中ShowInfo的地址
          item.OnClick := ItemClick;              //赋予菜单项的点击事件
          MainMenu1.Items.Add(item);              //添加到主菜单
        end;
    
      end;
    end;
    
    end.
    View Code
  • 相关阅读:
    maven工程下 读取resource下配置文件
    js生成二维码以及点击下载二维码
    RGB颜色值与十六进制颜色码对照表
    用Java实现给图片添加文字
    CryptoAPI与openssl数字签名与验证交互
    CryptoAPI与openssl RSA非对称加密解密(PKCS1 PADDING)交互
    openssl与cryptoAPI交互AES加密解密
    JAVA解析各种编码密钥对(DER、PEM、openssh公钥)
    Java与.NET兼容的RSA密钥持久化方法
    .NET导入openssl生成的公钥之BEGIN RSA PUBLIC KEY
  • 原文地址:https://www.cnblogs.com/key-ok/p/3428865.html
Copyright © 2011-2022 走看看