ShLwApi.StrFormatByteSize e Delphi 2010 Unicode
-
08-07-2019 - |
Domanda
Qualcuno può aiutarmi a risolvere questo problema:
{$IFDEF UNICODE}
function FormatStringByteSize( TheSize: Cardinal ): string;
{ Return a cardinal as a string formated similar to the statusbar of Explorer }
var
Buff: string;
Count: Integer;
begin
Count := Length(Buff);
FillChar(Buff, Count, 0);
ShLwApi.StrFormatByteSize( TheSize, PWideChar(Buff), Length( Buff ) * SizeOf( WideChar ) );
Result := Buff;
end;
{$ENDIF}
Soluzione
Almeno in Delphi 2009 (non posso testare nella versione 2010 perché non ce l'ho) la funzione StrFormatByteSize ()
è un alias della versione Ansi ( StrFormatByteSizeA ()
), non alla versione con caratteri estesi ( StrFormatByteSizeW ()
) come per la maggior parte delle altre funzioni dell'API di Windows. Pertanto, è necessario utilizzare direttamente la versione wide char, anche per le versioni precedenti di Delphi, per poter lavorare con file (sistema) di dimensioni superiori a 4 GB.
Non è necessario un buffer intermedio e puoi sfruttare il fatto che StrFormatByteSizeW ()
restituisce un puntatore al risultato convertito come PWideChar
:
{$IFDEF UNICODE}
function FormatStringByteSize(ASize: int64): string;
{ Return a cardinal as a string formatted similar to the status bar of Explorer }
const
BufLen = 20;
begin
SetLength(Result, BufLen);
Result := StrFormatByteSizeW(ASize, PChar(Result), BufLen);
end;
{$ENDIF}
Altri suggerimenti
Devi prima impostare la durata del buff. (Buff buff = 0)
Poi
- Cambia TheSize in Int64 - questo ti serve per le dimensioni > 4 GB comunque.
- Forse cambiare la chiamata a StrFormatByteSizeW (le diciture Delphi "dovrebbero essere state fatte in D2009 +)
- Nonostante il nome, FillChar prevede che la dimensione sia in byte, non in caratteri. Tuttavia, ciò non influirà sul risultato.
function FormatStringByteSize( TheSize: int64 ): string;
// Return an Int64 as a string formatted similar to the status bar of Explorer
var
Buff: string;
begin
SetLength(Buff, 20);
ShLwApi.StrFormatByteSizeW( TheSize, PWideChar(Buff), Length(Buff));
Result := PChar(Buff);
end;
Al momento non posso provarlo in D2009 / 10 poiché non ho ancora iniziato il passaggio a Unicode (prossimo progetto!) Funziona in D2006 con WideString.