RegQueryValueExW vs RegQueryValueExA
-
I am confused with RegQueryValueExW and RegQueryValueExA
DWORD sz = 0
LSTATUS status = RegQueryValueExW(key, L"", 0, 0, 0, &sz);Result: status : 0 sz : 20 But in
DWORD sz = 0
LSTATUS status = RegQueryValueExA(key, "", 0, 0, 0, &sz);Result: status : 0 sz : 10 Why status is ERROR_SUCCESS (0) and sz value is different.
-
I am confused with RegQueryValueExW and RegQueryValueExA
DWORD sz = 0
LSTATUS status = RegQueryValueExW(key, L"", 0, 0, 0, &sz);Result: status : 0 sz : 20 But in
DWORD sz = 0
LSTATUS status = RegQueryValueExA(key, "", 0, 0, 0, &sz);Result: status : 0 sz : 10 Why status is ERROR_SUCCESS (0) and sz value is different.
Because
sz
is the size of the value in bytes, not characters. If you retrieve a string from the registry usingRegQueryValueExA
you get a string with 1-byte ASCII characters, and if you useRegQueryValueExW
you get a string with 2-byte Unicode characters. -
Because
sz
is the size of the value in bytes, not characters. If you retrieve a string from the registry usingRegQueryValueExA
you get a string with 1-byte ASCII characters, and if you useRegQueryValueExW
you get a string with 2-byte Unicode characters. -
Thanks for reply but I am aware about Ansi and Unicode difference. Here question is little different: sz value coming different for empty value name which is strange for me.
What's so strange? The call is telling you the number of bytes needed to hold the value string that would be returned. For an ASCII string, you need 10 bytes. For the Unicode version, you need 20 bytes. Read the documentation on RegQueryValueExA (ow W):
Quote:
A pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter, in bytes. When the function returns, this variable contains the size of the data copied to lpData. The lpcbData parameter can be NULL only if lpData is NULL. If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, this size includes any terminating null character or characters unless the data was stored without them. For more information, see Remarks. ---> If the buffer specified by lpData parameter is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by lpcbData. In this case, the contents of the lpData buffer are undefined. <--- If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This enables an application to determine the best way to allocate a buffer for the value's data. If hKey specifies HKEY_PERFORMANCE_DATA and the lpData buffer is not large enough to contain all of the returned data, RegQueryValueEx returns ERROR_MORE_DATA and the value returned through the lpcbData parameter is undefined. This is because the size of the performance data can change from one call to the next. In this case, you must increase the buffer size and call RegQueryValueEx again passing the updated buffer size in the lpcbData parameter. Repeat this until the function succeeds. You need to maintain a separate variable to keep track of the buffer size, because the value returned by lpcbData is unpredictable. If the lpValueName registry value does not exist, RegQueryValueEx returns ERROR_FILE_NOT_FOUND and the value returned through the lpcbData parameter is undefined.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code