We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How do I set a REG_BINARY value with a Hex data

gbzhhu
gbzhhu asked
on
Medium Priority
2,798 Views
Last Modified: 2013-11-20
Hi,

I have data in Hex format and I would like to set a REG_BINARY value with this data.

Data is C9D3E900000000003766D3008DA3D500FFFFFF00FFFFFF00000000000C2F7F0000000000FFFFFF0000009900C0C0C000FFFFFF003766D300FFFFFF00C9D3E90080808000BFBFBF0000000000FFFFFF00FFFFFF0000000000FFFFFF0000000000FFFFCC00FFFFFF00000000004F9AF600C0C0C000

C++ code please.  I need to set the pbSysColor to hold the value above
RegSetValueEx(hKeyGWE, _T("SysColor"), 0, REG_BINARY, pbSysColor, cbSize);                   

Thanks
H
Comment
Watch Question

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Does this do it?
RegSetValueEx(hKeyGWE, _T("SysColor"), 0, REG_BINARY, (LPBYTE)pbSysColor, (DWORD)cbSize);                  
I assume cbSize is the length of the binary data

Author

Commented:
I am not a C++ programmer.  I don't know how to set the pbSysColor to the hex value.  How do you assign it before you even call the RegSetValueEx function.
>>>> Data is C9D3E900000000003766D3008D...

Do you have the data as string or as binary data?

For REG_BINARY you need it in binary, e. g.

   unsigned char bytearr[] = { 0xC9, 0xD3, 0xE9, 0x00, ... };

Or you convert the string like

#include <string>
#include <sstream>
#include <iostream>
using namespace std;
...

   const int MAX_ARR = 1024;  // you also couls take the exact length if you know
   unsigned char bytearr[MAX_ARR] = { 0 };
   string hex = "C9D3E900000000003766D3008DA3D500FFFFFF";
   for (int i = 0; i < (int)hex.size(); i+=2)
   {
         istringstream iss(hex.substr(i, 2));
         iss >> hex >> bytearr[i/2];
   }
   cbSize = (int)hex.size()/2;
   pbByte = byteArr;

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
I'm confused.
What is the question?  How to use RegSetValueEx or how to convert string to binary or ...

Author

Commented:
itsmeandnobo and AndyAinscow,

Let me explain more

I am changing a Pocket PC theme.  Somehow it changes all I need except system colours.  Apparently Pocket PC doesn't support SetSysColors API.  So I am left with setting the value in the registry (device) under HKLM\System\GWE key and the value is called syscolor.  

What I did is set the theme manually on the device then capture the registry value.  The reg viewer gives in Hex format but I know the reg value type is REG_BINARY.  What I would like to do is copy the hex as string and "somehow" write that value as REG_BINARY to syscolor.  

I hope I am making sense now

H




>>>> and "somehow" write that value as REG_BINARY to syscolor.
Any problems with the code I posted?

Both variants of filling a byte array should do what you want.

But if you are not a programmer maybe it is easier for you to use regedit to store the values.

Open regedit (Start -> Run -> regedit), navigate to the key and entry you want to set (if not exist til now take any other key with a binary entry). Then select the key and export it (File menu) to a text file. Use NT4 Reg format. Edit the file with notepad or Visual Studio and change keys and values accordingly (you can remove any entries you don't need to update). Finally open a command window and type

    regedit /s yourfile.reg

which will update (or insert) the entry.

Author

Commented:
itsmeandnobo&

I am a programmer.. and have been for 10 years.. just not C++ programmer.  I am coding with C# but devices need native unmanaged code.

>>Both variants of filling a byte array should do what you want.
Your first approach showed how to fill a byte array but not how to use that byte array in the call to RegSetValueEx
Your second route is too long winded and opens up too much code that I am familiar with

I cannot use regedit.  I am putting this code in a dll file that will be called from a CAB file.  

Please ask anything I can to assist you in helping me
 
The below should do as well if you already got the hKeyGWE  (handle to key above).
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
 
 
const char * szColor[] = "C9D3E900000000003766D3008DA3D500FFFFFF00FFFFFF00000000000C2F7F"
                         "0000000000FFFFFF0000009900C0C0C000FFFFFF003766D300FFFFFF00C9D3"
                         "E90080808000BFBFBF0000000000FFFFFF00FFFFFF0000000000FFFFFF0000"
                         "000000FFFFCC00FFFFFF00000000004F9AF600C0C0C000";
 
unsigned int setSysColor(hKeyGWE)
{
   const int MAX_ARR = 1024;  
   unsigned char bytearr[MAX_ARR] = { 0 };
   string hexstr = szColor;
   for (int i = 0; i < (int)hexstr.size(); i+=2)
   {
      istringstream iss(hexstr.substr(i, 2));
      iss >> hex >> bytearr[i/2]; 
   } 
   unsigned int   cbSize     = (int)hexstr.size()/2;
   unsigned char* pbSysColor = byteArr;
 
   return RegSetValueEx(hKeyGWE, _T("SysColor"), 0, REG_BINARY, pbSysColor, cbSize);  
}

Open in new window

>>>> Your first approach showed how to fill a byte array but not how to use that byte array in the call to RegSetValueEx

 it would be

   RegSetValueEx(hKeyGWE, _T("SysColor"), 0, REG_BINARY, bytearr, (DWORD)sizeof(bytearr));

Author

Commented:
Thanks for that  itsmeandnobo

I will try your code ...
>>>>  (DWORD)sizeof(bytearr);

That only worked with

  unsigned char bytearr[] = { 0xC9, 0xD3, 0xE9, 0x00, ... };

where you let the compiler determine the size.

Author

Commented:
Attempted the first approach

2 errors

Error      1      error C2440: 'initializing' : cannot convert from 'const char [233]' to 'const char *[]'      

Error      2      error C2440: 'initializing' : cannot convert from 'const char *[]' to 'std::basic_string<_Elem,_Traits,_Ax>'
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank u.  Build succeeds.  Will try solution on device ........
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.