c++读取REG_MULTI_SZ类型注册表

First: run RegQueryValueEx to get type and necessary memory size:

Single byte code:

 1 DWORD type, size;
 2 vector<string> target;
 3 if ( RegQueryValueExA(
 4     your_key, // HKEY
 5     TEXT("ValueName"),
 6     NULL,
 7     &type,
 8     NULL,
 9     &size ) != ERROR_SUCCESS )
10   return;
11 if ( type == REG_MULTI_SZ )
12 {
13   vector<char> temp(size);
14 
15   if ( RegQueryValueExA(
16       your_key, // HKEY
17       TEXT("ValueName"),
18       NULL,
19       NULL,
20       reinterpret_cast<LPBYTE>(&temp[0]),
21       &size ) != ERROR_SUCCESS )
22   return;
23 
24   size_t index = 0;
25   size_t len = strlen( &temp[0] );
26   while ( len > 0 )
27   {
28     target.push_back(&temp[index]);
29     index += len + 1;
30     len = strlen( &temp[index] );
31   }
32 }

Unicode:

 1 DWORD type, size;
 2 vector<wstring> target;
 3 if ( RegQueryValueExW(
 4     your_key, // HKEY
 5     TEXT("ValueName"),
 6     NULL,
 7     &type,
 8     NULL,
 9     &size ) != ERROR_SUCCESS )
10   return;
11 if ( type == REG_MULTI_SZ )
12 {
13   vector<wchar_t> temp(size/sizeof(wchar_t));
14 
15   if ( RegQueryValueExW(
16       your_key, // HKEY
17       TEXT("ValueName"),
18       NULL,
19       NULL,
20       reinterpret_cast<LPBYTE>(&temp[0]),
21       &size ) != ERROR_SUCCESS )
22   return;
23 
24   size_t index = 0;
25   size_t len = wcslen( &temp[0] );
26   while ( len > 0 )
27   {
28     target.push_back(&temp[index]);
29     index += len + 1;
30     len = wcslen( &temp[index] );
31   }
32 }
原文地址:https://www.cnblogs.com/davygeek/p/5286193.html