如何将 LockBox 3 安装到 Delphi 7 中?

Deb*_*bie 3 delphi lockbox-3

这是我第一次安装 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.
Run Code Online (Sandbox Code Playgroud)

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

Delphi 7 对 TEncoding 感到窒息,因为它只存在于 D2009+ 中。我用什么来替换这个功能?

Rem*_*eau 5

StringAnsiString在Delphi 7中是8位。只需将 分配TBytesLength()字符串并将Move()字符串内容分配到其中即可:

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

如果您想在政治上正确并与实际情况相符TEncoding.GetBytes(),则必须将 转换String为 a WideString,然后使用 Win32 APIWideCharToMultiBytes()函数将其转换为字节:

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;
Run Code Online (Sandbox Code Playgroud)