问题
This is my first time installing the libraries for Lockbox. I downloaded version 3.4.3 from sourceforge and have Delphi 7. The first step is to get this sucker to compile under Delphi 7 and it's been hell. I do hope that the components are easier to use once installed.
Ok. I have a unit that looks like this.
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.
BTW, the compatibility unit defines TBytes as TBytes = packed array of byte;
Delphi 7 chokes on the TEncoding because it only exists in D2009+. What do I replace this function with?
回答1:
String
is an 8bit AnsiString
in Delphi 7. Simply allocate the TBytes
to the Length()
of the string and Move()
the string content into it:
function AnsiBytesOf(const S: AnsiString): TBytes;
begin
SetLength(Result, Length(S) * SizeOf(AnsiChar));
Move(PChar(S)^, PByte(Result)^, Length(Result));
end;
If you want to be politically correct and match what TEncoding.GetBytes()
does, you would have to convert the String
to a WideString
and then use the Win32 API WideCharToMultiBytes()
function to convert that to bytes:
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;
回答3:
You can get LB 3.5 here:
http://lockbox.seanbdurkin.id.au/Grok+TurboPower+LockBox
Try 3.5 instead.
来源:https://stackoverflow.com/questions/26000177/how-do-i-install-lockbox-3-into-delphi-7