RegSetValueEx Only shows writes first character

杀马特。学长 韩版系。学妹 提交于 2019-12-23 17:53:21

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/22026462/regsetvalueex-only-shows-writes-first-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!