Note that there are some explanatory texts on larger screens.

plurals
  1. POHow convert null-terminated string to an AnsiString?
    primarykey
    data
    text
    <p>I have some code that compiles fine with D7 but fails with D2010. Obviously it is an Unicode issue:</p> <p>The compile error is: E2251 Ambiguous overloaded call to 'StrPas'</p> <p>Here is the whole procedure:</p> <pre><code>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)) &gt; 0 then begin Size := GetFileVersionInfoSize(FileName, VerHandle); if Size &gt; 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; </code></pre> <p>The doc for StrPas says</p> <pre><code>function StrPas(const Str: PAnsiChar): AnsiString; overload; </code></pre> <p>This function is provided fasor backwards compatibility only. To convert a null-terminated string to an AnsiString or native Delphi language string, use a typecast or an signment. </p> <p>So the question is should I remove all calls to StrPas ? The only way I make this to compile is to do a hardcast to PAnsi char like:</p> <pre><code>LabelProductName.Caption := StrPas(PAnsiChar(Data)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload