用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