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.
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).
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.
Re: A question regarding RegSetValueEx
Are you sure your application is using Unicode? If not, then either 14 or 16 is more than enough.
Re: A question regarding RegSetValueEx
Quote:
Originally Posted by
dullboy
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);