Skip to content

Commit

Permalink
- RegValueKind and RegKeyRights are distinct uint32's now (fixe…
Browse files Browse the repository at this point in the history
…d warning about enums with holes).

- Replaced `RegistryError` with `OSError` for consistency with built-in `registry` package.
- Removed support for deprecated `taintmode` feature.
- Moved tests out of main file to ensure things are exported correctly.
- Fixed deprecation/unused variable warnings.
- Updated documentation.
- Nim 1.6.0 is now minimum supported version.
  • Loading branch information
miere43 committed May 20, 2022
1 parent 573b9a3 commit ce65d64
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 161 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
nimcache/
winregistry.html
winregistry.exe
*.exe
htmldocs/
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ finally:
## Tests
Run in command line:
```
$ nim c -r winregistry
$ nim c -r -d:useWinAnsi winregistry
$ nimble test
$ nimble testansi
```
You should see a "tests passed" message.
You should see a "tests passed" message. If you get `Access is denied` error, try running with administrator rights.

## Changelog
### 1.0.0
- `RegValueKind` and `RegKeyRights` are distinct `uint32`'s now (fixed warning about enums with holes).
- Replaced `RegistryError` with `OSError` for consistency with built-in `registry` package.
- Removed support for deprecated `taintmode` feature.
- Moved tests out of main file to ensure things are exported correctly.
- Fixed deprecation/unused variable warnings.
- Updated documentation.
- Nim 1.6.0 is now minimum supported version.

### 0.2.1
- Added "enumValueNames"
- Fixed missing dealloc in case of exception in "enumSubkeys"
Expand Down
4 changes: 2 additions & 2 deletions doc/modulespec.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Please do not use this module to save your applications settings, use it to interact with older programs which make use of registry, because it is slow (implemented as file system inside a file rather than database), not cross-platform, has unoblivious stuff like registry virtualization, virtual storages, different behavior across 32/64-bit systems. As alternative you can supply some kind of INI or XML file with your application or store it into platform-specific settings folder (take a look at `appdirs module <https://github.com/MrJohz/appdirs>`_).
Please do not use this module to save your applications settings, use it to interact with older programs which make use of registry, because registry is slow, not cross-platform, has unoblivious stuff like registry virtualization, virtual storages and different behavior across 32/64-bit systems. As alternative you can supply some kind of INI or XML file with your application.

Notes
-----

* When writing UTF-16 chars in ``useWinAnsi`` mode using `writeMultiString<#writeMultiString>`_ you will see garbage if you view that string in registry, but `readMultiString<#readMultiString>`_ will decode it correctly.
* Registry paths use backslashes ``(\)``, forwardslashes ``(/)`` are parsed as normal characters.
* Module procs' do not convert values from UTF-16 to UTF-8, howerer, when ``useWinAnsi`` defined, Windows handles conversion from UTF-16 strings to ASCII.
* If some procs throw ``RegistryError``, but everything seems okay, make sure registry handle have proper security rights.
* If some procs throw ``OSError``, but everything seems okay, make sure registry handle have proper security rights.
* Registry on MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724871(v=vs.85).aspx
* Note that registry entries may be stored separately for 32/64-bit applications: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724072(v=vs.85).aspx
* ``Read about Registry Virtualization``: https://msdn.microsoft.com/en-us/library/windows/desktop/aa965884(v=vs.85).aspx
Expand Down
2 changes: 2 additions & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
switch("path", "..")
switch("path", ".")
96 changes: 96 additions & 0 deletions tests/tester.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import sequtils, winregistry, winlean

echo "unicode: " & $useWinUnicode

var passed = true
var msg, stacktrace: string
var handle, innerHandle: RegHandle
try:
handle = createOrOpen("HKEY_LOCAL_MACHINE\\Software\\AAAnim_reg_test",
samRead or samWrite or samWow32)

# String
handle.writeString("StringValue", "StringData")
assert(handle.readString("StringValue") == "StringData")
handle.writeString("StringPathValue", "C:\\Dir\\File")
assert(handle.readString("StringPathValue") == "C:\\Dir\\File")

# Binary
handle.writeBinary("BinaryValue", [0xff.byte, 0x00])
let binaryData = handle.readBinary("BinaryValue")
assert(binaryData[0] == 0xff)
assert(binaryData[1] == 0x00)

# Int32
handle.writeInt32("Int32Value", 1000)
assert(handle.readInt32("Int32Value") == 1000)
handle.writeInt32("Int32ValueMin", -2147483647)
assert(handle.readInt32("Int32ValueMin") == -2147483647)
handle.writeInt32("Int32ValueMax", 2147483647)
assert(handle.readInt32("Int32ValueMax") == 2147483647)

# Int64
handle.writeInt64("Int64Value", 1000)
assert(handle.readInt64("Int64Value") == 1000)
handle.writeInt64("Int64ValueMin", -9223372036854775807)
assert(handle.readInt64("Int64ValueMin") == -9223372036854775807)
handle.writeInt64("Int64ValueMax", 9223372036854775807)
assert(handle.readInt64("Int64ValueMax") == 9223372036854775807)

# Expand String
handle.writeExpandString("ExpandStringValue", "%PATH%")
assert(handle.readExpandString("ExpandStringValue").expandEnvString() != "%PATH%")

# Multi String
handle.writeMultiString("MultiStringValue", ["Hello, world!", "\u03AB世界", "世ϵ界", ""])
var multiString = handle.readMultiString("MultiStringValue")
assert(multiString.len == 3)
assert(multiString[0] == "Hello, world!")
assert(multiString[1] == "\u03AB世界")
assert(multiString[2] == "世ϵ界")

# Key/subkey/value operations
innerHandle = create(handle, "InnerKey", samAll)
assert(innerHandle.countSubkeys() == 0)
var numValues = innerHandle.countValues()
assert(numValues == 0)
assert(numValues == toSeq(innerHandle.enumValueNames()).len)

innerHandle.writeString("InnerStringValue", "Hello")
numValues = innerHandle.countValues()
var valueNames = toSeq(innerHandle.enumValueNames())
assert(numValues == 1)
assert(numValues == valueNames.len)
assert(valueNames[0] == "InnerStringValue")

close(innerHandle)

assert(handle.countSubkeys() == 1)
innerHandle = create(handle, "InnerKey_second", samAll)
close(innerHandle)
innerHandle = 0.RegHandle

assert(handle.countSubkeys() == 2)
delSubkey(handle, "InnerKey")
assert(handle.countSubkeys() == 1)
delTree(handle, "")
assert(handle.countSubkeys() == 0)

close(handle)
handle = 0.RegHandle

# delSubkey(HKEY_LOCAL_MACHINE, "Software\\AAAnim_reg_test", samWow32)
except OSError, AssertionDefect:
passed = false
msg = getCurrentExceptionMsg()
stacktrace = getStackTrace(getCurrentException())
finally:
close(handle)
close(innerHandle)
if passed:
echo "tests passed"
quit(QuitSuccess)
else:
echo "tests failed: ", msg
echo stacktrace
quit(QuitFailure)
Loading

0 comments on commit ce65d64

Please sign in to comment.