我在c void函数中有这个代码来获取和打印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这八个向量元素的值相同,这里的问题是什么可以用你的代码编译, 非常感谢
答案 0 :(得分:1)
鉴于您使用的是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;
}