P
Prashant
Hello All,
I am new to win32 programming,I was trying to get all the Subkeys
under any Key. I have written a C program for achieving this, I am
using WinNT Server .
In this program
*networkCardKeyList is a Global array
The memory allocated to the pointers in this array are freed in the
main function.
The problem that I am facing is that when Query function is
executing there is memory loss, I am not able to trace why is this
happening. Can anyone please help me.
The function is as follows:-
void Query(LPCTSTR path)
{
HKEY hKey;
TCHAR *achKey;
DWORD cbName;
TCHAR achClass[MAX_PATH] = TEXT("");
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;
DWORD retCode;
DWORD cchValue = MAX_VALUE_NAME;
/*
Opening the Key by name to get the values
*/
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_READ,&hKey)!=
ERROR_SUCCESS)
{
printf("Error in Connection");
return ;
}
// 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++)
{
achKey=(TCHAR *)malloc(MAX_VALUE_NAME * sizeof(TCHAR));
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
networkCardKeyList=(TCHAR*)malloc(MAX_VALUE_NAME *
sizeof(TCHAR));
strcpy(networkCardKeyList,achKey);
if(achKey!=NULL){
free(achKey);
achKey=NULL;
}
}
if(achKey!=NULL){
free(achKey);
achKey=NULL;
}
}
}
RegCloseKey(hKey);
}
Thanks
Prashant Gairola
I am new to win32 programming,I was trying to get all the Subkeys
under any Key. I have written a C program for achieving this, I am
using WinNT Server .
In this program
*networkCardKeyList is a Global array
The memory allocated to the pointers in this array are freed in the
main function.
The problem that I am facing is that when Query function is
executing there is memory loss, I am not able to trace why is this
happening. Can anyone please help me.
The function is as follows:-
void Query(LPCTSTR path)
{
HKEY hKey;
TCHAR *achKey;
DWORD cbName;
TCHAR achClass[MAX_PATH] = TEXT("");
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;
DWORD retCode;
DWORD cchValue = MAX_VALUE_NAME;
/*
Opening the Key by name to get the values
*/
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_READ,&hKey)!=
ERROR_SUCCESS)
{
printf("Error in Connection");
return ;
}
// 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++)
{
achKey=(TCHAR *)malloc(MAX_VALUE_NAME * sizeof(TCHAR));
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
networkCardKeyList=(TCHAR*)malloc(MAX_VALUE_NAME *
sizeof(TCHAR));
strcpy(networkCardKeyList,achKey);
if(achKey!=NULL){
free(achKey);
achKey=NULL;
}
}
if(achKey!=NULL){
free(achKey);
achKey=NULL;
}
}
}
RegCloseKey(hKey);
}
Thanks
Prashant Gairola