views:

53

answers:

3

One of our program writes program information(window title, memory etc) in Java Preferences. On windows this is available under registry. How can I read the values written by Java program using c (or c++).

Looks like API I should use is RegGetValue. Is this guaranteed to work on Windows XP 32 bit?

The String written by java is UTF-8 encoded. How do I read such strings in windows (win32 or vc++)

Cheers, Jayan

A: 

For accessing the registry in C++, you may be interested in the CRegKey ATL class.

Adam
+1  A: 

I dug into this a bit. RegGetValue() is a new registry call that takes care of some underlying nastiness of the traditional way of querying the registry (RegQueryValueEx). There's some good info about the difference here: http://blogs.msdn.com/larryosterman/archive/2006/01/12/512115.aspx

If you need backwards compatibility, RegGetValue() isn't going to work, so you should be using RegQueryValueEx to read data from the registry.

And now on to what I think is the real question:

  1. What do you get back when you use RegQueryValueEx() ?
  2. How do you know that the values stored in the registry are in utf-8 encoding? Is it stored as a byte array in the registry, or as a REG_SZ?
  3. Have you looked at the value using regedit? What do you see?

So if the question is how to convert a UTF-8 encoded string to an ascii null terminated string, then you should probably change the title of your question. For reference, I found this library that may be of use:

http://utfcpp.sourceforge.net

When I tried the link a few minutes ago, the server timed out - probably SF maintenance going on.

But I would suggest that you make real sure that the values in the registry aren't stored as REG_SZ entries already.

Kevin Day
What makes you think RegGetValue works on Windows XP? Did you test it yourself? On which service pack? The MSDN documentation states that Vista or XP SP1 is required.
Ben Voigt
fair enough - I thought the OP meant to write RegQueryValue() - I've updated my post accordingly.
Kevin Day
A: 

RegGetValue is a newer API. If you want your program to work on XP 32-bit (actually back before Windows 2000) use RegQueryValueEx

A UTF-8 string is NUL-terminated just like any single-byte ASCII string. However you might be interested in converting it to UTF-16. For that you'd want to use MultiByteToWideChar, setting CodePage to CP_UTF8. From UTF-16 you can convert to any other code page using WideCharToMultiByte.

Ben Voigt