.Net 4.5与.Net 3.5 RegSetValueEx返回一堆空字符 [英] .Net 4.5 vs .Net 3.5 RegSetValueEx returning a bunch null characters

查看:104
本文介绍了.Net 4.5与.Net 3.5 RegSetValueEx返回一堆空字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了RegSetValueEx(来自advapi32.dll)的问题。看起来像是用.Net 3.5中的该方法保存的REG_SZ不能使用RegistryKey.GetValue()在.Net 4.5中正确读取。下图说明:

  //-.. Net 3.5 ---(无问题)
调用RegSetValueEx()将base64字符串值 ajG8s保存到注册表
在该值上调用RegistryKey.GetValue()返回 ajG8s

//--..Net 4.5 ---(无问题)
调用RegSetValueEx()将base64字符串值 ajG8s保存到注册表
对该值调用RegistryKey.GetValue()返回 ajG8s

//- .Net 3.5和.Net 4.5 ---(问题)
调用RegSetValueEx()将base64字符串值 ajG8s保存到注册表中(.Net 3.5)
对该值调用RegistryKey.GetValue()返回 ajG8s \0\0\0\0\0\6\A\z ./,'./ Z? (在.Net 4.5中)

上述示例之间的代码100%相同-只需切换NET 4.5版本。



最初,我认为写入注册表的字符串未正确以null终止-是。我还验证了如果我尝试使用RegistryKey.SetValue()写入值,则不会发生这种情况-只有RegSetValueEx(来自advapi32.dll)似乎会导致这种现象。



<现在,最简单的解决方案是简单地在返回的字符串(在4.5中)中搜索第一个空终止字符,然后删除其余字符串。该解决方案有效,但我宁愿找出问题的根源。



关于造成此问题的原因的任何想法?非常感谢!

解决方案

我已经意识到实际的问题是什么。实际的问题是由于我们使用WinApi RegSetValueEx()错误地将base64字符串写入注册表而导致的-传递的length属性太高,结果将大量垃圾写入注册表。



实际的问题是由于(假定)更改而揭示的:

  RegistryKey。 .NET 3.5和4.5之间的GetValue()

。在.Net 3.5将第一个 \0字符终止GetValue()的情况下,.Net 4.5 GetValue()将继续读取,直到...某点。.这就是为什么我在指责.Net-而我们的代码应该受到谴责!仍然-节点的有趣变化。



希望这可以帮助其他遇到类似问题的人:)


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!

解决方案

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()

between .Net 3.5 and 4.5. Where .Net 3.5 would terminate the GetValue() on the first '\0' 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返回一堆空字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆