J'ai du code qui compile bien avec D7 mais échoue avec D2010. De toute évidence, il est un problème Unicode:Comment convertir une chaîne terminée par un caractère nul en un AnsiString?
L'erreur de compilation est: E2251 Ambigu appel surchargé à 'StrPas'
est toute la procédure ici:
procedure GetVersionInfo;
type
PLangCharSetInfo = ^TLangCharSetInfo;
TLangCharSetInfo = record
Lang: Word;
CharSet: Word;
end;
var
FileName: array [0..260] of Char;
SubBlock: array [0..255] of Char;
VerHandle: Cardinal;
Size: Word;
Buffer: Pointer;
Data: Pointer;
DataLen: LongWord;
LangCharSetInfo: PLangCharSetInfo;
LangCharSetString: string;
begin
LabelComments.Caption := 'No version information for this program is available!';
{Get size and allocate buffer for VerInfo}
if GetModuleFileName(hInstance, FileName, SizeOf(FileName)) > 0 then
begin
Size := GetFileVersionInfoSize(FileName, VerHandle);
if Size > 0 then
begin
GetMem(Buffer, Size);
try
if GetFileVersionInfo(FileName, VerHandle, Size, Buffer) then
begin
{Query first language and that language blocks version info}
if VerQueryValue(Buffer, '\VarFileInfo\Translation', Pointer(LangCharSetInfo), DataLen) then
begin
LangCharSetString := IntToHex(LangCharSetInfo^.Lang, 4) +
IntToHex(LangCharSetInfo^.CharSet, 4);
if VerQueryValue(Buffer, StrPCopy(SubBlock, '\StringFileInfo\' + LangCharSetString + '\ProductName'), Data, DataLen) then
begin
LabelProductName.Caption := StrPas(Data);
Caption := LabelProductName.Caption;
end;
if VerQueryValue(Buffer, StrPCopy(SubBlock, '\StringFileInfo\' + LangCharSetString + '\FileVersion'), Data, DataLen) then
LabelVersion.Caption := StrPas(Data);
if VerQueryValue(Buffer, StrPCopy(SubBlock, '\StringFileInfo\' + LangCharSetString + '\LegalCopyright'), Data, DataLen) then
LabelCopyright.Caption := StrPas(Data);
if VerQueryValue(Buffer, StrPCopy(SubBlock, '\StringFileInfo\' + LangCharSetString + '\Comments'), Data, DataLen) then
LabelComments.Caption := StrPas(Data);
end;
end;
finally
FreeMem(Buffer, Size);
end;
end
end;
end;
Le doc pour StrPas dit
function StrPas(const Str: PAnsiChar): AnsiString; overload;
Cette fonction est fournie fasor rétrocompatibilité seulement. Pour convertir une chaîne terminée par un caractère NULL en une chaîne de caractères Delphi AnsiString ou natif , utilisez un transtypage ou une signature.
Donc la question est de savoir si je dois supprimer tous les appels à StrPas? La seule façon que je fais cela pour compiler est de faire un hardcast à Pansi ombles comme:
LabelProductName.Caption := StrPas(PAnsiChar(Data));
Merci pour votre réponse et l'exemple. J'ai posté toute la procédure maintenant. Je ne veux pas changer plus que nécessaire pour éviter d'introduire des bugs. –