| The Windows Registry: from the Ground UpDeleting
Values From the Windows Registry
Now how to delete values and keys, but be warned you will need to take particular attention as using reading the following sections incorrectly could lead to the corruption of your computer. I will explain more about this below. To be able to delete values from the registry you need first open the key and then use the RegDeleteValue API function, which has the following parameters: hKey This is the handle returned from the RegOpenKeyEx API function. lpValueName This is the key that you want to delete and its contained value. Below is a sample of how to do this. Start a new standard-EXE project and add a command button to form1. Set the command buttons name to cmdDeleteValue and its caption to Delete Value. Now add the following code to the code window of form1: Option Explicit Private Declare Function FormatMessage Lib "kernel32" Alias _ "FormatMessageA" (ByVal dwFlags As Long, lpSource As _ Any, ByVal dwMessageId As Long, ByVal dwLanguageId As _ Long, ByVal lpBuffer As String, ByVal nSize As Long, _ Arguments As Long) As Long Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200 Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long) As Long Private Declare Function RegDeleteValue Lib "advapi32.dll" _ Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal _ lpValueName As String) As Long Private Const STANDARD_RIGHTS_ALL = &H1F0000 Private Const KEY_QUERY_VALUE = &H1 Private Const KEY_SET_VALUE = &H2 Private Const KEY_CREATE_SUB_KEY = &H4 Private Const KEY_ENUMERATE_SUB_KEYS = &H8 Private Const KEY_NOTIFY = &H10 Private Const SYNCHRONIZE = &H100000 Private Const KEY_CREATE_LINK = &H20 Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or _ KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY _ Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _ KEY_CREATE_LINK) And (Not SYNCHRONIZE)) Private Const HKEY_LOCAL_MACHINE = &H80000002 Private Sub cmdDeleteValue_Click() Dim lKeyHandle As Long, lTemp As Long, _ sTemp As String, sTempLen As Long, _ sData As String, sDataLen As Long ' Declare variables to hold opened key ' handle and error information, etc... lTemp = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\MW-SOFTWARE", 0&, KEY_ALL_ACCESS, lKeyHandle) ' Open the key and return the handle to the ' lKeyHandle variable lTemp = RegDeleteValue(lKeyHandle, "Rating") ' Call the RegDeleteValue API function to remove ' the key 'Rating' and its value from the opened ' handle of the sub-key Call RegCloseKey(lKeyHandle) 'Call RegCloseKey(lKeyHandle) ' close the opened key by passing the key handle sTemp = String(100, 0) ' Fix the length of the string sTempLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM _ Or FORMAT_MESSAGE_IGNORE_INSERTS, vbNullString, lTemp, _ 0&, sTemp, Len(sTemp), ByVal 0&) ' Call the format message API function to return ' error information in the form of a string MsgBox Left$(sTemp, sTempLen) ' Use the length of the error string returned ' to reduce the string back down to size and ' display the message in a message box End Sub Now if you run the project, and click on the command button that the value will be deleted from the registry. You will need to make sure that the value HKEY_LOCAL_MACHINE\SOFTWARE\MW-SOFTWARE and Key Name Rating exists in the registry. This function works for any type of value in the registry, and you should by now see how easy manipulating the registry is. |
___________________________________________ Send Bugs, comments to the webmaster at webmaster@mwe.8m.com © 1999 MW Software, all rights reserved |