枚举注册表子键

时间:2022-09-04 22:37:08
int CLogOp::GetWINLogFilePath()
{
HKEY hKey = NULL;
wchar_t* pBasePath = L"SYSTEM\\CurrentControlSet\\services\\eventlog";

if ( ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, pBasePath, 0, KEY_READ, &hKey) )
{
return REGOPEN_ERROR;
}


DWORD dwMaxSubKeyLen = 0;
DWORD dwSubKeyNum = 0;
if ( ERROR_SUCCESS != RegQueryInfoKey(hKey,NULL,NULL,NULL,&dwSubKeyNum,&dwMaxSubKeyLen,NULL,NULL,NULL,NULL,NULL,NULL) )
{
RegCloseKey(hKey);
return REGGET_ERROR;
}


wchar_t* pszSubKeyName = NULL;
pszSubKeyName = new wchar_t[dwMaxSubKeyLen+1];
if ( !pszSubKeyName )
{
RegCloseKey(hKey);
return OTHER_ERROR;
}


for ( unsigned int i=0; i < dwSubKeyNum; i++ )
{
memset(pszSubKeyName,0,sizeof(wchar_t)*(dwMaxSubKeyLen+1));
DWORD dwSubKeyRealLen = dwMaxSubKeyLen+1;


if ( ERROR_SUCCESS != RegEnumKeyEx(hKey, i, pszSubKeyName, &dwSubKeyRealLen, NULL, NULL, NULL, NULL) )
{
continue;
}


std::wstring wsSubKeyPath = pBasePath;
wsSubKeyPath.append(L"\\");
wsSubKeyPath.append(pszSubKeyName);
HKEY hSubKey = NULL;


if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE,wsSubKeyPath.c_str(),0,KEY_READ,&hSubKey))
{
RegCloseKey(hSubKey);
continue;
}


DWORD  dwFileValueLen = 0;
if(ERROR_SUCCESS != RegQueryValueEx(hSubKey,L"File",NULL,NULL,NULL,&dwFileValueLen))
{
RegCloseKey(hSubKey);
continue;
}


wchar_t* pszFileValue = NULL;
pszFileValue = new wchar_t[dwFileValueLen+1];
if(!pszFileValue)
{
RegCloseKey(hSubKey);
continue;
}


memset(pszFileValue,0,sizeof(wchar_t)*(dwFileValueLen+1));
dwFileValueLen++;
if ( ERROR_SUCCESS != RegQueryValueEx(hSubKey,L"File",NULL,NULL,(LPBYTE)pszFileValue,&dwFileValueLen) )
{
RegCloseKey(hSubKey);
if(pszFileValue)
{
delete[] pszFileValue;
pszFileValue = NULL;
}
continue;
}


wchar_t *pszWINLogFullPath = new wchar_t[dwFileValueLen];
memset(pszWINLogFullPath, 0, sizeof(wchar_t)*(dwFileValueLen));
ExpandEnvironmentStrings(pszFileValue, pszWINLogFullPath, dwFileValueLen);
_mVecWinLogFileFullPath.push_back(pszWINLogFullPath);


if(pszFileValue)
{
delete[] pszFileValue;
pszFileValue = NULL;
}
if ( pszWINLogFullPath )
{
delete[] pszWINLogFullPath;
pszWINLogFullPath = NULL;
}
}


if(pszSubKeyName)
{
delete[] pszSubKeyName;
pszSubKeyName = NULL;
}
RegCloseKey(hKey);

return NOERROR;
}