从注册表中提取REG_MULTI_SZ值并存储在字符串数组c ++中

时间:2011-07-13 19:59:58

标签: c++ visual-studio-2010 visual-c++ registry

我想在一个字符串数组中加入一个REG_MULTI_SZ值,以便我的程序可以用它们做其他事情。我从来没有使用过C ++来访问注册表,所以在按照一些例子后我有点迷失了。我正在使用VS10。

有一种简单的方法吗?谢谢。

1 个答案:

答案 0 :(得分:5)

首先:运行RegQueryValueEx以获取类型和必要的内存大小:

单字节代码:

DWORD type, size;
vector<string> target;
if ( RegQueryValueExA(
    your_key, // HKEY
    TEXT("ValueName"),
    NULL,
    &type,
    NULL,
    &size ) != ERROR_SUCCESS )
  return;
if ( type == REG_MULTI_SZ )
{
  vector<char> temp(size);

  if ( RegQueryValueExA(
      your_key, // HKEY
      TEXT("ValueName"),
      NULL,
      NULL,
      reinterpret_cast<LPBYTE>(&temp[0]),
      &size ) != ERROR_SUCCESS )
  return;

  size_t index = 0;
  size_t len = strlen( &temp[0] );
  while ( len > 0 )
  {
    target.push_back(&temp[index]);
    index += len + 1;
    len = strlen( &temp[index] );
  }
}

的Unicode:

DWORD type, size;
vector<wstring> target;
if ( RegQueryValueExW(
    your_key, // HKEY
    TEXT("ValueName"),
    NULL,
    &type,
    NULL,
    &size ) != ERROR_SUCCESS )
  return;
if ( type == REG_MULTI_SZ )
{
  vector<wchar_t> temp(size/sizeof(wchar_t));

  if ( RegQueryValueExW(
      your_key, // HKEY
      TEXT("ValueName"),
      NULL,
      NULL,
      reinterpret_cast<LPBYTE>(&temp[0]),
      &size ) != ERROR_SUCCESS )
  return;

  size_t index = 0;
  size_t len = wcslen( &temp[0] );
  while ( len > 0 )
  {
    target.push_back(&temp[index]);
    index += len + 1;
    len = wcslen( &temp[index] );
  }
}
相关问题