Beginner question about wxMemoryBuffer Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Beginner question about wxMemoryBuffer

Post by mathieumg » Fri Aug 06, 2010 5:34 pm

This will sound really basic to some of you, but there is a subtility I don't quite get here.

I thought this topic would answer my question, unfortunately it didn't:

My ultimate goal is to retrieve the value "UserPreferencesMask" from the key "HKEY_CURRENT_USER\Control Panel\Desktop" in the Windows registry. Then modify one bit in it, and save it back. (I want to set the second bit to 0, more information there: ... 57204.aspx)

Now, I've always been self-taught, so I tried to do this all by myself, and I feel like I've almost succeeded. I exported the above key from regedit to see the output it would give, and it is: "hex:9e,28,05,80".

Once converted to binary, that gives 1001 1110 0010 1000 0000 0101 1000 0000 (you can see that the second bit is already set to 0, but I'm developing an application that will have to change that bit to 0 on computers where it is set to 1).

My main problem is with handling memory, which I've almost never done (except putting/retrieving some structs into/from binary files back in school). I can only suspect the idea is similar here.

How would I proceed to modify that second bit? (I'm ok with wxRegKey methods, so I know how to save it back, I need help for the "read/modifiy bits" part)

Here is what I have so far:

Code: Select all

wxRegKey *pRegKey;
wxMemoryBuffer *myBuffer;

myBuffer = new wxMemoryBuffer();

pRegKey = new wxRegKey(wxT("HKEY_CURRENT_USER\\Control Panel\\Desktop"));

pRegKey->QueryValue(wxT("UserPreferencesMask"), *myBuffer);
delete pRegKey;

cout << myBuffer << endl;
This outputs 0xa5fff8, which I'm not quite where it's from.

Thanks in advance for helping me!

Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am

Post by Auria » Fri Aug 06, 2010 5:41 pm


first thing : ... c5ecb9d5fc

the method QueryValue returns a boolean to say if it was successful. Start by checking this boolean; if it's not successful don't even try to read the memory buffer.

Second, what you're printing here is the address of your buffer (you're printing a pointer, not the data being pointed to)

Finally, you're using new/delete in places where you don't strictly need them (though that won't cause bugs, it just makes the code harder to read, slower and more leak-prone)
"Keyboard not detected. Press F1 to continue"
-- Windows

Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg » Fri Aug 06, 2010 5:56 pm

You're right, I should be testing whether the read was successful first. However, for the sake of simplicity in this example, I did not include that test.

Thanks for pointing (haha) out the address thing, I should have seen that. How would I then proceed to access/modify the bits in the buffer?

Note taken regarding the new/delete statements.

Thank you!

User avatar
Posts: 15282
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Fri Aug 06, 2010 6:16 pm

I've used bit 7 in this example to make it clearer, (1<<1) could be a little confusing if you're not familiar with bit manipulation in C++

Code: Select all

wxRegKey pRegKey(wxT("HKEY_CURRENT_USER\\Control Panel\\Desktop"));

wxMemoryBuffer myBuffer;
pRegKey.QueryValue(wxT("UserPreferencesMask"), myBuffer);

if(myBuffer.GetDataLen()==4) {
  unsigned char *data=(unsigned char *)myBuffer.GetData();
  data[0] &= ~(1<<7);			// delete bit 7
  // alternative bit manipulations:
  //data[0] |= (1<<7);			// set bit 7
  //data[0] ^= (1<<7);			// toggle bit 7

  // write changed value back
  pRegKey.SetValue(wxT("UserPreferencesMask"), myBuffer);
pRegKey.Close(); ... ators.html

Be aware that you can only manipulate bits 0-7 with above code by changing the 7 in the expression (1 << 7). For higher bits you'd have to manipulate data[1], data[2] etc.
Use the source, Luke!

Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg » Fri Aug 06, 2010 6:52 pm

Thanks a lot doublemax!

I just read your link and it has taught me a lot, I had done manual bit manipulation in my circuits course but had never seen the C/C++ side of it.

Post Reply