从注册表读取数据时出现注册表问题 [英] Registry problem while reading data from registry

查看:252
本文介绍了从注册表读取数据时出现注册表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam将c ++软件移植到c#
我在C ++中读取注册表项,我的代码是

iam porting a c++ software to c#
iam reading a registry key in c++ my code is

if(::RegOpenKeyEx(hParentKey, (LPCTSTR) strKeyName2,NULL,KEY_READ  , &hKey)==ERROR_SUCCESS)
{
	lRet = RegQueryValueEx(hKey, key, NULL, &type,(LPBYTE)data,&dataLen); 
}

in c# my code is  


iam从注册表读取二进制数据时,从c#代码获取了不同的输出
我该如何解决这个问题?

我尝试过的事情:


iam getting different output from c# code while reading a binary data from registry
how can i solve this problem ?

What I have tried:

public static readonly UIntPtr HKEY_CURRENT_USER = (UIntPtr)0x80000001;
        
        public const string lpSubKey = "Software\\Scooter Software\\Beyond Compare 3";

        public const int KEY_QUERY_VALUE = 0x1;
        [DllImport("advapi32.dll", EntryPoint = "RegOpenKeyEx")]
        public static extern int RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out UIntPtr phkResult);

        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx", CharSet = CharSet.Auto)]
        public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, out uint lpType, StringBuilder lpData, ref int lpcbData);

        UIntPtr hKeyVal;
        int valueRet;
        uint lpType;

StringBuilder sb = new StringBuilder(1000);
            int lpcbData = sb.Capacity;
            //string sb = string.Empty;CacheID

            valueRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, KEY_QUERY_VALUE, out hKeyVal);


            valueRet = RegQueryValueEx(hKeyVal, "CacheID", 0, out lpType, sb, ref lpcbData);

            //string str = Encoding.Default.GetString(new byte[]())
            MessageBox.Show(string.Format("data type :{0}\n,data size :{1}\n,data :{2}\n", lpType.ToString(), lpcbData.ToString(), sb.ToString()), "Value", MessageBoxButtons.OK);

推荐答案

使用

不知道您为什么对Richard投票.他的回答完全正确.

无需尝试一对一翻译,只需使用.NET类重写代码即可.除了要做的事情之外,您将编写更少的代码,做同样的事情,并获得更好的异常处理能力,这只是忽略了所有返回代码以及因此而导致的任何错误.
Don''t know why you downvoted Richard. His answer was perfectly valid.

Instead of trying to go for a one-to-one translation, just rewrite the code using the .NET classes. You''ll be writing far less code, doing the same thing, and get the bonus of better exception handling over and above the problem of what you''re doing, which is just ignoring all return codes and, therefor, any errors.


这篇关于从注册表读取数据时出现注册表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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