【发布时间】:2014-09-23 06:00:25
【问题描述】:
我最近遇到了 RegSetValueEx 的问题(来自 advapi32.dll)。看起来在 .Net 3.5 中使用该方法保存的 REG_SZ 在 .Net 4.5 中使用 RegistryKey.GetValue() 被错误地读取。下图:
//---.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)
上述示例之间的代码 100% 相同 - 只需切换 .Net 4.5 版本。
最初我认为写入注册表的字符串可能没有正确地以空值终止 - 它是。我还验证了如果我尝试使用 RegistryKey.SetValue() 写入值不会发生这种情况 - 只有 RegSetValueEx(来自 advapi32.dll)似乎会导致这种行为。
现在最简单的解决方案是简单地在返回的字符串(在 4.5 中)搜索第一个空终止字符并删除字符串的其余部分。该解决方案有效,但我宁愿找出问题的根源。
关于是什么原因造成的任何想法?非常感谢!
【问题讨论】:
-
请显示您在注册表中看到的值,以及您为确定准确您在 .NET 中检索的内容而执行的任何诊断 - 目前没有我们可以说很多。
-
用更好的信息重写了这个问题,现在我知道问题到底是什么了:)
-
“保存 base64 字符串值”不是 100% clera 的意思 - 您是将其保存为字符串,还是将其保存为字节?你为什么要使用
RegSetValueEx?注册表显示什么?
标签: c# .net .net-3.5 registry .net-4.5