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

visual studio 2010 - Extract REG_MULTI_SZ value from registry and store in string array c++

本文关键字:存储 字符串 数组 MULTI 注册表 提取 REG SZ      更新时间:2023-10-16

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

是否有简单的方法来做到这一点?谢谢。

首先:运行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] );
  }
}