Article ID: 175329
Article Last Modified on 11/21/2006
hMutex = CreateMutex( NULL, TRUE, REG_RESTORE);
if ( !hMutex )
return GetLastError();
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
if ((lRet=WaitForSingleObject(hMutex, INFINITE)) != WAIT_OBJECT_0)
return lRet;
}
for (;;)
{
cbValue = REGSTR_MAX_VALUE_LENGTH; // reset value length
// remove this keys old values
lRet = RegEnumValue(hStartKey, // handle of key to query
0, // index of value to query
szValue, // address of buffer for value string
&cbValue, // address for size of value buffer
NULL, // reserved
NULL, // address of buffer for type code
NULL, // address of buffer for value data
NULL // address for size of data buffer
);
if ( ERROR_NO_MORE_ITEMS == lRet ) // all values deleted
{
lRet = ERROR_SUCCESS;
break;
}
else if ( ERROR_SUCCESS == lRet )
{
if ((lRet = RegDeleteValue(
hStartKey, // handle of key
szValue // address of value name
)) != ERROR_SUCCESS)
return lRet;
}
else
return lRet;
} // end for loop
// delete all subkeys
for(;;)
{
dwSubKeyLength = MAX_PATH;
lRet=RegEnumKeyEx(
hStartKey,
0,
szSubKey,
&dwSubKeyLength,
NULL,
NULL,
NULL,
NULL
);
if(lRet == ERROR_NO_MORE_ITEMS)
{
lRet = ERROR_SUCCESS;
break;
}
else if(lRet == ERROR_SUCCESS)
{
if((lRet = RegDeleteKey(hStartKey, szSubKey)) != ERROR_SUCCESS)
return lRet;
}
else
return lRet;
} // end for loop
// Load new hive
lRet = RegLoadKey(HKEY_USERS, "TEMP_HIVE", szNewHive );
DWORD RegCreateTree(HKEY hTree, HKEY hReplacement)
{
DWORD cdwClass, dwSubKeyLength, dwDisposition, dwKeyIndex = 0;
LPTSTR pSubKey = NULL;
TCHAR szSubKey[REGSTR_MAX_VALUE_LENGTH]; // this should be dynamic.
TCHAR szClass[REGSTR_MAX_VALUE_LENGTH]; // this should be dynamic.
HKEY hNewKey, hKey;
DWORD lRet;
for(;;)
{
dwSubKeyLength = REGSTR_MAX_VALUE_LENGTH;
cdwClass = REGSTR_MAX_VALUE_LENGTH;
lRet=RegEnumKeyEx(
hReplacement,
dwKeyIndex,
szSubKey,
&dwSubKeyLength,
NULL,
szClass,
&cdwClass,
NULL
);
if(lRet == ERROR_NO_MORE_ITEMS)
{
lRet = ERROR_SUCCESS;
break;
}
else if(lRet == ERROR_SUCCESS)
{
if ((lRet=RegCreateKeyEx(hTree, szSubKey,0, szClass,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hNewKey, &dwDisposition)) != ERROR_SUCCESS )
break;
else // add key values and recurse
{
if ((lRet=RegCreateValues( hReplacement, szSubKey, hNewKey))
!= ERROR_SUCCESS)
{
CloseHandle(hNewKey);
break;
}
if ( (lRet=RegOpenKeyEx(hReplacement, szSubKey, 0,
KEY_ALL_ACCESS, &hKey )) == ERROR_SUCCESS )
{
lRet=RegCreateTree(hNewKey, hKey);
CloseHandle(hKey);
CloseHandle(hNewKey);
if ( lRet != ERROR_SUCCESS )
break;
}
else
{
CloseHandle(hNewKey);
break;
}
}
}
else
break;
++dwKeyIndex;
} // end for loop
return lRet;
} // end RegCreateTree function
DWORD RegCreateValues(HKEY hReplacement, LPCTSTR lpSubKey, HKEY hNewKey)
{
DWORD cbValue, dwSubKeyIndex=0, dwType, cdwBuf;
DWORD dwValues, cbMaxValueData, i;
LPTSTR pSubKey = NULL;
TCHAR szValue[REGSTR_MAX_VALUE_LENGTH]; // this should be dynamic.
HKEY hKey;
DWORD lRet = ERROR_SUCCESS;
LPBYTE pBuf;
if ((lRet=RegOpenKeyEx(hReplacement, lpSubKey, 0,
KEY_ALL_ACCESS, &hKey )) == ERROR_SUCCESS)
{
if ((lRet=RegQueryInfoKey (hKey, NULL, NULL, NULL, NULL, NULL,
NULL, &dwValues,NULL, &cbMaxValueData,
NULL, NULL)) == ERROR_SUCCESS)
{
if ( dwValues )
{
if ((pBuf=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
cbMaxValueData )))
{
for (i = 0; i < dwValues ; i++)
{
// get values to create
cbValue = REGSTR_MAX_VALUE_LENGTH;
cdwBuf = cbMaxValueData;
lRet = RegEnumValue(
hKey, // handle of key to query
i, // index of value to query
szValue, // buffer for value string
&cbValue, // address for size of buffer
NULL, // reserved
&dwType, // buffer address for type code
pBuf, // address of buffer for value data
&cdwBuf // address for size of buffer
);
if ( ERROR_SUCCESS == lRet )
{
if( (lRet=RegSetValueEx(hNewKey, szValue, 0,
dwType, (CONST BYTE *)pBuf,
cdwBuf))!= ERROR_SUCCESS)
break;
}
else
break;
} // for loop
}
HeapFree(GetProcessHeap(), 0, pBuf);
}
}
CloseHandle(hKey);
}
return lRet;
} // end of RegCreateValues function
// UnLoad user hive lRet = RegUnLoadKey(HKEY_USERS, "TEMP_HIVE"); ReleaseMutex(hMutex);
Additional query words: win95 regedit
Keywords: kbhowto kbkernbase kbregistry KB175329