In the following code, RegSetValueEx is only writing the first letter of my string. I've tried changing the sizes to just about anything I can think of, and I only ever get the first string. Any help is appreciated.
LPCWSTR path = L"Test String";
size_t size = wclsen(path) * sizeof(wchar_t);
DWORD dwResult = RegSetValueEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\My App",
0,
REG_SZ,
(LPBYTE)path,
test);
I've tried using path.size() * sizeof(wchar_t) and any number of other sizes I could think of, but nothing seems to work right. Any ideas?
RegSetValueEx()
expects REG_SZ
data to be provided as const TCHAR*
, which in your case is const CHAR*
per your compiler settings - as evident by the fact that you are able to pass a char*
to the second parameter, which means you are actually calling RegSetValueExA()
. Since you are providing a const WCHAR*
to RegSetValueExA()
, the first 0x00
byte gets interpreted as a null terminator, hence only a single character value gets written.
Your options are:
RegSetValueExW(..., (const BYTE*) path, ...
CString sPath(path); RegSetValueEx(..., (const BYTE*) (LPCTSTR) sPath, ...
Switch project settings to Unicode build
Sounds like you haven't defined UNICODE
/ _UNICODE
before compiling, so the zero-byte in your wide string is being interpreted as signaling the end of the string.
Try using RegSetValueExW
(and L"SOFTWARE\\\\My App"
) instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.