【问题标题】:How do I install LockBox 3 into Delphi 7?如何将 LockBox 3 安装到 Delphi 7 中?
【发布时间】:2014-09-23 16:30:12
【问题描述】:

这是我第一次为 Lockbox 安装库。我从 sourceforge 下载了 3.4.3 版并拥有 Delphi 7。第一步是让这个傻瓜在 Delphi 7 下编译,这简直就是地狱。我确实希望这些组件在安装后更易于使用。

好的。我有一个看起来像这样的单元。

unit uTPLb_StrUtils;

interface

uses
  SysUtils, uTPLb_D7Compatibility;

function AnsiBytesOf(const S: string): TBytes;

implementation

function AnsiBytesOf(const S: string): TBytes;
begin
//compiler chokes here
  **Result := TEncoding.ANSI.GetBytes(S);**
end;

end.

顺便说一句,兼容单元将 TBytes 定义为 TBytes = 字节打包数组;

Delphi 7 扼杀了 TEncoding,因为它只存在于 D2009+ 中。我用什么替换这个函数?

【问题讨论】:

    标签: delphi lockbox-3


    【解决方案1】:

    String在Delphi 7中是一个8位的AnsiString。只需将TBytes分配给字符串的Length(),将Move()字符串内容分配到其中:

    function AnsiBytesOf(const S: AnsiString): TBytes;
    begin
      SetLength(Result, Length(S) * SizeOf(AnsiChar));
      Move(PChar(S)^, PByte(Result)^, Length(Result));
    end;
    

    如果您想政治正确并匹配TEncoding.GetBytes() 所做的,您必须将String 转换为WideString,然后使用Win32 API WideCharToMultiBytes() 函数来将其转换为字节:

    function AnsiBytesOf(const S: WideString): TBytes;
    var
      Len: Integer;
    begin
      Result := nil;
      if S = '' then Exit;
      Len := WideCharToMultiByte(0, 0, PWideChar(S), Length(S), nil, 0, nil, nil);
      if Len = 0 then RaiseLastOSError;
      SetLength(Result, Len+1);
      WideCharToMultiByte(0, 0, PWideChar(S), Length(S), PAnsiChar(PByte(Result)), Len, nil, nil);
      Result[Len] = $0;
    end;
    

    【讨论】:

      【解决方案2】:
      function Quux(const S: AnsiString): TBytes;
      var
        Count: Integer;
      begin
        Count := Length(S) * SizeOf(AnsiChar);
        {$IFOPT R+}
        if Count = 0 then Exit; // nothing to do
        {$ENDIF}
        SetLength(Result, Count);
        Move(S[1], Result[Low(Result)], Count);
      end;
      

      【讨论】:

      • 如果AnsiString 为空,访问S[1] 将崩溃并出现边界错误。
      • @RemyLebeau,刚刚用我的 XE2 测试过,但没有,嗯。
      • 在我的 XE2 上,如果启用范围检查,访问空白 AnsiStringS[1] 会引发 ERangeError,否则在从地址 $00000000 读取时会引发 EAccessViolation(这很有意义空白字符串是nil 指针)。
      • @RemyLebeau,虽然范围检查错误(如果启用)非常有意义,但访问冲突不会,因为当 Count 为零时,无法访问 nil^
      • 你是对的,只有在实际读取内存时才会发生 AV。当我进行S[1] 测试时,我没有使用Move()。尽管如此,Quux() 应该处理Count=0 的情况,这样它就可以在启用范围检查时避免ERangeCheck 错误。
      【解决方案3】:

      您可以在这里获得 LB 3.5:

      http://lockbox.seanbdurkin.id.au/Grok+TurboPower+LockBox

      改用 3.5。

      【讨论】:

        猜你喜欢
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-09
        • 2010-09-11
        • 2011-10-14
        • 2012-06-14
        • 2011-12-12
        相关资源
        最近更新 更多