用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 = then begin
MyFun := GetProcAddress(LibHandle, 'encrypt');
if Assigned(MyFun) then
MyFun(PChar(s));
finally
FreeLibrary(LibHandle);
end;
end;
版權所有,轉載請注明文章出處 http://blog/csdn.net/cadenzasolo