动态链接库的调用!(50分)

  • 主题发起人 主题发起人 米度斯
  • 开始时间 开始时间

米度斯

Unregistered / Unconfirmed
GUEST, unregistred user!
我自己编的base64编解码函数放在工程文件中能用,怎么放到动态链接库里调用后总是出现“InValide Pointer Operation”的错误提示?
程序和动态链接库:
implementation
function Base64_Encode(var str:string):boolean;external 'Base64lib.dll';
function Base64_Decode(var str:string):boolean;stdcall;external 'Base64lib.dll';
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var str:string;
begin
str:=edit1.Text;
Base64_Encode(str);
label1.Caption:=str;
Base64_Decode(str);
label2.Caption:=str;
end;

library Base64lib;

{ 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;

{$R *.res}

function Base64_Encode(var str:string):boolean;
const
EncodeBase64:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

var
Byte1,Byte2,Byte3:char;
LenMod:integer;
LenDiv:integer;
i:integer;
EncodeStr:string;
begin
EncodeStr:='';
LenDiv:=length(str) div 3;
LenMod:=length(str) mod 3;
for i:=1 to LenDiv do
begin
Byte1:=str[i*3-2];
Byte2:=str[i*3-1];
Byte3:=str[i*3];
EncodeStr:=EncodeStr+EncodeBase64[(Byte(Byte1) shr 2)+1];
EncodeStr:=EncodeStr+EncodeBase64[(((Byte(Byte1) shl 4) or (Byte(Byte2) shr 4)) and $3f)+1];
EncodeStr:=EncodeStr+EncodeBase64[(((Byte(Byte2) shl 2) or (Byte(Byte3) shr 6)) and $3f)+1];
EncodeStr:=EncodeStr+EncodeBase64[((Byte(Byte3) and $3f))+1];
end;{end of for}

if LenMod=1 then
begin
Byte1:=str[length(str)];
EncodeStr:=EncodeStr+EncodeBase64[(Byte(Byte1) shr 2)+1];
EncodeStr:=EncodeStr+EncodeBase64[((Byte(Byte1) shl 4) and $3f)+1];
EncodeStr:=EncodeStr+'=';
EncodeStr:=EncodeStr+'=';
end;

if LenMod=2 then
begin
Byte1:=str[length(str)-1];
Byte2:=str[length(str)];
EncodeStr:=EncodeStr+EncodeBase64[(Byte(Byte1) shr 2)+1];
EncodeStr:=EncodeStr+EncodeBase64[(((Byte(Byte1) shl 4) or (Byte(Byte2) shr 4)) and $3f)+1];
EncodeStr:=EncodeStr+EncodeBase64[((Byte(Byte2) shl 2) and $3f)+1];
EncodeStr:=EncodeStr+'=';
end;

Str:=EncodeStr;
end;

function Base64_Decode(var str:string):boolean;
const
DecodeBase64:array[0..127] of integer
=(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,63,
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);

var
i,LenDiv:integer;
DecodeStr:string;
B1,B2,B3,B4:char;
begin
LenDiv:=length(str) div 4;
DecodeStr:='';
for i:=1 to LenDiv do
begin
B1:=str[i*4-3];
B2:=str[i*4-2];
B3:=str[i*4-1];
B4:=str[i*4];
DecodeStr:=DecodeStr+char((DecodeBase64[ord(B1)] shl 2)
or (DecodeBase64[ord(B2)] shr 4));
if B3<>'=' then
DecodeStr:=DecodeStr+char((DecodeBase64[ord(B2)] shl 4)
or (DecodeBase64[ord(B3)] shr 2));
if B4<>'=' then
DecodeStr:=DecodeStr+char((DecodeBase64[ord(B3)] shl 6)
or DecodeBase64[ord(B4)]);
end;{end of for}

Str:=DecodeStr;
end;

Exports
Base64_Encode index 0,
Base64_Decode index 1;

begin
end.
 
library Base64lib;

{ 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. }

看看上面的注释
用PChar不要用string
 
uses
sharemem
 
用PChar不要用string
 
传字符串(string)很容易出错的,建议pChar
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
830
import
I
I
回复
0
查看
596
import
I
I
回复
0
查看
641
import
I
I
回复
0
查看
645
import
I
后退
顶部