[英]How to read a REG_MULTI_SZ type value from the registry using RegQueryValueEx(..) in c++
[英]Extract REG_MULTI_SZ value from registry and store in string array c++
有一個 REG_MULTI_SZ 值,我想把它放在一個字符串數組中,這樣我的程序就可以用它們做其他事情。 我從來沒有使用過 C++ 來訪問注冊表,所以在遵循一些示例后我有點迷失了。 我正在使用VS10。
有沒有一種簡單的方法可以做到這一點? 謝謝。
首先:運行RegQueryValueEx
獲取類型和必要的 memory 大小:
單字節碼:
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] );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.