从DLL返回字符串的问题,搞了好几天了,都没解决,请各位高手来指点一下.(100分)

W

wyjpg

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是DLL中的一个函数.
function DecryptPW(InStr: Pchar): Pchar;stdcall;
var
I: Integer;
j: Integer;
vInStr,vOutStr:String;
vKey:Integer;
begin
vKey:=PasswordKey;
vInStr:=InStr;
vOutStr:='';
for i:=1 to (length(vInStr) div 2) do
begin
j:=(Integer(vInStr[2*i-1])-65)*26;
j:=j+(Integer(vInStr[2*i])-65);
vOutStr:=vOutStr + Char(j);
end;
vInStr:=vOutStr;
for I := 1 to Length(vInStr) do
begin
vOutStr := char(byte(vInStr) xor (vKey shr 8));
vKey := (byte(vInStr) + vKey) * C1 + C2;
end;
showmessage('outstr='+voutstr);
Result:=Pchar(vOutStr);
end;
 
W

wyjpg

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是主程序调用.
function DecryptPW(InStr:String;Var OutStr:String):Boolean;
type
PublicDll=function ( InStr: Pchar): Pchar;stdcall;
var
DllHandle:THandle;
Dllfarproc:Tfarproc;
begin
Result:=False;
DllHandle:=LoadLibrary('PubFun.dll');
if DllHandle>32 then
begin
try
Dllfarproc:=GetProcAddress(DllHandle,'DecryptPW');
if DllfarProc<>Nil then
begin
OutStr:=PublicDll(Dllfarproc)(Pchar(InStr));
Result:=True;
end;
except
FreeLibrary(DllHandle);
end;
end
else
MessageBox(Application.Handle,'调用动态库失败!','提示',MB_IconInformation+MB_Ok);
end;
为什么在主程序调用解密时,有时能返回正确的密码,有时不能?
 
H

hfghfghfg

Unregistered / Unconfirmed
GUEST, unregistred user!
把你dll的 vOutStr 改成全局变量
你现在是局部变量
 
S

szhcracker

Unregistered / Unconfirmed
GUEST, unregistred user!
是不是和参数有关?你调用前先初始化一下试试。记住:PChar的实质是一个指针。
 
Z

zzutrain

Unregistered / Unconfirmed
GUEST, unregistred user!
因为你调用的返回指是pchar , 当遇到#0时,就结束了。 而你加密的内容有可能会生成#0字符. 所以建议你转16进制后返回, 就不会出问题了。
 
W

wyjpg

Unregistered / Unconfirmed
GUEST, unregistred user!
to zzutrain
能否讲详细点呢?
 
W

wyjpg

Unregistered / Unconfirmed
GUEST, unregistred user!
to hfghfghfg
谢谢你,你的方法有效!能否解释一下,为什么局部变量不行,而全局变量则行?
 
H

hfghfghfg

Unregistered / Unconfirmed
GUEST, unregistred user!
因为 Result:=Pchar(vOutStr);
当这个 函数 执行完
局部变量 vOutStr 可能销毁
那么 Result 指向 的vOutStr 在内存中的残骸
有可能是 原来的 vOutStr 也有可能 是其他啥的
 
W

wyjpg

Unregistered / Unconfirmed
GUEST, unregistred user!
哦,难怪返回的结果怪怪的.谢谢了!
 
W

wyjpg

Unregistered / Unconfirmed
GUEST, unregistred user!
多人接受答案了。
 

Similar threads

I
回复
0
查看
530
import
I
I
回复
0
查看
528
import
I
I
回复
0
查看
744
import
I
I
回复
0
查看
795
import
I
I
回复
0
查看
673
import
I
顶部