Reputation: 552
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.
Upvotes: 2
Views: 1556
Reputation: 5695
If the string manipulation code works as shown in an exe but not from a DLL check that the DLL has the delphi memory manager as the FIRST uses item.
uses
ShareMem
This is implicitt in a Dephi exe, but not in its DLLs.
This is always a favorite problem with Delphi 6 DLLs and strings, cant say if its still applicable in Delphi 7 however!
Upvotes: 0
Reputation: 613572
This code is all wrong:
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;
The local variable Decrypt
is never assigned to. So when you call Decrypt
, anything can happen. You need to move callfunction
to the bottom of the unit so that it appears after the real Decrypt
. And you need to remove the function pointer variable.
function callfunction(externalstring, value1, value2, value3, value4: PAnsiChar):integer; cdecl;
var
convkey1, convkey2, convkey3, convkey4 : string;
convstring, decodedstring : 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;
You also should not use string
as 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 same string
implementation, and the same memory manager. You need to find a different way to define the Decrypt
interface, 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 calling callfunction
rather than Decrypt
from the calling code. Anyway, in its current state, you must not export Decrypt
.
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.
Upvotes: 5