I have a problem with a Delphi 7 string-decoder function (“Decrypt”) I found online. It takes a string and four numeric values between 1 and 120 to decode it. Putting the function within an executable and running it works great. However, I want to run this within a DLL. I’m using an extra function to receive the necessary values from the calling task and call the decrypt function from there. For some reason I can’t put my finger on, the host app crashes every time the dll calls the decrypt-function. For the last few hours I have been trying everything I could come up with to get this to work, but without success. I’m new to Delphi and Pascal in general, so may be I just overlooked something obvious. Either way, I’m lost. Any help would be greatly appreciated.
Edit: Here’s the windows error that pops up upon crash:
Fault Module Name: StackHash_0a9e
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 00000000
Exception Code: c0000005
Exception Offset: 00000000
OS Version: 6.1.7600.2.0.0.256.1
Locale ID: 1031
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
Here’s the code that I use:
library decrypt_test_dll;
uses
SysUtils,
Classes,
Dialogs;
{$R *.res}
function callfunction(externalstring, value1, value2, value3, value4: PAnsiChar):integer; cdecl;
var
convkey1, convkey2, convkey3, convkey4 : string;
convstring, decodedstring : string;
Decrypt : function(Text : string; Key1, Key2, Key3, Key4 : Integer) : string;
begin
convkey1 := value1;
convkey2 := value2;
convkey3 := value3;
convkey4 := value4;
convstring := externalstring;
decodedstring := Decrypt(externalstring, strtoint(convkey1), strtoint(convkey2), strtoint(convkey3), strtoint(convkey4));
showmessage(decodedstring);
end;
function Decrypt(Text : string; Key1, Key2, Key3, Key4 : Integer) : string;
var
BufS, Hexa1, Hexa2 : string;
BufI, BufI2, Divzr, Sc, Sl, Num1, Num2, Num3, Num4, Res1, Res2, Res3, Res4 : Integer;
begin
showmessage('within decryption function');
Sl := Length(Text);
Sc := 0;
BufS := '';
if (Key1 in [1 .. 120]) and (Key2 in [1 .. 120]) and (Key3 in [1 .. 120]) and (Key4 in [1 .. 120]) then
begin
Divzr := Key1 * Key4;
BufI2 := Key3 * Key2;
Divzr := Divzr - BufI2;
if Divzr = 0 then
begin
Result := '';
Exit;
end;
end
else
begin
Result := '';
Exit;
end;
repeat
for BufI := 1 to 4 do
begin
Inc(Sc);
Hexa1 := IntToHex(Ord(Text[Sc]), 2);
Inc(Sc);
Hexa2 := IntToHex(Ord(Text[Sc]), 2);
if Hexa1 = 'FF' then
begin
Hexa1 := '00';
Hexa2 := '00';
end;
if Hexa1 = 'FE' then Hexa1 := '00';
if Hexa1 = 'FD' then
begin
Hexa1 := Hexa2;
Hexa2 := '00';
end;
case BufI of
1 : Res1 := StrToInt('$' + Hexa1 + Hexa2);
2 : Res2 := StrToInt('$' + Hexa1 + Hexa2);
3 : Res3 := StrToInt('$' + Hexa1 + Hexa2);
4 : Res4 := StrToInt('$' + Hexa1 + Hexa2);
end;
end;
BufI := Res1 * Key4;
BufI2 := Res2 * Key3;
Num1 := BufI - BufI2;
Num1 := Num1 div Divzr;
BufI := Res2 * Key1;
BufI2 := Res1 * Key2;
Num2 := BufI - BufI2;
Num2 := Num2 div Divzr;
BufI := Res3 * Key4;
BufI2 := Res4 * Key3;
Num3 := BufI - BufI2;
Num3 := Num3 div Divzr;
BufI := Res4 * Key1;
BufI2 := Res3 * Key2;
Num4 := BufI - BufI2;
Num4 := Num4 div Divzr;
BufS := BufS + Chr(Num1) + Chr(Num2) + Chr(Num3) + Chr(Num4);
until Sc >= Sl;
Result := BufS;
end;
exports
Decrypt index 1,
callfunction index 2;
begin
end.
This code is all wrong:
The local variable
Decryptis never assigned to. So when you callDecrypt, anything can happen. You need to movecallfunctionto the bottom of the unit so that it appears after the realDecrypt. And you need to remove the function pointer variable.You also should not use
stringas a parameter (or indeed a return value) in an exported DLL function. It’s not a valid type for interop since it relies on both sides of the interface using the samestringimplementation, and the same memory manager. You need to find a different way to define theDecryptinterface, if you are going to export it. Or possibly you should not be exporting that function at all. Impossible for me to know with any certainty. My guess is that you are actually callingcallfunctionrather thanDecryptfrom the calling code. Anyway, in its current state, you must not exportDecrypt.One other point to make. When you ask a question that involves a module interface, you should always show both sides of the interface. Here you have shown the DLL, but not the code that calls into the DLL. The error could be there. Indeed, there could be an error there in addition to the errors I have pointed out.