[RESOLVED] X64 W7 Registry Delete Sub Keys
Question:
1)
if i use the RegEnumKeyEx in X64 bit the hkey handle that is return is fine in the IDE, It deletes it
If it is compiled already The Keyname lost 1 character at the end
I have tried enumeration from the top too using regqueryinfo from highest number,
it lost 1 character too when run after compiled so it does not delete the sub key
in the IDE it deletes all sub keys?
Any help from reg experts please?
Note:
the DeleteTree works fine in W7 x64, im doing delete sub keys that does not have subsubkeys
If i use RegOpenKeyEx to return it it does not delete the subkey for both IDE and compiled
Code:
'testing building this for x64 key delete
'Subkeys of a KEY, Subkey must not contain subkey
Public Function Delete_KeySub(ByVal RootKey As HKEY_Type, ByVal sKey As String) As Boolean
Dim lRegkey As Long, bFailed As Boolean
Dim sName As String, lNameLen As Long
Dim sClass As String, lClassLen As Long
Dim tFILETIME As FILETIME
Dim lRetVal As Long
Dim Index As Long
On Error GoTo Handler
lRetVal = RegOpenKeyEx(RootKey, sKey, 0, _
KEY_WOW64_64KEY + KEY_ENUMERATE_SUB_KEYS, lRegkey)
If Not lRetVal = ERROR_NONE Then GoTo Handler
Index = 0
Do
sClass = Space$(255)
sName = Space$(255)
lClassLen = 255
lNameLen = 255
lRetVal = RegEnumKeyEx(lRegkey, Index, sName, lNameLen, ByVal 0, _
sClass, lClassLen, tFILETIME)
sName = Left(sName, lNameLen)
MsgBox lRegkey & ":" & sName
If lRetVal = ERROR_NONE Then
Dim lsubkey As Long 'will not be use if i get lsubkey to return and delete
If RegOpenKeyEx(lRegkey, sName, 0, _
KEY_WOW64_64KEY + KEY_ALL_ACCESS, lsubkey) = ERROR_NONE Then
RegDeleteKeyEx lRegkey, sName, KEY_WOW64_64KEY + KEY_ALL_ACCESS, 0 ' just testing to delete no need for open by the way
'RegDeleteKeyEx lsubkey, sName, KEY_WOW64_64KEY + KEY_ALL_ACCESS, 0 'Why not working? 'lsubkey will not be use if this works
End If
End If
bFailed = True
Else
bFailed = False
End If
Index = Index + 1
Loop Until Not lRetVal = 0
' Close the Key Handle
Call RegCloseKey(lRegkey)
Delete_KeySub = (Not bFailed)
'MsgBox Delete_KeySub
Handler:
lRetVal = RegCloseKey(lRegkey)
On Error GoTo 0
End Function
Re: X64 W7 Registry Delete Sub Keys
Then this trying out again, it deletes the subkeys during IDE testing, works well.
After compiled, it does not read the key?
I'm searching the net for issues regarding this, any ideas sirs?
Code:
'testing this function on X64 bit
'Subkeys of a KEY, Subkey must not contain subkey for it to be deleted
Public Function Delete_KeySubTwo(ByVal RootKey As HKEY_Type, ByVal sKey As String) As Boolean
Dim lRetVal As Long
Dim hKey As Long
Dim tFILETIME As FILETIME
Dim bFailed As Boolean
Dim lCtr As Long
Dim sName As String
Dim lNameLen As Long
Dim sClass As String
Dim lClassLen As Long
Dim lNumKeys As Long ' Number of subkeys of open key
Dim lSubkeyNameLen As Long ' Length of the longest subkey name
Dim sRegPath As String
If RegOpenKeyEx(RootKey, sKey, 0, KEY_WOW64_64KEY + KEY_ALL_ACCESS, hKey) = ERROR_NONE Then
' Get subkey information
lRetVal = RegQueryInfoKey(hKey, vbNullString, 0, 0, lNumKeys, lSubkeyNameLen, _
0, 0, 0, 0, 0, tFILETIME)
If lRetVal = ERROR_NONE Then
Dim sMsg As String
sMsg = "Root" & vbCrLf & RootKey & "\" & sKey & vbCrLf & vbCrLf
sMsg = sMsg & "Subkeys: " & lNumKeys & vbCrLf
sMsg = sMsg & "Longest subkey name: " & lSubkeyNameLen & vbCrLf
MsgBox sMsg
'Loop as many times as there are subkeys
For lCtr = lNumKeys - 1 To 0 Step -1
'Initialize buffer to hold name to be 1 greater than maximum length
sName = Space(lSubkeyNameLen) & Chr(0)
lNameLen = Len(sName)
sClass = Space$(255)
lClassLen = 255
RegEnumKeyEx hKey, lCtr, sName, lNameLen, 0, sClass, lClassLen, tFILETIME
'Trim returned buffer to extract key name
sName = Left(sName, lNameLen)
sMsg = "Key " & lCtr & ": " & sName & vbCrLf
MsgBox sMsg
'(SCENARIO 1)
RegDeleteKeyEx hKey, sName, KEY_WOW64_64KEY + KEY_ALL_ACCESS, 0
'(SCENARIO 2), Open the subkey first, Cannot delete hsubkey
'Open the subkey but does not work
'Dim hSubkey As Long
'If RegOpenKeyEx(hKey, sName, 0, KEY_WOW64_64KEY + KEY_ALL_ACCESS, hSubkey) = ERROR_NONE Then
'sMsg = sMsg & " Returned handle " & hSubkey & vbCrLf
'MsgBox sMsg
'RegDeleteKeyEx hSubkey, sName, KEY_WOW64_64KEY + KEY_ALL_ACCESS, 0
'RegDeleteKey hKey, sName 'Does not work for it is redirected
'RegCloseKey hSubkey
'End If
Next lCtr
RegCloseKey hKey
bFailed = True
Else
bFailed = False
End If
End If
Delete_KeySubTwo = (Not bFailed)
'MsgBox Delete_KeySubTwo
Handler:
lRetVal = RegCloseKey(hKey)
On Error GoTo 0
End Function
Re: X64 W7 Registry Delete Sub Keys
After compiled does your app run as Admin?
What manifest do you have ?
Re: X64 W7 Registry Delete Sub Keys
Yes it does run as Administrator. The Keys are returned wrong when compiled. I dont have problems with Admin Rights. It does also delete the sub keys in the IDE.
im still working on why as of this writing.
Re: X64 W7 Registry Delete Sub Keys
Actually i just remembered I had the same problem a while ago. I'm just trying to remember how i fixed it.
Is the subkey you are trying to delete empty? as in it has no more subkeys?
Re: X64 W7 Registry Delete Sub Keys
Yeah it is empty.
I want this to work, for now since i know the subkeys i delete it with the RegDeleteKeyEx first then delete the main key.
Going back to the problem,
When i stepped through the code it returns
NEW KEY#1
NEW KEY#2
NEW KEY#3
After compile it returns
NEW KEY#1
NEW KEY#
NEW KEY#
that is why it fails.
i read some article about enumeration and hkey handle in Microsoft regarding X64, i did open the key handle, it works in the IDE but then after that again it returns the stripped out name?
Re: X64 W7 Registry Delete Sub Keys
FYI:64_64 or 64_32
on trials i did enumeration key and split keynames, after compile it works fine
i have two function working now, i dont know yet if it will fail, will still see on days to come.
This is one of the solution i arrived, i cant delete with short and straight forward methods.
Open Key
Query Info
Start from highest count
Enumerate
Delete Key
only works like this, if subkey contains subkey then fails
' |-sKey
' -----|subkey1 (with no subkey, able to delete)
' -----|subkey2 (with no subkey, able to delete)
' -----|subkey3 (with no subkey, able to delete)
Code:
'testing this function on X64 bit
'Subkeys of a KEY, Subkey must not contain subkey for it to be deleted
Public Function Delete_KeySubThree(ByVal RootKey As HKEY_Type, ByVal sKey As String) As Boolean
Dim lRetVal As Long, hKey As Long
Dim tFILETIME As FILETIME
Dim bFailed As Boolean
Dim lCtr As Long
Dim sName As String, sClass As String
Dim lNameLen As Long, lClassLen As Long
Dim lNumKeys As Long, lSubkeyNameLen As Long
Dim strKeyName As String
On Error GoTo Handler:
If RegOpenKeyEx(RootKey, sKey, 0, KEY_WOW64_32KEY + KEY_ALL_ACCESS, hKey) = ERROR_NONE Then
' Get subkey information
lRetVal = RegQueryInfoKey(hKey, vbNullString, 0, 0, lNumKeys, lSubkeyNameLen, _
0, 0, 0, 0, 0, tFILETIME)
If lRetVal = ERROR_NONE Then
'Dim sMsg As String
'sMsg = "Key: " & RootKey & "\" & sKey & vbCrLf
'sMsg = sMsg & "Subkeys: " & lNumKeys & vbCrLf
'sMsg = sMsg & "Longest Subkey Name: " & lSubkeyNameLen & vbCrLf
'MsgBox sMsg
'start count from highest no key
For lCtr = lNumKeys - 1 To 0 Step -1
sName = Space(lSubkeyNameLen) & Chr(0)
lNameLen = Len(sName)
sClass = Space$(255)
lClassLen = 255
lRetVal = RegEnumKeyEx(hKey, lCtr, sName, lNameLen, ByVal 0, sClass, lClassLen, tFILETIME)
'(SCENARIO 1) working as of trial, strkeyname is just for debug
If lRetVal = ERROR_NONE Then
sName = Left$(sName, lNameLen)
sClass = Left$(sClass, lClassLen)
strKeyName = strKeyName & sName & "|"
lRetVal = RegDeleteKeyEx(hKey, sName, KEY_WOW64_32KEY + KEY_ALL_ACCESS, ByVal 0)
End If
If lRetVal <> ERROR_NONE Then bFailed = False: Exit For
Next lCtr
If lRetVal = ERROR_NONE Then bFailed = True
RegCloseKey hKey
Else
bFailed = False
End If
End If
Debug.Print strKeyName
Debug.Print bFailed
RegCloseKey hKey
Delete_KeySubThree = (Not bFailed)
Handler:
lRetVal = RegCloseKey(hKey)
On Error GoTo 0
End Function