zoukankan      html  css  js  c++  java
  • Delphi Dll 动态调用例子(1)

    http://blog.sina.com.cn/s/blog_62c46c3701010q7h.html

    一、编写dll

    library TestDllByD2007;

    uses
      SysUtils,
      Classes;
      function test(const astr:PChar):Boolean;stdcall;
      begin
        Result:=True;
      end;
    {$R *.res}
      exports test;
    begin
    end.

    注意:1.不能使用string类型,否则涉及到资源释放的问题,使用Pchar代替string。

            2.Dll中不能直接返回string,除非引用ShareMem单元,发布程序时需要包含BORLNDMM.DLL

    二、编写测试窗体,就一个button.在button的代码中,实现dll的动态加载和释放。

    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;

    type
      TFTest=function (const astr:PChar):Boolean;
      TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.btn1Click(Sender: TObject);
    var
      sdll:string;
      tfTTT:TFTest;
      handle:THandle;
      sstr:string;//声明该变量,目的是避免发生异常。
    begin
      sdll:='TestDllByD2007.dll';
      Caption:='';
      handle:=LoadLibrary(PAnsiChar(sdll));
      if Handle <> 0 then
      begin
        @tfTTT := GetProcAddress(Handle, 'test');
        if @tfTTT <> nil then
        begin
          sstr:='testsss';
          if tfTTT(PChar(sstr))=True then
          begin
            Caption:='true';
          end
          else
          begin
            Caption:='false';
          end;
        end
        else
        begin
          Caption:='can not find the test function';
        end;
        FreeLibrary(Handle);
      end
      else
      begin
        Caption:='can not load library '+ sdll;
      end;
    end;

    end.

  • 相关阅读:
    东南大学2021年高等代数
    北京师范大学2021年数学分析
    华东师范大学2021年数学分析考研试题参考解答修正版
    中国科学院大学2021年数学分析考研试题参考解答
    [数学考研竞赛00052]浙江大学2021年数学分析考研试题参考解答
    python3.6, cv2.imread()导入图片失败,none
    python商品分类信息
    mongodb 聚合(Map-Reduce)
    mongodb 聚合(aggregate)
    Java-Security(七):Spring Security方法注解认证启用
  • 原文地址:https://www.cnblogs.com/westsoft/p/5935718.html
Copyright © 2011-2022 走看看