我正在尝试为我们在 Delphi 2007 中的项目制作一个使用 XE7 中的 TAmazonStorageService 组件的 dll。
但是,我有几个内存泄漏,或者数组的所有索引都返回最后一个字符串。
这是我的功能:
function ListBuckets(const PrivateKEY: PAnsiChar; const PublicKEY: PAnsiChar;
out ArrayBuckets: TArray<PAnsiChar>;
out Error: PAnsiChar): Boolean; stdcall;
var
AmazonConn: TAmazonConnectionInfo;
AmazonS3: TAmazonStorageService;
ResponseInfo: TCloudResponseInfo;
BucketsList: TStrings;
I: Integer;
begin
Result := True;
AmazonConn := TAmazonConnectionInfo.Create(nil);
AmazonConn.AccountName := string(PublicKEY); { AccessKeyID }
AmazonConn.AccountKey := string(PrivateKEY); { SecretAccessKeyID }
AmazonS3 := TAmazonStorageService.Create(AmazonConn);
ResponseInfo := TCloudResponseInfo.Create;
Error := '';
try
BucketsList := AmazonS3.ListBuckets(ResponseInfo);
if not Assigned(BucketsList) then
begin
Result := False;
Error := PAnsiChar(AnsiString(ResponseInfo.StatusMessage));
end
else
begin
SetLength(ArrayBuckets, BucketsList.Count);
for I := 0 to BucketsList.Count - 1 do
ArrayBuckets[I] := PAnsiChar(AnsiString(BucketsList.Strings[I]));
end;
finally
BucketsList.Free;
ResponseInfo.Free;
AmazonS3.Free;
AmazonConn.Free;
end;
end;
exports ListBuckets;
这就是我如何“尝试”使用这个功能。需要在 Delphi XE7 和 Delphi 2007 中工作。
type
TAnsiCharArray = array of PAnsiChar;
function ListBuckets(const PrivateKEY: PAnsiChar; const PublicKEY: PAnsiChar;
out ArrayBuckets: TAnsiCharArray;
out MensagemErro: PAnsiChar): Boolean; stdcall;
external 'Test.dll';
function ListBucketsDelphi(const PrivateKEY: string; const PublicKEY: string;
out StringListBuckets: TStringList;
out Error: string): Boolean;
var
vAnsiPrivateKEY: PAnsiChar;
vAnsiPublicKEY: PAnsiChar;
vAnsiError: PAnsiChar;
vStringArray: TAnsiCharArray;
I: Integer;
begin
{$IFDEF UNICODE}
vAnsiPrivateKEY := PAnsiChar(RawByteString(PrivateKEY));
vAnsiPublicKEY := PAnsiChar(RawByteString(PublicKEY));
{$ELSE}
vAnsiPrivateKEY := PAnsiChar(PrivateKEY);
vAnsiPublicKEY := PAnsiChar(PublicKEY);
{$ENDIF}
//StringListBuckets need to be created before...
Result := ListBuckets(vAnsiPrivateKEY, vAnsiPublicKEY, vStringArray, vAnsiMensagemErro);
if not (Result) then
Error := string(vAnsiError);
try
if Result then
begin
for I := Low(vStringArray) to High(vStringArray) do
StringListBckets.Append(vStringArray[I]);
end;
except
Result := False;
Error := '"StringListBuckets" not created.';
end;
end;
我搜索了一下,但没有发现有关在 unicode dll 中返回数组的信息,或者我搜索错误。
有人可以帮忙吗?
提前致谢。