用Delphi编写和调用DLL

用Delphi编写和调用DLL

新建一个DLL工程先,再在工程中新建一个单元文件,在该文件中声明并实现你的加密与解密方法,在DLL中对字符串操作时,尽量使用PChar 。

//Delphi(Pascal) code

unit Unit1;

interface

function encrypt(Str: PChar): PChar;
function decrypt(Str: PChar): PChar;

implementation

function encrypt(Str: PChar): PChar;
begin
  //your code
end;

function decrypt(Str: PChar): PChar;
begin
  //your code
end;

end.

然后在你的DLL工程中输出这两个方法

Delphi(Pascal) code

library Project1;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  Unit1 in 'Unit1.pas';

{$R *.res}

Exports
   encrypt;
   decrypt;

begin
end.

调用代码如下:

//Delphi(Pascal) code

type
  TMyFun = function (Str: PChar): PChar

procedure TForm1.Test;
var
  s: String;
  P: PChar;
  MyFun: TMyFun;
  LibHandle: THandle;
begin
  s := 'abcdefg';
  LibHandle := LoadLibrary(DLLFileName);//你编译后的DLL所在的路径
  try
    if LibHandle = 0 then begin  
      MyFun := GetProcAddress(LibHandle, 'encrypt');
      if Assigned(MyFun) then
        MyFun(PChar(s));
  finally
    FreeLibrary(LibHandle);
  end;
end;

版权所有,转载请注明文章出处 http://blog/csdn.net/cadenzasolo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值