How to write GUID to register ?

Discussion in 'C++' started by Frits JK, Jun 16, 2004.

  1. Frits JK

    Frits JK Guest

    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

    }

    //--------------------------------------------------------------------------
     
    Frits JK, Jun 16, 2004
    #1
    1. Advertisements

  2. Frits JK

    Jarmo Guest

    Jarmo, Jun 16, 2004
    #2
    1. Advertisements

  3. Frits JK

    David Harmon Guest

    On Wed, 16 Jun 2004 18:24:01 GMT in comp.lang.c++, "Frits JK"
    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()
     
    David Harmon, Jun 17, 2004
    #3
  4. Frits JK

    ak Guest

    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
     
    ak, Jun 17, 2004
    #4
  5. Frits JK

    David Harmon Guest

    On Thu, 17 Jun 2004 19:48:48 +0800 in comp.lang.c++, ak
    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
     
    David Harmon, Jun 17, 2004
    #5
  6. Frits JK

    Frits JK Guest

    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.
     
    Frits JK, Jun 18, 2004
    #6
  7. Frits JK

    David Harmon Guest

    On Thu, 17 Jun 2004 23:09:02 GMT in comp.lang.c++, "Frits JK"
    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.
    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.
     
    David Harmon, Jun 18, 2004
    #7
  8. Frits JK

    ak Guest

    yes master
     
    ak, Jun 18, 2004
    #8
  9. Frits JK

    Frits JK Guest

    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




     
    Frits JK, Jun 18, 2004
    #9
  10. Frits JK

    David Harmon Guest

    Please don't top post.

    On Fri, 18 Jun 2004 18:17:11 GMT in comp.lang.c++, "Frits JK"
    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.
    std::string BinString = Hex2Bin( "A1FF01999ED3" );

    std::string Hex2Bin( std::string const & HexString )
    {
    int n1, n2, i;
    std::string result;
    for(i = 0; i < HexString.size(); i+=2) {
    n1 = HexString;
    n1 = (n1>57) ? n1-48 : n1-55;
    // these magic numbers are very error-prone.
    n2 = HexString[i+1];
    n2 = (n2>57) ? n2-48 : n2-55;
    result.push_back(16*n1 + n2);
    return result;
    }
    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.
    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);
    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._
     
    David Harmon, Jun 19, 2004
    #10
  11. Frits JK

    Frits JK Guest

    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
     
    Frits JK, Jun 19, 2004
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.