CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    A question regarding RegSetValueEx

    Here is the definition of RegSetValueEx,
    LONG WINAPI RegSetValueEx(
    __in HKEY hKey,
    __in_opt LPCTSTR lpValueName,
    __reserved DWORD Reserved,
    __in DWORD dwType,
    __in_opt const BYTE *lpData,
    __in DWORD cbData
    );

    According to MSDN, If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters. But I tried to specify the cbData without considering terminating null character, it still works. It means, for example, if lpData points to _T("dullboy"), I specify cbData as 14, it still works as I specify cbData as 16. Would anybody here explain why? Thanks for your inputs.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding RegSetValueEx

    Maybe you got lucky and the memory after the end of the char buffer was zero'd?

    Maybe the api checks for a terminating null at length - 1 and writes one out if it doesn't find one (or appends two nulls for multi-sz).

  3. #3
    Join Date
    Nov 2007
    Posts
    613

    Re: A question regarding RegSetValueEx

    A function detects the end of a string either by using the null terminator or by using a parameter for the lenght of the string. It only uses one of the two methods. So, if there is a parameter for the string lenght, the null terminator is ignored. Present or absent, it doesn't matter, the function will only take the declared lenght, regardless of whats's after that.

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: A question regarding RegSetValueEx

    Are you sure your application is using Unicode? If not, then either 14 or 16 is more than enough.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  5. #5
    Join Date
    Nov 2001
    Posts
    251

    Re: A question regarding RegSetValueEx

    Quote Originally Posted by dullboy View Post
    Here is the definition of RegSetValueEx,
    LONG WINAPI RegSetValueEx(
    __in HKEY hKey,
    __in_opt LPCTSTR lpValueName,
    __reserved DWORD Reserved,
    __in DWORD dwType,
    __in_opt const BYTE *lpData,
    __in DWORD cbData
    );

    According to MSDN, If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters. But I tried to specify the cbData without considering terminating null character, it still works. It means, for example, if lpData points to _T("dullboy"), I specify cbData as 14, it still works as I specify cbData as 16. Would anybody here explain why? Thanks for your inputs.
    Windows registry strings are natively stored as UTF-16.

    If your project is compiled as Unicode, then I would definitely follow the guidelines.
    This is what I do:
    DWORD cbData = (_tcslen(str)+1) * sizeof(TCHAR);

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