D7写的Dll,D2009如何调用呢?
dll 代码是:
library Code;uses
SysUtils,AES,
Classes;function EnCode(Str,Key:String):String;stdcall;
begin
result:=EncryptString(Str, Key);
end;function DeCode(Str,Key:String):String;stdcall;
begin
result:=DecryptString(Str, Key);
end; exports
Encode, DeCode;{$R *.res}begin
end.D2009中 用以下两个调用报错:function EnCode(Str, Key:String):String;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:String):String;stdcall;external 'Code.dll' ;
如何解决?
光 引用时候变成
function EnCode(Str, Key:AnsiString):AnsiString;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:AnsiString):AnsiString;stdcall;external 'Code.dll'
不行的。
dll 代码是:
library Code;uses
SysUtils,AES,
Classes;function EnCode(Str,Key:String):String;stdcall;
begin
result:=EncryptString(Str, Key);
end;function DeCode(Str,Key:String):String;stdcall;
begin
result:=DecryptString(Str, Key);
end; exports
Encode, DeCode;{$R *.res}begin
end.D2009中 用以下两个调用报错:function EnCode(Str, Key:String):String;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:String):String;stdcall;external 'Code.dll' ;
如何解决?
光 引用时候变成
function EnCode(Str, Key:AnsiString):AnsiString;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:AnsiString):AnsiString;stdcall;external 'Code.dll'
不行的。
function EnCode(Str,Key:String):String;stdcall;
begin
result:=EncryptString(Str, Key);
end; 因为EncryptString是aes单元的固定函数, 参数和result都是string的,没法变,dll函数应该怎么定义呢?
那比如 dll 中
function EnCode(Str,Key:pchar):pchar;stdcall;
一定可以的,目前我寫的dll都改成pchar了,沒有出錯過,請參考謝謝。
function EnCode(Str,Key:pchar):pchar;stdcall;
begin
result:=pchar(EncryptString(Str, Key));
end;function DeCode(Str,Key:pchar):pchar;stdcall;
begin
result:=PChar(DecryptString(Str, Key));
end;
D7 中 主程序写
function EnCode(Str, Key:pchar):pchar;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:pchar):pchar;stdcall;external 'Code.dll' ;
begin
edt2.Text:=EnCode(pchar(edt1.Text), '123');
edt3.Text:=DeCode(pchar(edt2.Text), '123');
end;没有问题,都正确。但是主程序代码挪到d2009中编译,也能运行,不过edt2.text变成了: ,edt3解密不对 这又是啥原因呢?
library Code;uses
SysUtils,AES,
Classes;function EnCode(Str,Key:pansichar):pansichar;stdcall;
begin
result:=pansichar(EncryptString(Str, Key));
end;function DeCode(Str,Key:pansichar):pansichar;stdcall;
begin
result:=pansichar(DecryptString(Str, Key));
end;
exports
Encode, DeCode;{$R *.res}begin
end.
D2009中改成了
function EnCode(Str, Key:PansiChar):PansiChar;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:PansiChar):PansiChar;stdcall;external 'Code.dll' ;
此时不管加密的是什么字符,只能对第一个字节加密,如加密123,结果是对1的加密结果。
而解密就会报错的。改成
function EnCode(Str, Key:pwidechar):pwidechar;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:pwidechar):pwidechar;stdcall;external 'Code.dll' ;
加解密都不报错,但也是只能对第一个字符进行加解密。
vRetStr : Array[0..255-1] of AnsiChar;
vEncryptStr : String; vEncryptStr := 'test';
strpcopy(vRetStr ,vEncryptStr); 再返回。