473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Retrive unicode keys from the registry

First I was astonished to see that _winreg.QueryValue doesn't accept
unicode key names, then I came up with this pattern:

def RegQueryValue(root, subkey):
if isinstance(subkey, unicode):
return _winreg.QueryValue(root, subkey.encode("mbcs"))
return _winreg.QueryValue(root, subkey)

Does this look ok?

Thomas
Jul 18 '05 #1
9 2807
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.

Neil
Jul 18 '05 #2
"Neil Hodgson" <nh******@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
Jul 18 '05 #3
Thomas Heller:
In the actual use case I have, I'm quite sure that the subkeys are coded
in latin-1:
And you are also sure that your locale will always use a latin-1 code
page or another code page similar enough to work on your keys.
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?
This is somewhat unpleasant, requiring runtime conditional code. For the
Python standard library, in posixmodule.c, places where there is a need to
branch first check that the OS is capable of wide calls with
unicode_file_names(), then check if the argument is Unicode and if it is
then it calls the wide system API. While the wide APIs do not work on 9x,
they are present so the executable will still load.

One limitation on the Unicode support in posixmodule is that it doesn't
try to detect and use MSLU on Windows 9x/Me.
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).


Then you have to choose the encoding and switch the current locale to
that encoding as there is no encoding or locale parameter to RegQueryValue.
Values with characters from different languages may not be encodable into
any non-Unicode encoding.

Neil
Jul 18 '05 #4
Neil Hodgson wrote:
This is somewhat unpleasant, requiring runtime conditional code. For the
Python standard library, in posixmodule.c, places where there is a need to
branch first check that the OS is capable of wide calls with
unicode_file_names(), then check if the argument is Unicode and if it is
then it calls the wide system API. While the wide APIs do not work on 9x,
they are present so the executable will still load.


I believe the story would be completely different for the registry: the
wide registry functions are available on all Windows versions, AFAIK.

Contributions are welcome.

Regards,
Martin

Jul 18 '05 #5
Thomas Heller wrote:
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...


It certainly is. It is possible to create a registry key which the
"ANSI" API (RegQueryValueExA) cannot find - i.e. where you truly
need the "wide" API (RegQueryValueExW).

An example would be a registry key with Cyrillic, Greek, or Japanese
characters on your system. Encoding them as "mbcs" will convert those
characters into question marks (which is a bug in itself - Python
should rasise an exception instead).

Regards,
Martin

Jul 18 '05 #6
Martin v. Löwis:
I believe the story would be completely different for the registry: the
wide registry functions are available on all Windows versions, AFAIK.


The documentation for RegQueryValue says:
""" Unicode: Implemented as Unicode and ANSI versions. Note that Unicode
support on Windows Me/98/95 requires Microsoft Layer for Unicode. """

Neil
Jul 18 '05 #7
Neil Hodgson wrote:
The documentation for RegQueryValue says:
""" Unicode: Implemented as Unicode and ANSI versions. Note that Unicode
support on Windows Me/98/95 requires Microsoft Layer for Unicode. """


Too bad. Perhaps we should link Python with MSLU?

Regards,
Martin

Jul 18 '05 #8
Martin v. Löwis:
Too bad. Perhaps we should link Python with MSLU?


I don't have any experience with it and fear it would introduce a new
class of bug where the wide API is incompletely supported.

Neil
Jul 18 '05 #9
"Martin v. Löwis" <ma****@v.loewis.de> writes:
Neil Hodgson wrote:
The documentation for RegQueryValue says:
""" Unicode: Implemented as Unicode and ANSI versions. Note that Unicode
support on Windows Me/98/95 requires Microsoft Layer for Unicode. """


Too bad. Perhaps we should link Python with MSLU?


I don't know how to interpret the license that's contained in
unicows.exe.

REDIST.TXT is this:

<quote>
===============================================
Microsoft Layer for Unicode on Windows 95/98/ME
===============================================

In addition to the rights granted in Section 1 of the Agreement
("Agreement"), with respect to UNICOWS.DLL, you have the following
non-exclusive, royalty free rights subject to the Distribution
Requirements detailed in Section 1 of the Agreement:

(1) You may distribute UNICOWS.DLL with the following: Windows 95,
Windows 98, Windows 98 Second Edition, Windows Millennium, Windows NT4,
Windows 2000, Windows XP, and Windows Server 2003.
<end quote>

Since we're not distributing windows ;-) I don't know what this means.

And LICENSE.TXT contains this:

<quote>
* Distribution Terms. You may reproduce and distribute an unlimited
number of copies of the Sample Code and/or Redistributable Code
(collectively "Redistributable Components") as described above in object
code form, provided that

(a) you distribute the Redistributable Components only in conjunction
with and as a part of your Application solely for use with a Microsoft
Operating System Product;

[...]

(c) you distribute your Application containing the Redistributable
Components pursuant to an End-User License Agreement (which may be
"break-the-seal", "click-wrap" or signed), with terms no less protective
than those contained herein;

(d) you do not permit further redistribution of the Redistributable
Components by your end-user customers; (e) you do not use """
<end quote>

Thomas
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Gerson Kurz | last post by:
AAAAAAAARG I hate the way python handles unicode. Here is a nice problem for y'all to enjoy: say you have a variable thats unicode directory = u"c:\temp" Its unicode not because you want it...
0
by: DJP | last post by:
Hi there I need to be able to programmatically set permissions on registry keys using VB / VBScript / VBA. So far I have had a look at doing this using the WScript.Shell object, API calls, and...
16
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm looking for a way to programmatically retrieve the following if possible: Windows Installation Key or COA from the registry Windows installed...
3
by: Sheikko | last post by:
Hi, I am deveopping an application on the pocket pc and I need to program keys to toggle between 2 application. Exactly from the first application I want to open a second application and throught...
5
by: =?Utf-8?B?S2V2aW4gVGFuZw==?= | last post by:
In MFC, CRichEditCtrl contrl, I want to set the codepage for the control to Unicode. I used the following method to set codepage for it (only for ANSI or BIG5, etc, not unicode). How should I...
3
by: The Frog | last post by:
Hi everyone, Does anyone know how to place a set of WEP or WPA keys into the registry(?) programmatically. I have an application (idea) that I am testing to connect temporarily to a wireless...
0
by: Gary | last post by:
I'm having trouble entering Additional Registry Keys into my Package Solution that will deploy the Access 2007 runtime. (Client machine is running XP with Office 2007 Standard.) The Package...
8
by: mario | last post by:
I have checks in code, to ensure a decode/encode cycle returns the original string. Given no UnicodeErrors, are there any cases for the following not to be True? unicode(s, enc).encode(enc)...
3
by: =?Utf-8?B?WVhR?= | last post by:
I want to export some keys to a reg file using "regedit /e", how to export more keys to one file once but no only a key? thank you.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.