[英]return array of windows registry key subkeys c++
我在交流無效功能中有此代碼來獲取和打印Windows注冊表項的子項
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
// Enumerate the subkeys, until RegEnumKeyEx fails.
if (cSubKeys)
{
printf( "\nNumber of subkeys: %d\n", cSubKeys);
for (i=0; i<cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
_tprintf(TEXT("(%d) %s\n"), i+1, achKey);
}
}
}
我如何修改以返回具有所有子項值的數組,謝謝
大衛,您好!感謝您的回應,我無法使用vector<string> subkeys
和這些標頭正確編譯
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
如果我更改為:
vector<TCHAR> getSubKeys(HKEY key)
{
vector<TCHAR>> subkeys;
....
for (i=0; i<cSubKeys; i++)
{
// get subkey name
subkeys.push_back(TCHAR>(achKey));
}
....
return subkeys;
}
進行此更改后,它起作用了,但是在t_main函數中,當我嘗試向控制台列出向量時,只顯示八個(子鍵的數目正確)數字,例如65000,八個向量元素的值相同,這是問題所在或如何編譯與您的代碼,非常感謝
鑒於您正在使用C ++,因此不應使用數組。 相反, vector<T>
是適當的數據結構。 創建其中之一來保存您的注冊表項字符串。
vector<string> subkeys;
當前在哪里打印achKey
,而是添加到subkeys
:
subkeys.push_back(string(achKey));
如果要構建Unicode,請改用wstring
。
您的函數可能如下所示:
vector<string> getSubKeys(HKEY key)
{
vector<string> subkeys;
....
for (i=0; i<cSubKeys; i++)
{
// get subkey name
subkeys.push_back(string(achKey));
}
....
return subkeys;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.