[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Deleting a registry key

Posted on 2006-05-09
Medium Priority
Last Modified: 2013-12-25
I am trying to delete a registry key from my VB app. I am using following function to delete the registry key.

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

I am getting error code 2, and after looking in the WinError.h, it stands for ERROR_FILE_NOT_FOUND. I have given Full control to everyone for all the registry operations and for this hive as well.

Question by:deming
LVL 12

Expert Comment

ID: 16647417

'=================================[ FORM CODES ]=================================
Private Sub Command1_Click()
  If DeleteRegKey(HKEY_LOCAL_MACHINE, "\Software\MyApplication\MyValue") Then
    MsgBox "Registry key deleted"
    MsgBox "Failed to delete registry key"
  End If
End Sub
'=================================[ FORM CODES ]=================================

'=================================[ MODULE CODES ]=================================
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Enum RegHive
   HKEY_CLASSES_ROOT = &H80000000
   HKEY_CURRENT_USER = &H80000001
   HKEY_LOCAL_MACHINE = &H80000002
End Enum

Public Function DeleteRegKey(ByVal hKey As RegHive, ByVal sPath As String) As Long
 Dim lResult As Long
 lResult = RegDeleteKey(hKey, sPath)
 DeleteRegKey = lResult
End Function
'=================================[ MODULE CODES ]=================================

Author Comment

ID: 16647531
I am also using the same function. but i am getting the return value as 2. Can u explain me why??
LVL 14

Accepted Solution

MilanKM earned 2000 total points
ID: 16649298
Under WindowsNT environment like Win2000 or XP it doesn't work if the key contains subkeys. I think that's why ur getting error. So, u need to delete the subkeys first followed by the parent key. See: www.devx.com/vb2themax/Tip/19133

You can try this: www.vbaccelerator.com/home/VB/Code/Libraries/Registry_and_Ini_Files/Complete_Registry_Control/article.asp


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question