51920

RegSetValueEx Only shows writes first character

Question:

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?

Answer1:

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:

<ol><li>

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

</li> <li>

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

</li> <li>

Switch project settings to Unicode build

</li> </ol>

Answer2:

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.

Recommend

  • add current/selected user to a group
  • C++ 307 Errors when trying to include winhttp.h in visual studio 2010
  • C++ blowfish works with fewer letters,but C# blowfish doesn't
  • Why does this Batch file fail to change the wallpaper (Windows 7)
  • Setting Environemnt Type to reg_expand_sz using powershell
  • How to iterate registry from the root?
  • How to remove registry value and key from registry entry
  • Access Windows Performance Counters in a locale independent way
  • Task failed because “LC.exe” was not found after upgrade to Windows 8.1
  • Create a REG_NONE empty value in the Windows Registry via C#
  • Synchronize windows folders
  • configure: error: no acceptable C compiler found in $PATH
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Query to find the duplicates between the name and number in table
  • Unable to get column index with table.getColumn method using custom table Model
  • Eclipse CDT error: Unable to compile
  • Parse a date string in a specific locale (not timezone!)
  • How to define and use opencv mat of user type
  • Django: Count of Group Elements
  • Handling un-mapped Rest path
  • Is there a javascript serializer for JSON.Net?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • What is the “return” in scheme?
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Where to put my custom functions in Wordpress?
  • JSON with duplicate key names losing information when parsed
  • Symfony2: How to get request parameter
  • How to make Safari send if-modified-since header?
  • Jquery - Jquery Wysiwyg return html as a string
  • Akka Routing: Reply's send to router ends up as dead letters
  • How to pass list parameters for each object using Spring MVC?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • Buffer size for converting unsigned long to string
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • JaxB to read class hierarchy
  • Binding checkboxes to object values in AngularJs
  • How to load view controller without button in storyboard?