Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Registry Operations using Win32 - Part 2
#1
Registry Operations using Win32 - Part 2
Read This First ->Registry Operations using Win32 - Part 1

4. RegSetValueEx() - Used to set or change value in a registry key .
The Win32 API reference definition :
Code:
LONG RegSetValueEx(
HKEY hKey,    // handle of key to set value for
LPCTSTR lpValueName,    // address of value to set
DWORD Reserved,    // reserved
DWORD dwType,    // flag for value type
CONST BYTE *lpData,    // address of value data
DWORD cbData     // size of value data
);
Ok, now by using the last two functions, we have opened a subkey. Now its time to set or change a value
inside the subkey. If you remember, the handle of subkey is in the variable hKey .
The first parameter hKey is the handle to the open subkey. So if you have used one of the first two functions,
you will know that the handle used was hKey variable.

The second parameter is lpValueName . This is the name of value to be changed . You will understand more
when seeing the example.
The third parameter is Reserved and is set as 0.

The fourth parameter, dwType specifies type of the value. ie, REG_BINARY or REG_SZ or REG_DWORD and so on...
The fifth parameter is lpData. It is a pointer to the data that is to be stored in the value.
The last parameter cbData is the size of data being stored. You can use the sizeof() function to find the size.

Eg. Now, I have opened the key
Code:
HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run
and its handle is hKey.
There is a value named "abcd" and I want to set its data as 0. So lets see the example code .
DWORD res,val=0;

res=RegSetValueEx (hKey, "abcd", 0, REG_DWORD, (LPBYTE)&val , sizeof(DWORD));

if(res!=ERROR_SUCCESS)
MessageBox(0,"Error using RegSetValueEx() function","Error",0);

5. RegDeleteValue() -- used to delete a value from a specified registry key.
The defintion in Win32 API reference is :
Code:
LONG RegDeleteValue(
HKEY hKey,    // handle of key
LPCTSTR lpValueName     // address of value name
);
The function is very simple and easy to understand.
An example would be enough.
Eg. I have pened the key

Code:
HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run
and its handle is hKey.
There is a value named "abcd" which I want to delete. The code would be
DWORD res;
res=RegDeleteValue(hKey,"abcd");

if(res!=ERROR_SUCCEDD)
MessageBox(0,"Error deleteing value","Error",0);

6. RegDeleteKey() - Used to delete a registry key.
The defintion according to Win32API reference is :
Code:
LONG RegDeleteKey(
HKEY hKey,    // handle of open key
LPCTSTR lpSubKey     // address of name of subkey to delete
);
Here, hKey parameter is the parent key of the key that is to be deleted .

The lpSubKey parameter is the name of subkey to be deleted . It is not case sensitive.


Eg. Consider that I have opened a key HKEY_CURRENT_USER\Software and the handle to it is hKey.
There is a subkey named "ABCD" that I have to delete. The code would be
Code:
DWORD res;
res=RegDeleteKey(hKey,"ABCD");
if(res!=ERROR_SUCCESS)
MessageBox(0,"Error deleting registry key","Error",0);

7. RegEnumKeyEx() - Used to enumerate all the sub keys of an open registry key.
The defintion is :
Code:
LONG RegEnumKeyEx(
HKEY hKey,    // handle of key to enumerate
DWORD dwIndex,    // index of subkey to enumerate
LPTSTR lpName,    // address of buffer for subkey name
LPDWORD lpcbName,    // address for size of subkey buffer
LPDWORD lpReserved,    // reserved
LPTSTR lpClass,    // address of buffer for class string
LPDWORD lpcbClass,    // address for size of class buffer
PFILETIME lpftLastWriteTime     // address for time key last written to
);
The first parameter hKey is the handle to open registry key.
The second parameter dwIndex is the index of subkey to retrieve. ie, first subkey is index 0, second is index 1.
So this value is incremented to get all subkeys using a loop.
The third parameter is lpName is a pointer to receive the name of subkey of corresponding index.

The fourth parameter lpcbName points to a variable that specifies the size, in characters, of variable lpName
The fifth parameter lpReserved is reserved and is set as NULL
The sixth parameter lpClass can be set as NULL for now. It points to a buffer that contains the class of the enumeratedsubkey when the function returns
The seventh parameter lpcbClass points to a variable that specifies the size, in characters, of the variable lpClass.
As we set lpClass NULL, we can set this parameter to be NULL too.

The last parameter is lpftLastWriteTime , which points to a variable that receives the time the enumerated subkey waslast written to.

Eg. Consider I have opened HKEY_LOCAL_MACHINE\\Software key and its handle is hKey.
I want to enumerate all the subkeys of this key.
Code:
DWORD dwIndex=0;
TCHAR lpName[1024];
DWORD lpcbName=1024;
FILETIME fTime;
while(RegEnumKeyEx(hKey, dwIndex, lpName, &lpcbName, NULL, NULL, NULL, &fTime) == ERROR_SUCCESS)
{
MessageBox(0,szKeyName,"Sub Key Name",0);
dwindex++;
}
For this function to work, the handle hKey should have been opened with KEY_ENUMERATE_SUB_KEYS access.
So when opening a subkey using the first two functions we talked about, make sure you give the correct
access options.

8. RegCloseKey() -- This function closes the registry key opened and releases the handle of the specified key.
The Win32API reference definition is :

Code:
LONG RegCloseKey(
HKEY hKey     // handle of key to close
);
The parameter hKey is the handle of the open subkey.
Eg. Consider I have opened the registry key HKEY_LOCAL_MACHINE\Software and its handle is hKey.
Then to close this key, I would write
Code:
DWORD res;
res=RegCloseKey(hKey);
if(res!=ERROR_SUCCESS)
MessageBox(0,"Error closing registry key","Error",0);

That Is All For Now, Enjoy!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Win32/RBot [Source] [C] XDarkCoder 0 1,078 10-11-2012, 10:48 AM
Last Post: XDarkCoder
  C++ Language Tutorial (Part 1) se7en 36 7,180 11-21-2011, 04:20 PM
Last Post: Ignite.
  Registry Operations using Win32 - Part 1 se7en 0 952 03-13-2011, 06:42 AM
Last Post: se7en

Forum Jump:


Users browsing this thread: 1 Guest(s)