Cannot Delete Registry Key in Windows 7 64-bit

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 5166 Views - Last Post: 04 July 2012 - 01:03 PM Rate Topic: -----

#1 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 01:58 PM

Hi there,

I'm trying to delete registry key from Windows 7 64-bit registry, defined everything fine, the API wont delete registry key, but the function returns me 0 to ERROR_SUCCESS and TRUE value, like it was successfully deleted the key, but the key is still remains there...

Option Explicit

Private Const ERROR_SUCCESS = 0&

Public Enum HKEY_Type
 HKEY_CLASSES_ROOT = &H80000000
 HKEY_CURRENT_USER = &H80000001
 HKEY_LOCAL_MACHINE = &H80000002
 HKEY_USERS = &H80000003
 HKEY_PERFORMANCE_DATA = &H80000004
 HKEY_CURRENT_CONFIG = &H80000005
 HKEY_DYN_DATA = &H80000006
End Enum

Private Const KEY_ALL_ACCESS = &H3F
Private Const KEY_WOW64_64KEY As Long = &H100&

Private Declare Function RegDeleteKeyEx Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal samDesired As Long, ByVal Reserved As Long) As Long

Public Function DeleteKey(ByVal RootKey As HKEY_Type, ByVal SubKey As String) As Boolean
 On Error Resume Next
 
 Dim lgRet As Long
 
 lgRet = RegDeleteKeyEx(RootKey, SubKey, KEY_ALL_ACCESS Or KEY_WOW64_64KEY, 0&)

 If lgRet = ERROR_SUCCESS Then
  DeleteKey = True
 Else
  DeleteKey = False
 End If
 
 Exit Function
End Function


I wish to delete custom created key by me, calling:

Dim blRet As Boolean
blRet = DeleteKey(HKEY_LOCAL_MACHINE, "Software\Test By Me")


Just to mention I CAN WRITE, READ, DELETE VALUES to/from 64-bit registry, but I can't DELETE a key...everything working great except that DELETE KEY function!

p.s. UAC is disabled, so virtualisation not an issue here.

Thanks for your time and for your help! :smile2:

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Delete Registry Key in Windows 7 64-bit

#2 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 268
  • Joined: 21-May 09

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 03:26 PM

Sorry Neku, I edited your post by mistake instead of posting my own. My bad.

This post has been edited by BobRodes: 01 July 2012 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,303
  • Joined: 28-March 11

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 03:27 PM

GRRRRRR, why do people use *ALL_ACCESS for files and the Registry? Ask only for the access right you need!!!

Now, what makes you think a normal user can delete anything from any key outside of HKCU? Just because "It worked on XP/2K"? Windows 7 and above are different beasts.

Can you delete it while logged in as Admin? How do you know the key is still there? Did you refresh (F5) Regedit?

Does the key have subkeys? If so, you must enumerate the subkeys and delete each one before deleting the parent. You could use SHDeleteKey which deletes a key and all subkeys.

Oh, and your getting good answers over here
Was This Post Helpful? 1
  • +
  • -

#4 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 04:15 PM

View PostNeku, on 29 June 2012 - 11:26 PM, said:

uumm.. maybe you simply cant delete a key?
a key is for example HKEY_CLASSES_ROOT
unless it have value i guess the OS would simply ignore it...

maybe you write the value Software\Test By Me into a key?

please post the codes for write read and delete values

do you also have a code for creating a key?


"Test By Me" is a SubKey and no Values/Data in it...

Here is the code for creating a key:

Dim hCurKey As Long
Dim SA As SECURITY_ATTRIBUTES
 
SA.nLength = Len(SA)
SA.bInheritHandle = CLng(True)

Call RegCreateKeyEx(hKey, stPath, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS Or KEY_WOW64_64KEY, SA, hCurKey, 0&)

Was This Post Helpful? 0
  • +
  • -

#5 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 04:23 PM

View PostGunnerInc, on 29 June 2012 - 11:27 PM, said:

GRRRRRR, why do people use *ALL_ACCESS for files and the Registry? Ask only for the access right you need!!!

Now, what makes you think a normal user can delete anything from any key outside of HKCU? Just because "It worked on XP/2K"? Windows 7 and above are different beasts.

Can you delete it while logged in as Admin? How do you know the key is still there? Did you refresh (F5) Regedit?

Does the key have subkeys? If so, you must enumerate the subkeys and delete each one before deleting the parent. You could use SHDeleteKey which deletes a key and all subkeys.

Oh, and your getting good answers over here


1. So, what Access Right I need?

2. ???

3. Yes, used as Admin and Yes, refreshed in Regedit by pressing F5 key and the Key is still there.

4. No, there is no SubKeys in it, just that one empty Key.

5. That is also my post, no one can give me the answer there.
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,303
  • Joined: 28-March 11

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 04:32 PM

This is wrong: "Software\Test By Me"

Should be "\Software\Test By Me"
Was This Post Helpful? 1
  • +
  • -

#7 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 268
  • Joined: 21-May 09

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 04:36 PM

View PostGunnerInc, on 30 June 2012 - 02:32 AM, said:

This is wrong: "Software\Test By Me"

Should be "\Software\Test By Me"



good point, maybe thats why it did not work
Was This Post Helpful? 0
  • +
  • -

#8 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 04:41 PM

View PostGunnerInc, on 30 June 2012 - 12:32 AM, said:

This is wrong: "Software\Test By Me"

Should be "\Software\Test By Me"


No, it's not wrong, everything working like it should, just the Delete Key wont work on Win7 64-bit!

btw...If I replace what you proposed, it gives me error 161
Was This Post Helpful? 0
  • +
  • -

#9 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 04:55 PM

View PostNeku, on 30 June 2012 - 12:36 AM, said:

View PostGunnerInc, on 30 June 2012 - 02:32 AM, said:

This is wrong: "Software\Test By Me"

Should be "\Software\Test By Me"



good point, maybe thats why it did not work


Are both of you serious or just joking with me? :oops:

Go to this page and see the example on the bottom of the page or better here.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 29 June 2012 - 05:15 PM

View PostGunnerInc, on 29 June 2012 - 06:27 PM, said:

GRRRRRR, why do people use *ALL_ACCESS for files and the Registry? Ask only for the access right you need!!!

Typical Windows mentality. All access, all the time. Every user is admin. Everything is full-bore, full-throttle.
Was This Post Helpful? 0
  • +
  • -

#11 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 30 June 2012 - 01:40 AM

Does anybody know the correct answer to my question or not?

Did somebody test my code posted above to see what is the issue or just guessing around?


Settle down, bro. There's a saying "Don't look a gift horse in the mouth". If you don't understand it, suppose you look it up.

This post has been edited by BobRodes: 01 July 2012 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#12 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,796
  • Joined: 26-March 09

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 30 June 2012 - 02:23 AM

What's the full key you're creating and or values you put under it (Full tree).
Was This Post Helpful? 0
  • +
  • -

#13 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 30 June 2012 - 02:52 AM

View Postmaj3091, on 30 June 2012 - 10:23 AM, said:

What's the full key you're creating and or values you put under it (Full tree).


Whatever Key or Value I created using API, I cannot delete it:

HKEY_LOCAL_MACHINE, "Software\Test By Me\", "Data", "test"
HKEY_LOCAL_MACHINE, "Software\Reg3264 Component\", "Data", "test"


I can Write, Read, Delete Value(s) from created key...but cannot Delete Key itself, even if it's empty!

See screenshot attached.

p.s. If I create manually whatever key in "Regedit" and try that to delete using the same API, I get the same result, 0 successfully deleted, but the key still remains in the registry.
Was This Post Helpful? 0
  • +
  • -

#14 beic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-June 12

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 30 June 2012 - 02:57 AM

Attached screenshot...

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Cannot Delete Registry Key in Windows 7 64-bit

Posted 30 June 2012 - 02:58 AM

What happens if you run your program as administrator?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2