Solved

How do I set a REG_BINARY value with a Hex data

Posted on 2009-07-09
14
1,473 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
0
Comment
Question by:gbzhhu
  • 6
  • 6
  • 2
14 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24812501
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
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 24812562
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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24812602
>>>> 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;

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 24812604
I'm confused.
What is the question?  How to use RegSetValueEx or how to convert string to binary or ...
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 24812701
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




0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24812831
>>>> 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.
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 24812902
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
 
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24812914
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

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24812946
>>>> 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));
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 24813003
Thanks for that  itsmeandnobo

I will try your code ...
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24813076
>>>>  (DWORD)sizeof(bytearr);

That only worked with

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

where you let the compiler determine the size.
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 24813183
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>'
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24813240
should be

const char * szColor = ...

without the brackets [] or

const char  szColor[] = ...

with brackets
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 24813276
Thank u.  Build succeeds.  Will try solution on device ........
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
distributed computing in Python 4 99
MaxSpan challenge 9 65
Annoying "thing" blocks my view 4 50
Making an alias 7 52
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now