C++ 读取REG_SZ 、REG_DWORD 、REG_MULTI_SZ 类型注册表值
2015-03-07 16:53
1246 查看
读取REG_SZ 类型的注册表键值
#include "windows.h"
#include "tchar.h"
#include "conio.h"
#include "stdio.h"
#define MY_KEY _T("PathToMyRegistryKey\\MyRegistryKey") // Registry key
#define MY_VALUES _T("NameOfTheREG_MULTI_SZListOfValues") // Registry values
#define NEW_VALUE _T("MyNewValue") // New value
#define FIND_VALUE _T("AnExistingValue") // We will insert the new value after this one
int _tmain(int argc, _TCHAR* argv[])
{
LONG lResult = 0;
HKEY hKey = NULL;
LPTSTR lpValues = NULL;
LPTSTR lpValue = NULL;
LPTSTR lpNewValues = NULL;
LPTSTR lpNewValue = NULL;
DWORD cbValues = 0;
DWORD cbNewValues = 0;
DWORD cbNewValue = 0;
BOOL bFound = FALSE;
__try
{
// OPEN THE REGISTRY KEY
//
_tprintf(_T("RegOpenKeyEx..."));
lResult = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
MY_KEY,
0,
KEY_ALL_ACCESS,
&hKey
);
if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return 1; }
_tprintf(_T("SUCCESS\n"));
// READ THE REG_MULTI_SZ VALUES
//
// Get size of the buffer for the values
_tprintf(_T("RegQueryValueEx..."));
lResult = RegQueryValueEx(
hKey,
MY_VALUES,
NULL,
NULL,
NULL,
&cbValues
);
if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return 1; }
_tprintf(_T("SUCCESS\n"));
// Allocate the buffer
_tprintf(_T("malloc..."));
lpValues = (LPTSTR)malloc(cbValues);
if (NULL == lpValues) { _tprintf(_T("ERROR 0x%x\n"), GetLastError()); return 1; }
_tprintf(_T("SUCCESS\n"));
// Get the values
_tprintf(_T("RegQueryValueEx..."));
lResult = RegQueryValueEx(
hKey,
MY_VALUES,
NULL,
NULL,
(LPBYTE)lpValues,
&cbValues
);
if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return 1; }
_tprintf(_T("SUCCESS\n"));
// SHOW THE VALUES
//
_tprintf(_T("\n**************************\n"));
_tprintf(_T("OLD VALUES\n"));
_tprintf(_T("**************************\n\n"));
lpValue = lpValues;
for (; '\0' != *lpValue; lpValue += _tcslen(lpValue) + 1)
{
// Show one value
_tprintf(_T("%s\n"), lpValue);
}
_tprintf(_T("\n**************************\n\n"));
// INSERT A NEW VALUE AFTER A SPECIFIC VALUE IN THE LIST OF VALUES
//
// Allocate a new buffer for the old values plus the new one
_tprintf(_T("malloc..."));
cbNewValue = (_tcslen(NEW_VALUE) + 1) * sizeof(TCHAR);
cbNewValues = cbValues + cbNewValue;
lpNewValues = (LPTSTR)malloc(cbNewValues);
if (NULL == lpNewValues) { _tprintf(_T("ERROR 0x%x\n"), GetLastError()); return 1; }
_tprintf(_T("SUCCESS\n"));
// Find the value after which we will insert the new one
lpValue = lpValues;
lpNewValue = lpNewValues;
bFound = FALSE;
for (; '\0' != *lpValue; lpValue += _tcslen(lpValue) + 1)
{
// Copy the current value to the target buffer
memcpy(lpNewValue, lpValue, (_tcslen(lpValue) + 1) * sizeof(TCHAR));
if (0 == _tcscmp(lpValue, FIND_VALUE))
{
// The current value is the one we wanted to find
bFound = TRUE;
// Copy the new value to the target buffer
lpNewValue += _tcslen(lpValue) + 1;
memcpy(lpNewValue, NEW_VALUE, (_tcslen(NEW_VALUE) + 1) * sizeof(TCHAR));
lpNewValue += _tcslen(NEW_VALUE) + 1;
}
else
{
// This is not the value we want, continue to the next one
lpNewValue += _tcslen(lpValue) + 1;
}
}
if (!bFound)
{
// We didn't find the value we wanted. Insert the new value at the end
memcpy(lpNewValue, NEW_VALUE, (_tcslen(NEW_VALUE) + 1) * sizeof(TCHAR));
lpNewValue += _tcslen(NEW_VALUE) + 1;
}
*lpNewValue = *lpValue;
// SHOW THE NEW VALUES
//
_tprintf(_T("\n**************************\n"));
_tprintf(_T("NEW VALUES\n"));
_tprintf(_T("**************************\n\n"));
lpNewValue = lpNewValues;
for (; '\0' != *lpNewValue; lpNewValue += _tcslen(lpNewValue) + 1)
{
// Show one value
_tprintf(_T("%s\n"), lpNewValue);
}
_tprintf(_T("\n**************************\n\n"));
// WRITE THE NEW VALUES BACK TO THE KEY
//
_tprintf(_T("RegSetValueEx..."));
lResult = RegSetValueEx(
hKey,
MY_VALUES,
NULL,
REG_MULTI_SZ,
(LPBYTE)lpNewValues,
cbNewValues
);
if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return 1; }
_tprintf(_T("SUCCESS\n"));
}
__finally
{
// Clean up
//
if (NULL != lpValues) { free(lpValues); }
if (NULL != lpNewValues) { free(lpNewValues); }
if (NULL != hKey) { RegCloseKey(hKey); }
//_tprintf(_T("\n<<PRESS ANY KEY>>\n"));
//_getch();
}
return 0;
}
// 此模板来自http://blogs.msdn.com/b/alejacma/archive/2009/11/12/how-to-manipulate-reg-multi-sz-values-from-the-registry-c.aspx
View Code
相关文章推荐
- VBScript读取注册表中REG_MULTI_SZ类型数值的两种方法
- 注册表写入REG_MULTI_SZ类型
- 关于NSIS 写入注册表的问题, REG_MULTI_SZ 类型
- 读取注册表REG_DWORD类型的键值
- 关于 NSIS 写入注册表的问题, REG_MULTI_SZ 类型
- (转载)RegSetValueEx设置REG_SZ类型键值时要注意的问题
- 用VC实现注册表中DWORD类型数据的读取
- C++读取文档及string类型与算术类型的相互转换
- ANSI Unicod UTF-8 编码 及其C++读取三种类型文档实现
- RegQueryValueEx 读取 DWORD
- c++ 类型转换string、wstring、cstring、 char、 tchar、int、dword(三)
- c++读取某文件夹下的所有文件并查找.txt类型文件名
- 2014-04-16工作日志:error:用RegSetValueEx设置注册表的SZ_DWORD类型的值时,提示无访问内存的权限
- c++分行读取并利用数字类型文件数据
- 在用c++读取xml文件时,运行时出现以下错误:出现未处理的“System.Xml.XmlException”类型的异常出现在 system.xml.dll 中
- C++ 读取文件内容到指定类型的变量
- c++读取某个文件夹下全部某种类型的文件
- 关于C++读取mysql字段获取得到的类型
- 用C#读取C/C++中struct数据类型生成的文件
- C#操作注册表REG_DWORD类型键异常(RegistryValueKind.DWord异常)解决方案