[Lazarus] Windows Registry, how do I read data from rdExpandString

Alexander Hofmann alexander.hofmann at new-h.de
Tue Feb 6 13:35:28 CET 2018


Hi,

as stated in the MSDN article you cited, the REG_EXPAND_SZ type is a
hint to the API/API-User, that the string (stored as any other string
either 2-byte or 1-byte depending in the Version) might contain
environment variables, that need to be expanded before use.

Respective M$ PowerShell, REG.exe versions etc. honor this hint and
expand the variables before returning you the results...

Am 06.02.2018 um 11:15 schrieb Bo Berglund via Lazarus:
>
> If I use Reg.ReadString method on a REG_EXPAND_SZ item which I have
> found in the user settings in the registry for CVSNT I get back a
> string looking like this:
> C:\Programs\cvsnt\cvsnt-default.pem
Does it look like this in Regedit.exe? I think yes in this case. The
REG_EXPAND_SZ was "misused" here, because the next hex data below
translates to the same text. The intended use of REG_EXPAND_SZ would be
to store "%programfiles%\cvsnt\cvsnt-default.pem" instead - so that on
internationalized (pre-win7/vista) systems it might expand to e.g.
"c:\Programme\"...

> But if I export the registry key with RegEdit I get this instead:
> "CertificateFile"=hex(2):43,00,3a,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,\
>
> 6d,00,73,00,5c,00,63,00,76,00,73,00,6e,00,74,00,5c,00,63,00,76,00,73,00,6e,\
>
> 00,74,00,2d,00,64,00,65,00,66,00,61,00,75,00,6c,00,74,00,2e,00,70,00,65,00,\
>   6d,00,00,00
See e.g.
https://social.msdn.microsoft.com/Forums/en-US/6bbef2d0-425d-4fb7-8ced-29ff18b91698/regexpandsz-to-regsz-question?forum=vblanguage
REG_EXPAND_SZ has to be encoded in Hex to be compatible with old Regedit
versions / to be sure it is stored with the correct datatype.
> Can someone explain how it works? And why there is no TRegistry class
> method for fetching the REG_EXPAND_SZ data type?
That I don't know - but according to
https://msdn.microsoft.com/de-de/library/windows/desktop/ms724884(v=vs.85).aspx,
you are required to do the conversion yourself, i.e. call
ExpandEnvironmentStrings to get a usable path. But - you should try
reading a "correct" REG_EXPAND_SZ value first, or look at the code of
TRegistry, to see if there is any conversion going on at all.

As for displaying - I would suggest displaying the "original" value
(without expansion) alongside with a "current reading" e.g. containing
the expanded string for the current system/user.

Greetings,
Alex


More information about the Lazarus mailing list