Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

CS609 Assignment 2

Question:
Write a C/C++ program that will open a duplicated new Registry Key using

RegOpenKeyEx() function. After opening, enlist all subkeys of duplicated key using

regEnumKeyEx() function.

Detailed Instructions:

 Assume that the currently open key is the predefined key:

HKEY_CURRENT_USER_LOCAL_SETTINGS. You have to make a duplicate of this

key by using RegOpenKeyEx() function.

 The DWORD ulOptions parameter in RegOpenKeyEx() function should be zero.

 Take KEY_ENUMERATE_SUBKEY as the access mask for the duplicated key.

 In RegEnumKeyEx() function, the value of dwIndex should be zero on first call


and should be incremented on each subsequent calls.
 Create a character buffer which will store the name of the sub keys (NULL terminated

name). Buffer size will be 255.

 You may take last three parameters of RegEnumKey() function as NULL.

Solution:-

#include <stdlib.h>
#include <Windows.h>

int main()
{
constintMax_Key_Length = 255;
LONG lRValue;
DWORD dwIndex;
DWORD dwKeyNameLength = Max_Key_Length;
HKEY hOpenKey;

lRValue = RegOpenKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, KEY_READ,


&hOpenKey);

if (lRValue == ERROR_SUCCESS) {
printf("Opened the subkey successfully.\n\n");
}
else {
printf("Error encountered.\n");
exit(EXIT_FAILURE);
}
lRValue = RegEnumKeyEx(hOpenKey, dwIndex, keyName, &dwKeyNameLength, 0,
NULL, NULL, &structFT);
while (lRValue == ERROR_SUCCESS) {
_tprintf(_T("%s\n"), keyName);
dwKeyNameLength = Max_Key_Length;
lRValue = RegEnumKeyEx(hOpenKey, ++dwIndex, keyName, &dwKeyNameLength, 0,
NULL, NULL, &structFT);
}

if (lRValue == ERROR_NO_MORE_ITEMS) {
printf("\nSubkeys all enumerated successfully.\n\n");
}

lReturnValue = RegCreateKeyEx(hKey, tszNewkeyName, 0, NULL,


REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubkey, &dwDisposition);

if (lReturnValue == ERROR_SUCCESS) {
printf("Created/opened new subkey.\n");
if (dwDisposition == REG_OPENED_EXISTING_KEY) {
_tprintf(_T("%s Already Existing File is opening"), tszNewkeyName);
}
else {
_tprintf(_T("%s New file is Created and Now opening\n"), tszNewkeyName);
}

HKEY hTheKey;
longlongintllRValue;

llRValue = RegOpenKeyEx(
HKEY_CURRENT_USER,
NULL,
0,
KEY_QUERY_VALUE,
&hTheKey
);

if (llRValue == ERROR_SUCCESS) {
printf("Duplicate registry key created!\n");
}
else {
printf("Duplicate registry key not created!\n");
}

RegCloseKey(hKey);

return 0;
}

You might also like