在delphi dll的地址访问冲突

时间:2016-11-12 13:53:33

标签: delphi

我的delphi(XE 10)dll有时会出错。

我在第2页得到“地址上的访问权限违规”。

  

步骤2模块中地址0AC95985的访问违规...读取   地址FFFFFFFC,高(a)= 31

我的delphi dll

Library hash_sha256;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  System.SysUtils,
  System.Classes,
  System.Hash,
  Vcl.Dialogs;

{$R *.res}

function MyHash(AKey, AData: PAnsiChar): Pchar; stdcall;
var
  a: TArray<byte>;
  i: integer;
  s: ShortString;
begin
  try
    s:='';
    Result:='';

    try
      a:=THashSHA2.GetHMACAsBytes(String(AData), String(AKey));
    except
      on E : Exception do
         ShowMessage('Step1'+E.Message);
    end;


    try
      for i:=0 to high(a) do
        s:=s+PChar(inttohex(a[i],2));
    except
      on E : Exception do
        ShowMessage('Step2 '+E.Message+', high(a)='+IntToStr(high(a)));
    end;

    try
      Result:=Pchar(s+'');
    except
      on E : Exception do
        ShowMessage('Step3'+E.Message);
    end;

  except
    on E : Exception do
      begin
        Result:=Pchar(AnsiString(E.Message));
      end;
  end;
end;

exports MyHash name 'hash_sha256';

begin
end.

谁能帮助我?

2 个答案:

答案 0 :(得分:2)

在步骤2中,您尝试通过PWideChar将UnicodeString附加到Ansi ShortString。 FFFFFFFC是-4的十六进制,这意味着编译器试图通过nil指针访问前导字符串/数组头。

这很可疑,但是在第3步你将ShortString输入到PWideChar,这是完全错误的。你真的需要停止将Ansi和Unicode混合在一起。

首先返回指向局部变量的指针也是错误的。当函数退出时,变量被释放,指针指向无效的内存。更安全的选择是使调用者在一个已分配的缓冲区中传递,该缓冲区根据需要填充该函数。

尝试更像这样的事情:

Library hash_sha256;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  System.SysUtils,
  System.Classes,
  System.Hash,
  Vcl.Dialogs;

{$R *.res}

function MyHash(AKey, AData: PAnsiChar; OutStr: PChar; OutLen: Integer): Integer; stdcall;
var
  a: TArray<Byte>;
  i: Integer;
  s: String;
begin
  Result := 0;
  try
    try
      a := THashSHA2.GetHMACAsBytes(AnsiString(AData), AnsiString(AKey));
    except
      on E : Exception do begin
        E.Message := 'Step1 ' + E.Message;
        raise;
      end;
    end;

    try
      s := '';
      for i := Low(a) to High(a) do
        s := s + IntToHex(a[i], 2);
    except
      on E : Exception do begin
        E.Message := 'Step2 ' + E.Message + ', high(a)=' + IntToStr(High(a));
        raise;
      end;
    end;

    if OutStr <> nil then
    begin
      try
        StrPLCopy(OutStr, s, OutLen);
      except
        on E : Exception do begin
          E.Message := 'Step3 ' + E.Message;
          raise;
        end;
      end;    
      Result := min(Length(s), OutLen);
    end else
      Result := Length(s)+1;        
  except
    on E : Exception do
      ShowMessage(E.Message);
  end;
end;

exports
  MyHash name 'hash_sha256';

begin
end.

答案 1 :(得分:0)

正如其他人所说,不要返回PChar(或PAnsiChar),特别是如果它是指向本地字符串的指针则不会。该字符串将在函数末尾删除,P(Ansi)Char变为无效。因此AV。

请改为:

function MyHash(AKey, AData: PAnsiChar; AResult: PAnsiChar; AResultLen: Integer): Boolean; stdcall;
var
  s: AnsiString;
  i: Integer;
  a: TArray<Byte>;
begin

  ...

  for i := 0 to High(a) do
    s := s + AnsiString(IntToHex(a[i], 2));

  except
    Exit(False);
  end;

  StrLCopy(AResult, PAnsiChar(s), AResultLen);
  Exit(True); 
end; 

你这样使用它:

var
  LargeBuffer: array[Word] of AnsiChar;
  // etc.
begin
  if MyHash(yourKey, yourData, @LargeBuffer[0], Length(LargeBuffer)) then
    ...

或动态:

var
  LargeBuffer: array of AnsiChar;
  ...
begin
  SetLength(LargeBuffer, 65536);
  if MyHash(yourKey, yourData, @LargeBuffer[0], Length(LargeBuffer)) then
    ...

或使用字符串:

var
  a: AnsiString;
  ...
begin
  SetLength(a, High(Word));
  if MyHash(yourKey, yourData, PAnsiChar(a), Length(a)) then
  begin
    a := PAnsiChar(a); // Updates a to the proper length (internally uses StrLen).
    // Use a here.