reading registry

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
i've a requirement to scan the registry keys programatically in a console
application and read the data of the keys..
given a registry key, say,"HKEY_USERS". user should be able to scan the
subkeys of the given key recursively(i.e. all the subkeys of the given
subkeys etcc..) till the last indent level....

i've used a recursion to do so..but it's taking lot of time..
is there any efficient way to acheive it.....
 
Maybe using recursion is causing performance to be not so good as expected.
Try to replace recursive calls with using a queue where yo can put the nodes
yo have to explore.
 
regPath is like \\software\google
and path z HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE wateva.

Define two hash

#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383

call function like this :

registryCall("\\software\google",HKEY_CURRENT_USER);
it will open a text file C:\\regInfo.txt and dump all the values with key names.

void registryCall(CString regPath,HKEY path)
{

HKEY hKey;
if (RegOpenKeyEx(path,regPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return;

TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys=0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;

TCHAR achValue[50];
DWORD cchValue = MAX_VALUE_NAME;

// Get the class name and the value count.
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)
{

for (i=0; i<cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if(!retCode)
{
CString regTempPath=regPath;
regTempPath+="\\";
regTempPath+=achKey;
registryCall(regTempPath,path);

}

}
}
if(cValues)
{
writeToFileRegVal(hKey,achValue,cValues,regPath);
}

// Enumerate the key values.


RegCloseKey(hKey);

}

void writeToFileRegVal(HKEY hKey,TCHAR achValue[], DWORD cValues,CString pathOfReg)
{
DWORD dwType=0;
bool setInstallPath = false;
DWORD lResult;
TCHAR valuedata[50];
DWORD cchValue = MAX_VALUE_NAME;
CStdioFile cfile_object;
cfile_object.Open("C:\\regInfo.txt", CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
cfile_object.SeekToEnd();
cfile_object.WriteString(newLine);
cfile_object.WriteString(pathOfReg);
cfile_object.WriteString(newLine);
for (int i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
{
cchValue = MAX_VALUE_NAME;
valuedata[0]= '\0';
retCode = RegEnumValue(hKey, i,
achValue,
&cchValue,
NULL,
&dwType,
NULL,
NULL);


if(dwType == REG_DWORD)
{
DWORD dwVal;
lResult = RegGetDWord(hKey, achValue, &dwVal);
cfile_object.WriteString(achValue);
cfile_object.WriteString(tabLine);
itoa(dwVal,(char*)valuedata,10);
cfile_object.Write(valuedata,sizeof(&dwVal));
cfile_object.WriteString(newLine);

}
else if(dwType == REG_SZ )
{

LPTSTR szVal;
lResult= RegGetString(hKey, achValue, &szVal);
cfile_object.WriteString(achValue);
cfile_object.WriteString(tabLine);
//atoi(dwVal,(char*)valuedata,10);
cfile_object.WriteString(szVal);
cfile_object.WriteString (newLine);

CString achVal = achValue;
if(!achVal.Compare(_T("Path")))
InstalllDir = szVal;

}
}


cfile_object.Close();

}

HRESULT RegGetString(HKEY hKey, LPCTSTR szValueName, LPTSTR * lpszResult) {

// Given a HKEY and value name returns a string from the registry.
// Upon successful return the string should be freed using free()
// eg. RegGetString(hKey, TEXT("my value"), &szString);

DWORD dwType=0, dwDataSize=0, dwBufSize=0;
LONG lResult;

// Incase we fail set the return string to null...
if (lpszResult != NULL) *lpszResult = NULL;

// Check input parameters...
if (hKey == NULL || lpszResult == NULL) return E_INVALIDARG;

// Get the length of the string in bytes (placed in dwDataSize)...
lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, NULL, &dwDataSize );

// Check result and make sure the registry value is a string(REG_SZ)...
if (lResult != ERROR_SUCCESS) return HRESULT_FROM_WIN32(lResult);
else if (dwType != REG_SZ) return DISP_E_TYPEMISMATCH;

// Allocate memory for string - We add space for a null terminating character...
dwBufSize = dwDataSize + (1 * sizeof(TCHAR));
*lpszResult = new TCHAR[dwBufSize];

if (*lpszResult == NULL) return E_OUTOFMEMORY;

// Now get the actual string from the registry...
lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, (LPBYTE) *lpszResult, &dwDataSize );

// Check result and type again.
// If we fail here we must free the memory we allocated...
if (lResult != ERROR_SUCCESS) { free(*lpszResult); return HRESULT_FROM_WIN32(lResult); }
else if (dwType != REG_SZ) { free(*lpszResult); return DISP_E_TYPEMISMATCH; }

// We are not guaranteed a null terminated string from RegQueryValueEx.
// Explicitly null terminate the returned string...
(*lpszResult)[(dwBufSize / sizeof(TCHAR)) - 1] = TEXT('\0');

return NOERROR;
}


// =====================================================================================
HRESULT RegGetDWord(HKEY hKey, LPCTSTR szValueName, DWORD * lpdwResult) {

// Given a value name and an hKey returns a DWORD from the registry.
// eg. RegGetDWord(hKey, TEXT("my dword"), &dwMyValue);

LONG lResult;
DWORD dwDataSize = sizeof(DWORD);
DWORD dwType = 0;

// Check input parameters...
if (hKey == NULL || lpdwResult == NULL) return E_INVALIDARG;

// Get dword value from the registry...
lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, (LPBYTE) lpdwResult, &dwDataSize );

// Check result and make sure the registry value is a DWORD(REG_DWORD)...
if (lResult != ERROR_SUCCESS) return HRESULT_FROM_WIN32(lResult);
else if (dwType != REG_DWORD) return DISP_E_TYPEMISMATCH;

return NOERROR;
}





JuanmaMtne wrote:

Maybe using recursion is causing performance to be not so good as expected.
26-Sep-07

Maybe using recursion is causing performance to be not so good as expected.
Try to replace recursive calls with using a queue where yo can put the nodes
yo have to explore.



:

Previous Posts In This Thread:

reading registry
hi,
i've a requirement to scan the registry keys programatically in a console
application and read the data of the keys..
given a registry key, say,"HKEY_USERS". user should be able to scan the
subkeys of the given key recursively(i.e. all the subkeys of the given
subkeys etcc..) till the last indent level....

i've used a recursion to do so..but it's taking lot of time..
is there any efficient way to acheive it.....

Maybe using recursion is causing performance to be not so good as expected.
Maybe using recursion is causing performance to be not so good as expected.
Try to replace recursive calls with using a queue where yo can put the nodes
yo have to explore.



:

EggHeadCafe - Software Developer Portal of Choice
Using the Intercepting Filter Pattern to create a Generic Reusable Processing Pipeline
http://www.eggheadcafe.com/tutorial...7-c7f21cbe1b3c/using-the-intercepting-fi.aspx
 
Back
Top