I have recently ran into a problem with RegSetValueEx (from advapi32.dll). It looks like a REG_SZ saved with that method in .Net 3.5 gets incorrectly read in .Net 4.5 using RegistryKey.GetValue(). Illustration below:
//---.Net 3.5--- (no issues)
Call RegSetValueEx() to save base64 string value "ajG8s" to the registry
Call RegistryKey.GetValue() on that value returns "ajG8s"
//---.Net 4.5--- (no issues)
Call RegSetValueEx() to save base64 string value "ajG8s" to the registry
Call RegistryKey.GetValue() on that value returns "ajG8s"
//---.Net 3.5 & .Net 4.5--- (issues)
Call RegSetValueEx() to save base64 string value "ajG8s" to the registry (in .Net 3.5)
Call RegistryKey.GetValue() on that value returns "ajG8s\0\0\0\0\0\6\A\z./,'./Z?" (in .Net 4.5)
The code is 100% the same between the examples above - simply switching the .Net 4.5 version.
Originally I thought that maybe the string being written to the registry was not correctly null terminated - it is. I have also verified that this does not occur if I try to write the value using RegistryKey.SetValue() - only RegSetValueEx (from advapi32.dll) seems to cause this behaviour.
Now the simplest solution would be to simply search the returned string (in 4.5) for the first null terminating character and remove the rest of the string. That solution works, but I would rather figure out the root of the issue.
Any ideas on what is causing it? Many thanks!