Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegSetValueEx Only shows writes first character

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?

like image 921
TheRedAgent Avatar asked Sep 08 '25 07:09

TheRedAgent


2 Answers

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:

  1. RegSetValueExW(..., (const BYTE*) path, ...

  2. CString sPath(path); RegSetValueEx(..., (const BYTE*) (LPCTSTR) sPath, ...

  3. Switch project settings to Unicode build

like image 66
Roman R. Avatar answered Sep 11 '25 02:09

Roman R.


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.

like image 25
Jerry Coffin Avatar answered Sep 11 '25 00:09

Jerry Coffin