Floating point numbers in registry

I want to save some floating point parameters in the registry and retrieve them in my program.

Question: How do I save floating point numbers in the registry? The values I want to save are in the range 0.2 to 5. For now, I multiply them by 1000 and save as REG_DWORD. After reading them, I divide again by 1000. Is this recommended?
You'll need to save them as strings (REG_SZ).
Thank you, can I also get some help parsing the values. I have this till now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float ParamVal = 0;
TCHAR keydata[MAX_VALUE_NAME];
DWORD keydatasize = MAX_VALUE_NAME;
/*Code to enumerate subkeys*/
retCode = RegEnumValue(hKey, i, 
			achValue, 
			&cchValue, 
			NULL, 
			NULL,
			(LPBYTE)&keydata,
			&keydatasize);

if (retCode == ERROR_SUCCESS ) 
{ 
	if( !_tcscmp(ParamName, achValue) )
		//ParamVal = (float) strtod ( (char*)&keydata , NULL); --Tried this first
		sscanf_s ((char*)&keydata, "%f", &ParamVal );
}


ParamName is a TCHAR parameter that I send to the function, if it matches the key name then retrieve the value.

I have been able to get 0.2 into the string keydata.
Question: Both statements on Lines 16 and 17 put 0.0 in ParamVal even when the string value in keydata shows 0.2 in debug. Why does the parse of the string keydata to a floating point value not work?
Last edited on
To write:
1
2
3
char buffer[32];
snprintf(buffer, sizeof(buffer), "%f", fvalue);
RegSetValueEx(hKey, subkey, 0, REG_SZ, (BTYE*)buffer, strlen(buffer) );


To read:
1
2
3
4
5
6
7
char buffer[32];
memset(buffer, 0, sizeof(buffer));
DWORD sz = sizeof(buffer) - 1;
DWORD type = 0;
RegQueryValueEx(hKey, subkey, 0, &type, (BYTE*)buffer, &sz);
if (type == REG_SZ)
    fvalue = atof(buffer);

This worked! Thank you.

I had to change the RegQueryValueEx to RegQueryValueExA

This was because the contents of buffer (the registry value to be retrieved is 0.2) looked like
1
2
3
4
5
6
7
[0] 48 '0'
[1] 0
[2] 46 '.'
[3] 0
[4] 50 '2'
[5] 0
....

and fvalue = atof(buffer); was taking 0.2 from buffer and converting to 0.0.

My best guess was that the string in buffer was not properly formatted and I decided to use the ANSI output function RegQueryValueExA, which seems to have worked. The contents of buffer were now displayed as
1
2
3
4
5
6
7
[0] 48 '0'
[1] 46 '.'
[2] 50 '2'
[3] 0
[4] 50 '2'
[5] 0
....


I don't know where the [4] 50 '2' is from.
Last edited on
If you had to change RegQueryValueEx, then you have _UNICODE defined. You may not need this.
Resolved.

I switched off UNICODE character set (VS2008 > Project Properties > Configuration Properties > General, select "Character Set" and changed the "Use Unicode Character set" to "Use Multi-Byte Character Set".) and there were no problems using RegQueryValueEx.

Thank you very much kbw!
Topic archived. No new replies allowed.