RegSetValueEx BOOL

I need some help
im writing an reading from the reg on startup of app an on close

on close i can write the bool to the reg with this
 
RegSetValueEx( hk, "On/Off", 0, REG_DWORD, (PBYTE)&BOOL_ON, sizeof(BOOL));


but my problem is i cant work out how to read from the reg then set the BOOL (BOOL_ON) to what the reg has stored?
This is a Windows-specific question, so should really go in the Windows Programming forum.
1
2
3
4
5
6
7
8
DWORD val     = 0;
DWORD valSize = sizeof(DWORD);
DWORD valType = REG_NONE;
LONG ret = RegQueryValueEx( hk, "On/Off", NULL, &valType, (PBYTE)&val, &valSize );
if( (ERROR_SUCCESS == ret) && (REG_DWORD == valType) )
{
    BOOL_ON = (0 != val);
}


And your set should really be

1
2
DWORD val = BOOL_ON;
RegSetValueEx( hk, "On/Off", 0, REG_DWORD, (PBYTE)&value, sizeof(DWORD));


(which will now work whether BOOL_ON is bool or BOOL)

Andy
Last edited on
thanks heaps.
whats the differance with BOOL an bool?
an FALSE an false
When it comes to C++...

bool is a built-in C++ native type which takes the built-in values true or false.
By built-in, I mean that the C++ compiler understands bool, true, and false as key words.

But with C...

Before C99, there was no boolean type in C. So you had to use one of the integral types to represent a boolean value. The one used by the Windows API (which is a C API, not a C++ API) is BOOL.

BOOL is a typdef of int, and TRUE and FALSE are #defined as 1 and 0, to work with BOOL.

(I think that int is used rather than short, char, etc because of the way C promotes values to ints when comparing. It's been a while since I read about this, and I am a C++ programmer, but I believe a C program will deal with this code

1
2
3
4
5
6
7
char ch1 = 'a';
char ch2 = 'b';

// etc

if(ch1 == ch2) {
    // etc 


by promoting ch1 and ch2 to integers and then comparing them (C promotes everthing that can fit into an int to an int. 64-bit intergers, etc. are treated a bit differently.)

So you might as well use int for BOOL is most cases (unless you have so many of them you're worried about memory, I guess.)

C++ does not promote before the comparison unless types are different.)

C99 has its own boolean type: _Bool. But this is usually #defined to bool. And true and false are #defines of 1 and 0.

Andy
Last edited on
Topic archived. No new replies allowed.