如何读取注册表项值 [英] How do I read Registry Key Value

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

问题描述

我使用以下代码创建注册表项:

  LPCTSTR lpNDS = TEXT(SOFTWARE\\myKEY ); 
if(OK == ERROR_SUCCESS)
{
MessageBox(NULL,Success,
_T(SimpleShlExt),
MB_ICONINFORMATION);
}
else
{
MessageBox(NULL,Failed,
_T(SimpleShlExt),
MB_ICONINFORMATION);
}

LONG openRes = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
lpNDS,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
& hRegKey,
NULL);

if(openRes!= ERROR_SUCCESS)
MessageBox(NULL,注册表创建失败,
_T(SimpleShlExt),
MB_ICONINFORMATION);

现在我使用以下命令写入密钥:



pre> CSimpleShlExt :: WriteToRegistry(LPSTR lpRegKeyVal)
{
LONG setRes = RegSetValueEx(hRegKey,NDS,0,REG_SZ,(LPBYTE)lpRegKeyVal, strlen(lpRegKeyVal)+1);
CloseRegistryKey();

}



现在我正在尝试读取我的注册表项值使用 WriteToRegistry 函数创建。我试过了

  RegOpenKeyEx(hRegKey,lpNDS,0,KEY_QUERY_VALUE,& hRegKey); 

但这是失败。



函数应该用来读取键中包含的值?

解决方案

您可以尝试类似:

  TCHAR szValue [TEMP_STR_SIZE] = {0}; 
DWORD dwStorageSize = TEMP_STR_SIZE;
LPBYTE lpStorage = reinterpret_cast< LPBYTE>(szValue);

//在注册表值检索失败的情况下
if(ERROR_SUCCESS!= RegQueryValueEx(hRegKey,_T(NDS),0,0,lpStorage,& dwStorageSize))
{
//提示错误
}

编辑:

只是注意到没有子项,更改了我的示例代码


I am Creating the Registry Key using following code:

   LPCTSTR  lpNDS= TEXT("SOFTWARE\\myKEY");
    if(OK==ERROR_SUCCESS)
    {
            MessageBox ( NULL, "Success", 
                             _T("SimpleShlExt"),
                            MB_ICONINFORMATION );
    }
    else
    {
        MessageBox ( NULL, "Failed" ,
                             _T("SimpleShlExt"),
                            MB_ICONINFORMATION );
    }

        LONG openRes = RegCreateKeyEx(
                    HKEY_LOCAL_MACHINE,
                    lpNDS,
                    0,
                    NULL,
                    REG_OPTION_NON_VOLATILE,
                    KEY_ALL_ACCESS,
                    NULL,
                    &hRegKey,
                    NULL);

         if (openRes!=ERROR_SUCCESS) 
                MessageBox ( NULL, "Registry Createion Failed", 
                             _T("SimpleShlExt"),
                            MB_ICONINFORMATION );

Now I am writing up to the Key using:

CSimpleShlExt::WriteToRegistry(LPSTR lpRegKeyVal)
{
        LONG setRes = RegSetValueEx (hRegKey, "NDS", 0, REG_SZ, (LPBYTE)lpRegKeyVal, strlen(lpRegKeyVal)+1);
        CloseRegistryKey();

}

Now I am trying read the registry key value that I have created using WriteToRegistry function. I have tried with

RegOpenKeyEx(hRegKey,lpNDS,0,KEY_QUERY_VALUE,&hRegKey);

But this is failing.

Which function shall I use to read the value contained inside the key?

解决方案

You can try something like :

TCHAR szValue[TEMP_STR_SIZE] = {0};
DWORD dwStorageSize = TEMP_STR_SIZE;
LPBYTE lpStorage = reinterpret_cast<LPBYTE>(szValue);

// In case of registry value retrieval failure
if (ERROR_SUCCESS != RegQueryValueEx(hRegKey, _T("NDS"), 0, 0, lpStorage, &dwStorageSize))
{
    // Prompt error
}

Edit:
Just noticed there are no subkey, changed my sample code

这篇关于如何读取注册表项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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