Retrive unicode keys from the registry

Thomas Heller theller at python.net
Fri Jan 16 15:50:53 EST 2004


"Neil Hodgson" <nhodgson at bigpond.net.au> writes:

> Thomas Heller:
>
>> def RegQueryValue(root, subkey):
>>     if isinstance(subkey, unicode):
>>         return _winreg.QueryValue(root, subkey.encode("mbcs"))
>>     return _winreg.QueryValue(root, subkey)
>>
>> Does this look ok?
>
>    It will fail for keys that can not be encoded in your current code page.
> That will not often be a problem but if you want to be safe then access to
> the wide version is needed.

In the actual use case I have, I'm quite sure that the subkeys are coded
in latin-1:

# -*- coding: latin-1 -*-
LOG_KEYS = [u"DSC-S Cs Gun Emitter [Ah]",
            u"Ga Gun Beam Defining Aperture [µAh]"]

and in my tests it worked.
Even if I wrote this:

# -*- coding: latin-1 -*-
LOG_KEYS = ["DSC-S Cs Gun Emitter [Ah]",
            "Ga Gun Beam Defining Aperture [µAh]"]

But, assume that I wanted to provide a patch to Python (or implement in
ctypes) so that QueryValue accepts unicode subkey names (I was
astonished to find out that it does not).  How could this be done,
hopefully portable between NT/2000/XP and 98, and assuming unicows.dll
is not installed - so the wide version is not available on 98?

My understanding is that it's possible to convert any (for a certain
definition of 'any) unicode string in a byte string (because the
encoding for the byte string can be specified), but that it's impossible
to convert a byte string into unicode unless the byte string's encoding
is known.

Maybe this only shows my un-understanding of unicode...

Thanks,

Thomas



More information about the Python-list mailing list