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.

  • 相关阅读:
    python斐波那契数列
    python装饰器
    Python文件操作
    python 第三方库的安装方法
    Ajax 基本使用学习记录
    微分方程概述
    Typora中的数学公式
    Ubuntu20.04 安装 mysql8.0 之后 root 账户无法登录
    Ubuntu20.04换源之后依旧慢?如何更有效的换源
    Ubuntu18.04更新python版本
  • 原文地址:https://www.cnblogs.com/westsoft/p/5935718.html
Copyright © 2011-2022 走看看