• 、Dll文件的编写 调用 说明


    1>新建Dll文件TestLib.dll  

    新建Unit文件U_TestFunc  

    U_TestFunc代码如下:

     unit U_TestFunc;

     interface    

     uses //尽可能的少uses这样会缩小dll的体积     

       SysUtils;     

     //求和   

     function Sum(x1,x2: Integer): Integer; stdcall    

     implementation    

     function Sum(x1,x2: Integer): Integer; stdcall  

     begin   

       Result := x1+x2;  

     end;    

    end.    

    TestLib代码如下:    

    library TestLib;    

    uses    SysUtils,   

    U_TestFunc in 'U_TestFunc.pas';    

    {$R *.res}    

    exports

         Sum;    

    begin  

    end.  

     

    2>调用方法  源码如下:

     unit Unit1;    

     interface    

     uses

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

     type   

      TForm1 = class(TForm)     

      Edit1: TEdit;

          BitBtn1: TBitBtn;

          Edit2: TEdit;

          BitBtn2: TBitBtn;     

        procedure BitBtn1Click(Sender: TObject);     

        procedure BitBtn2Click(Sender: TObject);

          private     

        { Private declarations }

      public     

        { Public declarations }   

      end;    

     var   

      Form1: TForm1;    

     implementation    

     {$R *.dfm}     

       function Sum(a1,a2: Integer): Integer; stdcall; external 'C:TestLib.dll';

       procedure TForm1.BitBtn1Click(Sender: TObject);

       begin   

      //if FileExists('C:TestLib.dll') then  这里感觉这两个函数没有什么区别,文件在本地不在本地貌似效果都一样。   

        if LocaleFileExists('C:TestLib.dll') then

            Edit1.Text := IntToStr(Sum(100,200))   

      else

            ShowMessage('文件不存在!');

       end;    

     procedure TForm1.BitBtn2Click(Sender: TObject);  

     type

       TIntFunc = function(a1,a2: Integer): Integer; stdcall;  

     var

       Th: THandle;   

       Tf: TIntFunc;

     begin

       Th := LoadLibrary('C:TestLib.dll');   

      if Th>0 then   

      begin

          try

            @Tf := GetProcAddress(Th, PAnsiChar('Sum'));

             if @Tf<>nil then

             begin

               Edit2.Text := IntToStr(Tf(100,200));

             end else

                ShowMessage('Sum函数没有找到!');

          finally

             FreeLibrary(Th); //释放Dll

           end;

         end else

           ShowMessage('TestLib.dll文件没有找到!');

       end;

       end.

  • 相关阅读:
    spark调度器FIFO,FAIR
    elasticsearch5.6.8 创建TransportClient工具类
    elasticsearch TransportClient bulk批量提交数据
    java 参数来带回方法运算结果
    idea上传代码到git本地仓库
    2020-03-01 助教一周小结(第三周)
    2020-02-23 助教一周小结(第二周)
    2020-02-16 助教一周小结(第一周)
    寻找两个有序数组的中位数
    无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/vin2008/p/3979504.html
Copyright © 2020-2023  润新知