How to write GUID to register ?

F

Frits JK

Hallo,

I have to write a GUID to the register but I don't know to convert to
hexadecimal.
I have tried several things but I am shore that line 5 and 6 are not
correct.
//--------------------------------------------------------------------------
----------

if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\MSMQ\\SimpleClient\\" , 0, NULL, 0, 0, NULL, &hKey,
&disp)== ERROR_SUCCESS)

{

GUID pguid ; // 2
CSTUtil su; // 3
su.CoCreateGuid( &pguid); // 4
char Myguid[50]; // 5 ??
sprintf( Myguid, "%#8x%#4x%#4x%s", pguid.Data1, pguid.Data2,
pguid.Data3, pguid.Data4 ); // 6 ??
RegSetValueEx(hKey, L"fjkGUID", 0, REG_BINARY , (BYTE*)Myguid, 16 );
// 7

}

//--------------------------------------------------------------------------
 
D

David Harmon

On Wed, 16 Jun 2004 18:24:01 GMT in comp.lang.c++, "Frits JK"
sprintf( Myguid, "%#8x%#4x%#4x%s", pguid.Data1, pguid.Data2,
pguid.Data3, pguid.Data4 ); // 6 ??

Been using streams so long I had to get out an old book to review printf
formats. You should too. I doubt if you want any # in that. Make sure
you are getting the string built right before proceeding further.

Try perhaps:

ostringstream format_guid;
format_guid << hex << setfill('0') << setw(8) << pguid.Data1
<< setw(4) << pguid.Data2
etc.

then use result form
format_guid.str().c_str()
 
A

ak

Hallo,

I have to write a GUID to the register but I don't know to convert to
hexadecimal.
I have tried several things but I am shore that line 5 and 6 are not
correct.
//--------------------------------------------------------------------------
----------

if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\MSMQ\\SimpleClient\\" , 0, NULL, 0, 0, NULL, &hKey,
&disp)== ERROR_SUCCESS)

{

GUID pguid ; // 2
CSTUtil su; // 3
su.CoCreateGuid( &pguid); // 4
char Myguid[50]; // 5 ??
sprintf( Myguid, "%#8x%#4x%#4x%s", pguid.Data1, pguid.Data2,
pguid.Data3, pguid.Data4 ); // 6 ??
RegSetValueEx(hKey, L"fjkGUID", 0, REG_BINARY , (BYTE*)Myguid, 16 );
// 7

}

//--------------------------------------------------------------------------

if you are using MSC7:

try using the CRegKey class instead for registry handling, it is more convenient
it has a function called SetGUIDValue which should do the trick, check out
online doc on that. e.g. SetGUIDValue( L"fjkGUID", pguid );


hth
ak
 
D

David Harmon

On Thu, 17 Jun 2004 19:48:48 +0800 in comp.lang.c++, ak
if you are using MSC7:

try using the CRegKey class instead for registry handling, it is more

This comment is OFF TOPIC in comp.lang.c++. Please keep your answers to
standard portable C++ content and avoid proprietary API discussions.

See the welcome message posted twice per week in comp.lang.c++ or
available at http://www.slack.net/~shiva/welcome.txt
 
F

Frits JK

Thanks for all the suggestions, but up till now I don't have a real solution
for something that must be easy for a real C++ programmer.
The problem is that I have worked many years with xBase language's and
started only some weeks

ago with C++ because I have to build a small application in C++ for the
pocket PC




The problem on this moment is that I have to convert a 64 byte Unicode
string to a 16 byte binary string



for instance I need a chr(245) and I have a chr(70) + chr(0)+ chr(53) +
chr(0) = F5



In xBase I can write in 2 minutes a do while loop to convert this string but
on this moment I don't have the know-how to do this in C++







Frits Janse Kok.
 
D

David Harmon

On Thu, 17 Jun 2004 23:09:02 GMT in comp.lang.c++, "Frits JK"
The problem on this moment is that I have to convert a 64 byte Unicode
string to a 16 byte binary string

What is the content of the Unicode string? Hexadecimal? In solving
such problems, I find that the most important thing is to be very clear
about what you start with and what you want to end up with.

Your original question looks more like converting the other way.
for instance I need a chr(245) and I have a chr(70) + chr(0)+ chr(53) +
chr(0) = F5

This example on my system prints 245 = f5.


#include <iostream>
#include <iomanip>
#include <algorithm>

inline int hex1(wchar_t ch)
{
static const wchar_t digits[] = L"0123456789ABCDEF";
return std::find(digits, digits+16, ch)
- digits;
}

inline int hex2(wchar_t ch1, wchar_t ch2)
{
return hex1(ch1) * 16 + hex1(ch2);
}

int main(void)
{
wchar_t s[] = L"F5";
int bits = hex2(s[0], s[1]);
std::cout << bits << " = " << std::hex << bits;
return 0;
}

Perhaps if you included your XBase fragment then the answers might more
exactly fit your intentions, style, etc.
 
F

Frits JK

Perhaps if you included your XBase fragment then the answers might more
exactly fit your intentions, style, etc.


BinString := Hex2Bin( "A1FF01999ED3" )
//-----------------------------------
function Hex2Bin( HexString )

Local n1,n2,i, result:=""

for i=1 to len(HexString) step 2

n1=asc(substr(HexString,i,1)
n1=if(n1>57 , n1-48 , n1-55 )

n2=asc(substr(HexString,i+1,1)
n2=if(n2>57 , n2-48 , n2-55 )

Result+= chr(16*n1 + n2 )
next i
return Result
//--------------------------------

Here you see my xBase example, I think that your example gives the same
result.
I am still studing on the sintax or the command line " std::cout <<
bits << " = " << std::hex << bits; "
On this moment I don't realy understand what this means.
Could you please tell me the equivalents of the xBase functions :
asc ( ) // returns the ASCII value of a character
chr( ) // returns 1 character
substr( String , nStart, nCount) // I use EVC++ and MFC


Thanks you all
Frits Janse Kok




David Harmon said:
On Thu, 17 Jun 2004 23:09:02 GMT in comp.lang.c++, "Frits JK"
The problem on this moment is that I have to convert a 64 byte Unicode
string to a 16 byte binary string

What is the content of the Unicode string? Hexadecimal? In solving
such problems, I find that the most important thing is to be very clear
about what you start with and what you want to end up with.

Your original question looks more like converting the other way.
for instance I need a chr(245) and I have a chr(70) + chr(0)+ chr(53) +
chr(0) = F5

This example on my system prints 245 = f5.


#include <iostream>
#include <iomanip>
#include <algorithm>

inline int hex1(wchar_t ch)
{
static const wchar_t digits[] = L"0123456789ABCDEF";
return std::find(digits, digits+16, ch)
- digits;
}

inline int hex2(wchar_t ch1, wchar_t ch2)
{
return hex1(ch1) * 16 + hex1(ch2);
}

int main(void)
{
wchar_t s[] = L"F5";
int bits = hex2(s[0], s[1]);
std::cout << bits << " = " << std::hex << bits;
return 0;
}

Perhaps if you included your XBase fragment then the answers might more
exactly fit your intentions, style, etc.
 
D

David Harmon

Please don't top post.

On Fri, 18 Jun 2004 18:17:11 GMT in comp.lang.c++, "Frits JK"
Here you see my xBase example, I think that your example gives the same
result.

And here is a mostly line-by-line translation into C++. This may not be
the best C++ code, but I provide it in order to show some (hopefully)
comparable concepts.
BinString := Hex2Bin( "A1FF01999ED3" )

std::string BinString = Hex2Bin( "A1FF01999ED3" );

//-----------------------------------
function Hex2Bin( HexString )

std::string Hex2Bin( std::string const & HexString )
{
Local n1,n2,i, result:=""

int n1, n2, i;
std::string result;
for i=1 to len(HexString) step 2

for(i = 0; i < HexString.size(); i+=2) {
n1=asc(substr(HexString,i,1)

n1 = HexString;
n1=if(n1>57 , n1-48 , n1-55 )

n1 = (n1>57) ? n1-48 : n1-55;
// these magic numbers are very error-prone.
n2=asc(substr(HexString,i+1,1)

n2 = HexString[i+1];
n2=if(n2>57 , n2-48 , n2-55 )

n2 = (n2>57) ? n2-48 : n2-55;
Result+= chr(16*n1 + n2 )

result.push_back(16*n1 + n2);
next i
}

return Result

return result;
}
//--------------------------------

I am still studing on the sintax or the command line " std::cout <<
bits << " = " << std::hex << bits; "
On this moment I don't realy understand what this means.

In particular, std::hex comes from #include<iomanip> and sets flags in
the cout stream to tell it to use hexadecimal format for following
integer output. The flags can be reset with std::dec.
Could you please tell me the equivalents of the xBase functions :
asc ( ) // returns the ASCII value of a character
chr( ) // returns 1 character

Single chars are an integral type in C and C++ and you can often just
let the default conversions happen like you will spot in my example
above. In case you need to force a conversion, the most foolproof way
is also the most work to write:
int i = static_cast<int>(ch);
char ch = static_cast said:
substr( String , nStart, nCount) // I use EVC++ and MFC

I use std::string from the standard library. It has a substr member
function that you can look up in any decent C++ reference, for instance
section 20.3.13 of Stroustrup _The C++ Programming Language, Third Ed._
 
F

Frits JK

Dear David,

This code is working fine !!!
Thank you very much for every thing you did for me.
In the last days I have learned a lot of new C++ syntax.
For instance I did not know about str::string ( I allways used CString from
MFC ).
This weekend I will try to find some good C++ books.

Ones more : " thank you very much "

Greetings from The Netherlands, Europe
Frits Janse Kok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top