2

オンラインで見つけた Delphi 7 の文字列デコーダ関数(「復号化」)に問題があります。デコードするには、文字列と 1 から 120 までの 4 つの数値が必要です。関数を実行可能ファイルに入れて実行すると、うまく機能します。ただし、これを DLL 内で実行したいと考えています。追加の関数を使用して、呼び出し元のタスクから必要な値を受け取り、そこから復号化関数を呼び出しています。何らかの理由で指を置くことができません.dllがdecrypt-functionを呼び出すたびに、ホストアプリがクラッシュします。ここ数時間、これを機能させるために思いつく限りのことを試みましたが、成功しませんでした。私は一般的にDelphiとPascalに慣れていないので、明らかなことを見落としているかもしれません。いずれにせよ、私は迷っています。どんな助けでも大歓迎です。

編集:クラッシュ時にポップアップするWindowsエラーは次のとおりです。

  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

私が使用するコードは次のとおりです。

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.
4

2 に答える 2

5

このコードはすべて間違っています:

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;

ローカル変数Decryptが割り当てられることはありません。したがって、 を呼び出すとDecrypt、何でも発生する可能性があります。callfunction実際の の後に表示されるように、ユニットの下部に移動する必要がありますDecrypt。そして、関数ポインタ変数を削除する必要があります。

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;

stringまた、エクスポートされた DLL 関数でパラメーター (または実際には戻り値) として使用しないでください。stringこれは、同じ実装と同じメモリ マネージャーを使用するインターフェイスの両側に依存するため、相互運用には有効な型ではありません。Decryptインターフェースをエクスポートする場合は、別の方法でインターフェースを定義する必要があります。または、その関数をまったくエクスポートしないでください。確実に知ることは私には不可能です。私の推測では、呼び出し元のコードからcallfunctionではなく、実際に呼び出していると思いDecryptます。いずれにせよ、現状ではエクスポートしてはいけませんDecrypt

もう1つのポイント。モジュール インターフェイスに関する質問をするときは、常にインターフェイスの両側を表示する必要があります。ここでは DLL を示していますが、DLL を呼び出すコードは示していません。エラーはそこにある可能性があります。確かに、私が指摘したエラーに加えて、そこにエラーがある可能性があります。

于 2012-04-21T19:56:23.083 に答える
0

文字列操作コードが exe では示されているように機能するが、DLL からでは機能しない場合は、最初に使用する項目として DLL に Delphi メモリ マネージャーがあることを確認します。

uses
  ShareMem

これは Dephi exe では暗黙的ですが、DLL では暗黙的です。

これは、Delphi 6 の DLL と文字列で常に好まれる問題ですが、Delphi 7 でも適用できるかどうかはわかりません。

于 2012-04-21T20:02:55.637 に答える