1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <cstdlib>
#include <cstdio> // for vprintf
using namespace std;
unordered_map< string, vector<string> > REG_TABLE;
void QueryKey(HKEY hKey1, LPCSTR STR);
void Test();
void __cdecl Output(const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
va_end(va);
}
void Test()
{
HKEY hKey = NULL; // was uninit
LONG dwRegOPenKey = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\MyProgram", 0, KEY_READ, &hKey);
if(dwRegOPenKey == ERROR_SUCCESS)
{
// RegOpenKeyEx doesn't use last error, so no point reporting random last error...
//Output("RegOpenKeyEx succeeded, error code %d\n", GetLastError());
Output("RegOpenKeyEx succeeded\n");
QueryKey(hKey, "SOFTWARE\\MyProgram");
} else
{
Output("RegOpenKeyEx failed, error code %d\n", dwRegOPenKey);
}
RegCloseKey(hKey);
}
void QueryKey(HKEY hKey1, LPCSTR STR)
{
//moved//TCHAR achKey[MAX_PATH];
//moved//DWORD cbName;
//gone//TCHAR achClass[MAX_PATH] = TEXT("");
//gone//DWORD cchClassName = MAX_PATH;
DWORD cSubKeys = 0; // not NULL (it's not a pointer)
//gone//DWORD cbMaxSubKey;
//gone//DWORD cchMaxClass;
//gone//DWORD cValues;
//gone//DWORD cchMaxValue;
//gone//DWORD cbMaxValueData;
//gone//DWORD cbSecurityDescriptor;
//gone//FILETIME ftLastWriteTime;
//RegQueryInfoKey( hKey1, achClass, &cchClassName, NULL, &cSubKeys, &cbMaxSubKey, &cchMaxClass, &cValues, &cchMaxValue, &cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime);
RegQueryInfoKey( hKey1, NULL, NULL, NULL, &cSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
if (cSubKeys)
{
TCHAR achKey[MAX_PATH]; // moved here
DWORD cbName = MAX_PATH; // moved here
for (DWORD dw = 0; dw < cSubKeys; dw++){
cbName = MAX_PATH;
//while(RegEnumKeyEx(hKey1, dw, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
while(RegEnumKeyEx(hKey1, dw, achKey, &cbName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
Output("subkeys(%d) %s\n", dw+1, achKey);
HKEY HKey2 = NULL; // NULL is better that 0 for pointers
//moved//char buf[MAX_PATH];
//moved//DWORD dwBufSize = MAX_PATH; // sizeof(buf);
//char* subKeyPath = new char[sizeof(STR) + cbName];
char* subKeyPath = new char[strlen(STR) + cbName + 2]; // + 2 for \ and null term
sprintf(subKeyPath, "%s\\%s", STR, achKey);
vector<string> key;
//if( RegOpenKey(HKEY_CURRENT_USER,subKeyPath,&HKey2) == ERROR_SUCCESS )
if( RegOpenKeyEx(HKEY_CURRENT_USER,subKeyPath,0,KEY_READ,&HKey2) == ERROR_SUCCESS )
{
char buf[MAX_PATH]; // moved here
DWORD dwBufSize = MAX_PATH; // moved here
int i = 0;
while( RegEnumValue(HKey2, i++, buf, &dwBufSize, 0, 0, 0, 0) == ERROR_SUCCESS )
{
dwBufSize = sizeof(buf);
TCHAR value[MAX_PATH];
DWORD value_length = MAX_PATH; // was sizeof(buf);
if( RegQueryValueEx(HKey2, buf, NULL, NULL, (LPBYTE)&value, &value_length) == ERROR_SUCCESS ){
Output("(%d) %s - %s\n", i, buf, value);
key.push_back(value);
}
}
RegCloseKey(HKey2);
}
REG_TABLE[achKey] = key;
delete [] subKeyPath; // free memory, so we don't leak and leak
}
}
}
}
int main ()
{
Test();
return 0;
}
|