CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Posts
    83

    Angry RegQueryValue & string types

    Hi,

    Win APIs & string passed to them as arguments are driving me crazy:


    char tmp[22];
    ::RegQueryValue(HKEY_LOCAL_MACHINE, "x", "y", tmp);

    gives me:

    converting 2nd parameter from 'const char [2]' to 'LPCWSTR' not possible


    and


    char tmp[22];
    ::RegQueryValueA(HKEY_LOCAL_MACHINE, "x", "y", tmp);

    converting parameter 4 from 'char [22]' to 'PLONG' not possible



    Is there any reference/explanation as for all those string types?
    Why cant i pass a simple "text string" to an API?

    thx,

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: RegQueryValue & string types

    First of all we have to pay more attention to SDK documentation.
    Taking a look at RegQueryValue, you may notice arguments of type, HKEY, LPCTSTR, LPTSTR, and PLONG.
    HKEY is a handle.
    LPCTSTR is an alias for LPCWSTR if UNICODE is defined (Unicode string). Otherwise it's an alias for LPCSTR (ANSI string).
    Somewhere in a SDK header (winnt.h) you can find something like
    Code:
    #ifdef  UNICODE 
    //...
    typedef LPCWSTR LPCTSTR; 
    //...
    #else
    //...
    typedef LPCSTR LPCTSTR; 
    //...
    #endif
    Finally, LPCWSTR is an alias type for const wchar_t* while LPCSTR is an alias for const char*.
    wchar_t is a standard type keeping wide characters (Unicode).

    LPTSTR "expands" in a very similar way, except that it's not const.
    PLONG is a pointer to LONG (long*).

    Generally in Windows SDK, the functions which are dealing with strings have two versions: one for Unicode and one for ANSI (in our case RegQueryValueW and RegQueryValueA, respectively).
    You cand find that RegQueryValue is just a macro defined as:
    Code:
    #ifdef UNICODE
    #define RegQueryValue  RegQueryValueW
    #else
    #define RegQueryValue  RegQueryValueA
    #endif
    That's to allow using the same code in both Unicode and ANSI builds. The same purpose has types like TCHAR and the macro _T.

    There are many other things to tell here but for the moment we can put these together and get rid of errors by writing something like
    Code:
       #define MAX_DATA_LENGTH 256
       //...
       HKEY hKey = HKEY_LOCAL_MACHINE;
       LPCTSTR pszSubKey = _T("Software\\my_key");
       TCHAR lpData[MAX_DATA_LENGTH] = {0};
       LONG cbValue = MAX_DATA_LENGTH;
       LONG lRet = ::RegQueryValue(hKey, pszSubKey, lpData, &cbValue);
       // ...
    See also:
    Last edited by ovidiucucu; May 23rd, 2010 at 04:57 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: RegQueryValue & string types

    I'd also like to add that according to MSDN (RegQueryValue Function):
    RegQueryValue Function
    Retrieves the data associated with the default or unnamed value of a specified registry key. The data must be a null-terminated string.

    Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the RegQueryValueEx function.
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: RegQueryValue & string types

    Right, Victor.
    Returning to...
    Quote Originally Posted by ovidiucucu View Post
    First of all we have to pay more attention to SDK documentation. [...]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: RegQueryValue & string types

    Quote Originally Posted by ovidiucucu View Post
    Right, Victor.
    Returning to...
    Quote Originally Posted by ovidiucucu View Post
    First of all we have to pay more attention to SDK documentation.
    Wow!
    Sorry, Ovidiu!
    I had read your post "diagonally"
    And my post was, of course, not to you but to OP!
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: RegQueryValue & string types

    No problem. Anyhow, anytime, there is more and more to tell...
    Thank you for completing!
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    May 2010
    Posts
    83

    Re: RegQueryValue & string types

    Thanks guys!
    Perfect!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured