问题描述
我最近遇到了 RegSetValueEx(来自 advapi32.dll)的问题.看起来在 .Net 3.5 中使用该方法保存的 REG_SZ 在 .Net 4.5 中使用 RegistryKey.GetValue() 被错误地读取.下图:
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 6Az./,'./Z?" (in .Net 4.5)
上述示例之间的代码 100% 相同 - 只需切换 .Net 4.5 版本即可.
The code is 100% the same between the examples above - simply switching the .Net 4.5 version.
最初我认为可能写入注册表的字符串没有正确以空字符结尾 - 确实如此.我还验证了如果我尝试使用 RegistryKey.SetValue() 写入值不会发生这种情况 - 只有 RegSetValueEx(来自 advapi32.dll)似乎会导致这种行为.
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.
现在最简单的解决方案是简单地在返回的字符串(在 4.5 中)搜索第一个空终止字符并删除字符串的其余部分.该解决方案有效,但我更愿意找出问题的根源.
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!
推荐答案
我已经意识到实际问题是什么.实际问题是由于我们使用 WinApi RegSetValueEx() 错误地将 base64 字符串写入注册表引起的 - 传入的长度属性太高,结果导致大量垃圾被写入注册表.
I've realized what the actual problem was. The actual issue was caused by our incorrect writing of the base64 string to the registry using WinApi RegSetValueEx() - the length property passed in was too high, and as a result and bunch of garbage was being written to the registry.
由于(假设的)变化,实际问题被揭示:
The actual problem was revealed because of (assumed) change in:
RegistryKey.GetValue()
介于 .Net 3.5 和 4.5 之间..Net 3.5 将在第一个 ' ' 字符上终止 GetValue() 的地方,.Net 4.5 GetValue() 将继续阅读直到......某个时候......这就是我'责备'.Net的原因 - 而应该怪我们的代码!仍然 - 一个有趣的节点变化.
between .Net 3.5 and 4.5. Where .Net 3.5 would terminate the GetValue() on the first ' ' character, the .Net 4.5 GetValue() would keep on reading until... some point.. That is why I was 'blaming' .Net - whereas it's our code that is to blame! Still - an interesting change to node.
希望这能帮助其他有类似问题的人:)
Hopefully this helps somebody else with a similar problem :)
这篇关于.Net 4.5 与 .Net 3.5 RegSetValueEx 返回一堆空字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!