- Offizieller Beitrag
Interaktionen mit der Registry sind ja ständig erforderlich. Es gibt dafür auch ein Modul "winregistry" (Dokumentation).
Ich habe damit ergänzend mein Modul "registry" mit folgenden Funktionen erstellt:
- AppPathGet
Returns the installation path of an passed application
- VolatileEnvironmentGet
Returns a tuple with all volatile environment values
- EnvironmentGet
Returns the value of a passed environment name
- EnvironmentValueNamesGet
Returns a list with all environment names
- CountKeyValues
Returns the number of key-value pairs that are associated with the specified
registry key. Does not count default key-value pair.
- ListNameValuePairs
Fills a table with all name-value-pairs for a passed registry key.
The table to receive the values must passed too.
- RegistryGetValueType
Gets the type of the value for a passed registry key.
Die Funktion "RegistryGetValueType" gefällt mir aber nicht so richtig, weil sie auf der Basis von Try&Error arbeitet, also eher eine Notlösung darstellt. Dazu hatte ich auch meine bisher einzige Anfrage im Nim-Forum erstellt - leider ohne Resonanz.
Vielleicht habt ihr ja eine Idee, wie man das etwas eleganter gestalten kann.
Code
#------------------------------------------------------------------------------
# Gets the type of the value for a passed registry key.
#------------------------------------------------------------------------------
# example:
# var
# h: RegHandle
#
# try:
# h = open(r"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control", samRead or samQueryValue)
# echo RegistryGetValueType(h, "PreshutdownOrder") # output --> REG_MULTI_SZ
# except RegistryError:
# echo "Registry Open Error"
# finally:
# close(h)
#------------------------------------------------------------------------------
proc RegistryGetValueType*(h: RegHandle, key: string): string =
var
iErr = 0
readStr: string
readMultiStr = newSeq[string]()
readInt32: int32
readInt64: int64
readBinary = newSeq[byte]()
try:
readMultiStr = h.readMultiString(key) # REG_MULTI_SZ
except RegistryError:
iErr = 1
if iErr == 1:
try:
readStr = h.readExpandString(key) # REG_EXPAND_SZ
except RegistryError:
iErr = 2
if iErr == 2:
try:
readStr = h.readString(key) # REG_SZ
except RegistryError:
iErr = 3
if iErr == 3:
try:
readInt32 = h.readInt32(key) # REG_DWORD
except RegistryError:
iErr = 4
if iErr == 4:
try:
readInt64 = h.readInt64(key) # REG_QWORD
except RegistryError:
iErr = 5
if iErr == 5:
try:
readBinary = h.readBinary(key) # REG_BINARY
except RegistryError:
iErr = 6
case iErr
of 6:
return "UNKNOWN_TYPE"
of 5:
return "REG_BINARY"
of 4:
return "REG_QWORD"
of 3:
return "REG_DWORD"
of 2:
return "REG_SZ"
of 1:
return "REG_EXPAND_SZ"
of 0:
return "REG_MULTI_SZ"
else:
discard
#-------------------------------------------------------- /RegistryGetValueType
Alles anzeigen