Refactor and update handling of registry keys to support encoding of NUL characters in key names as well

Also fix some compilation issues with mingw based on reports from Jeremy Hedges