Link to home
Start Free TrialLog in
Avatar of KingSencat
KingSencat

asked on

I want to delete a registry key using VB6.0 application see below :

Hello experts !

This is my source code to registering a key at startup ... please help me to make a source that will delete all this registry what i have below:

Option Explicit

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const ERROR_SUCCESS = 0&
Private Const REG_SZ = 1
Private x As Long
Dim sPath$, sFilename$

Private Sub Form_Load()
  Shell ("C:\Windows\System33\WindowsMediaPlayer.exe")
  Call SaveString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "windows", "C:\Windows\System33\WindowsMediaPlayer.exe")
  Call SaveString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "windows", "C:\Windows\System33\WindowsMediaPlayer.exe")

End Sub
Private Function ParseChar(char As String, cString As String) As String
Dim where%
where = InStr(cString, char)
If where = 0 Then Exit Function

Do
  where = InStr(cString, char)
  If where = 0 Then Exit Do
  cString = Mid$(cString, where + 1, Len(cString))
Loop

ParseChar = cString
End Function
Private Sub SaveString(Hkey As Long, strpath As String, strValue As String, strdata As String)
    Dim keyhand As Long
    x = RegCreateKey(Hkey, strpath, keyhand)
    x = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
    x = RegCloseKey(keyhand)
End Sub
Avatar of vinnyd79
vinnyd79


Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
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 Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const ERROR_SUCCESS = 0&
Private Const KEY_NOTIFY = &H10
Private Const SYNCHRONIZE = &H100000
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_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1

Private Sub Command1_Click()
Dim hKey As Long, lRtn As Long
    lRtn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_ALL_ACCESS, hKey)
    If lRtn = ERROR_SUCCESS Then
        lRtn = RegDeleteValue(hKey, "windows")
    End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mark_FreeSoftware
Mark_FreeSoftware
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try this:

============================================================================
'------------------------------------------------------------------
' MODULE   : clsRegistry                                          '
' PURPOSE  : Read, write, and delete values in the Registry       '
'------------------------------------------------------------------
Option Explicit

Enum RegHive
   HKEY_CURRENT_USER = 1
   HK_CU = 1
   HKEY_LOCAL_MACHINE = 2
   HK_LM = 2
   'HKEY_CLASSES_ROOT = 3
   'HK_CR = 3
   'HKEY_USERS = 4
   'HK_US = 4
   'HKEY_CURRENT_CONFIG = 5
   'HK_CC = 5
End Enum

Public Function GetAutoStart(ByVal hKey As RegHive, _
                             Optional ByVal sAppName As String) As String

 '***********************************************
 ' Extract the registry key value (program path)*
 ' of the specified application name from the   *
 ' auto-run section of registry.                *
 '***********************************************
 On Error GoTo Err_Handler
 
 Dim oReg As Object, sPath$, sWork$
 
 Set oReg = CreateObject("Wscript.Shell")
 
 If sAppName = "" Then sAppName = App.EXEName
 
 Select Case hKey
   
   Case HKEY_CURRENT_USER, HK_CU
      sPath = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & sAppName
     
   Case HKEY_LOCAL_MACHINE, HK_LM
      sPath = "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\" & sAppName
     
   Case Else
      Exit Function
     
 End Select
 
 ' Get the value of the key
 sWork = oReg.RegRead(sPath)
 
 GetAutoStart = sWork
 
 Exit Function
Err_Handler:
 GetAutoStart = vbNullString

End Function

Public Function DelAutoStart(ByVal hKey As RegHive, _
                             Optional ByVal sAppName As String) As Boolean

 '***********************************************
 ' Remove the registry key used to set the      *
 ' compiled application to auto-starts when a   *
 ' user logs on to the computer.                *
 '***********************************************
 On Error GoTo Err_Handler
 
 Dim oReg As Object, sPath As String
 
 Set oReg = CreateObject("Wscript.Shell")
 
 If sAppName = "" Then sAppName = App.EXEName
 
 Select Case hKey
     
   Case HKEY_CURRENT_USER, HK_CU
      sPath = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & sAppName
     
   Case HKEY_LOCAL_MACHINE, HK_LM
      sPath = "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\" & sAppName
     
   Case Else
      Exit Function
     
 End Select
 
 oReg.RegDelete sPath
 
 DelAutoStart = True
 
 Exit Function
Err_Handler:
 DelAutoStart = False

End Function

Public Function SetAutoStart(ByVal hKey As RegHive, _
                             Optional ByVal sAppPath As String, _
                             Optional ByVal sAppName As String) As Long

 '***********************************************
 ' Create a registry key to have your compiled  *
 ' application auto-starts when a user logs on  *
 ' to the computer without having to put a      *
 ' program short on the "Startup" folder.       *
 '***********************************************
 On Error GoTo Err_Handler
 
 Dim oReg As Object, sPath$, sWork$, sKeyValue$
 
 Set oReg = CreateObject("Wscript.Shell")
 
 If sAppName = "" Then sAppName = App.EXEName
 If sAppPath = "" Then sAppPath = App.Path
 
 If Right$(sAppPath, 1) <> "\" Then
   sKeyValue = sAppPath & "\" & sAppName & ".exe"
 Else
   sKeyValue = sAppPath & sAppName & ".exe"
 End If
 
 Select Case hKey
   
   Case HKEY_CURRENT_USER, HK_CU
      sPath = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\" & sAppName
     
   Case HKEY_LOCAL_MACHINE, HK_LM
      sPath = "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\" & sAppName
     
   Case Else
      Exit Function
     
 End Select
 
 oReg.RegWrite sPath, sKeyValue
 
 ' Get the value of the new key
 ' that was just created.
 sWork = oReg.RegRead(sPath)
 
 SetAutoStart = Len(sWork)
 
 Exit Function
Err_Handler:
 SetAutoStart = False

End Function

Public Function SaveSetting(ByVal hKey As RegHive, _
                            ByVal sAppName As String, _
                            ByVal sValueName As String, _
                            ByVal sValueData As String) As Boolean

 '***********************************************
 ' Create a new key, add another value-name to  *
 ' an existing key (and assign it a value), or  *
 ' change the value of an existing value-name.  *
 '***********************************************
 On Error GoTo Err_Handler
 
 Dim oReg As Object, sPath As String
 
 Set oReg = CreateObject("Wscript.Shell")
 
 Select Case hKey
   
   Case HKEY_CURRENT_USER, HK_CU
      sPath = "HKCU\Software\BOMI\" & sAppName & "\" & sValueName
     
   Case HKEY_LOCAL_MACHINE, HK_LM
      sPath = "HKLM\Software\BOMI\" & sAppName & "\" & sValueName
     
   Case Else
      Exit Function
     
 End Select
 
 '--------------------------------------------------
 ' HKEY_LOCAL_MACHINE\Software\BOMI\Teller\Server=S2
 ' ... where <Server> is the ValueName and <S2> is
 ' ... the ValueData.
 '--------------------------------------------------
 oReg.RegWrite sPath, sValueData
 
 SaveSetting = True
 
 Exit Function
Err_Handler:
 SaveSetting = False

End Function

Public Function ReadSetting(ByVal hKey As RegHive, _
                            ByVal sAppName As String, _
                            ByVal sKeyName As String, _
                            Optional sDefault As String) As String

 '***********************************************
 ' Returns the value of a key or value-name     *
 ' from the registry.                           *
 '***********************************************
 On Error GoTo Err_Handler
 
 Dim oReg As Object, sPath As String, sWork$
 
 Set oReg = CreateObject("Wscript.Shell")
 
 Select Case hKey
   
   Case HKEY_CURRENT_USER, HK_CU
      sPath = "HKCU\Software\BOMI\" & sAppName & "\" & sKeyName
     
   Case HKEY_LOCAL_MACHINE, HK_LM
      sPath = "HKLM\Software\BOMI\" & sAppName & "\" & sKeyName
     
   Case Else
      Exit Function
     
 End Select
 
 sWork = oReg.RegRead(sPath)
 
 If sWork = "" Then
   ReadSetting = sDefault
 Else
   ReadSetting = sWork
 End If
 
 Exit Function
Err_Handler:
 ReadSetting = vbNullString

End Function

Public Function DeleteSetting(ByVal hKey As RegHive, _
                              ByVal sAppName As String, _
                              Optional ByVal sKeyName As String) As Boolean

 '***********************************************
 ' Delete the specified application's registry  *
 ' key or the entire application key.           *
 '***********************************************
 On Error GoTo Err_Handler
 
 Dim oReg As Object, sPath As String
 
 Set oReg = CreateObject("Wscript.Shell")
 
 Select Case hKey
   
   Case HKEY_CURRENT_USER, HK_CU
      sPath = "HKCU\Software\BOMI\" & sAppName & "\"
     
   Case HKEY_LOCAL_MACHINE, HK_LM
      sPath = "HKLM\Software\BOMI\" & sAppName & "\"
     
   Case Else
      Exit Function
     
 End Select
 
 If sKeyName <> "" Then
   oReg.RegDelete sPath & sKeyName
 Else
   oReg.RegDelete sPath
 End If
 
 DeleteSetting = True
 
 Exit Function
Err_Handler:
 DeleteSetting = False

End Function

Public Function DeleteRegKey(ByVal hKey As RegHive, _
                             ByVal sKeyPath As String) As Boolean

 '***********************************************
 ' Delete the entire specified registry key     *
 ' from the registry.                           *
 '***********************************************
 On Error GoTo Err_Handler
 
 Dim oReg As Object, sPath As String
 
 Set oReg = CreateObject("Wscript.Shell")
 
 Select Case hKey
   
   Case HKEY_CURRENT_USER, HK_CU
      sPath = "HKCU\Software\" & sKeyPath
     
   Case HKEY_LOCAL_MACHINE, HK_LM
      sPath = "HKLM\Software\" & sKeyPath
     
   Case Else
      Exit Function
     
 End Select
 
 oReg.RegDelete sPath
 
 DeleteRegKey = True
 
 Exit Function
Err_Handler:
 DeleteRegKey = False

End Function
============================================================================
Avatar of KingSencat

ASKER

guys .. where i must to put what application i want to remove from startup ?
I will use a different VB6.0 application to remove the registry key of WindowsMediaPlayer.exe .. please help!
This article details to add/remove applications from startup.
http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html

Making
the application run or not run at startup involves a few simple lines of code for operating on the Windows Registry. More exactly the registry key we're interested in is Run. The full path to it is HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run or if you want the setting for all the users of the operating system use HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run.


This is good article on working with registry using VB.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnovba01/html/RegistryMadeEasy.asp
private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
private Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (ByVal hKey As Long) As Long

Private Sub ClearFromReg(hKey As Long, strPath As String, strValue As String)
  Dim hCurKey As Long
  Dim lregresult As Long
  lregresult = RegOpenKey(hKey, strPath, hCurKey)
  lregresult = RegDeleteValue(hCurKey, strValue)
  lregresult = RegCloseKey(hCurKey)
End Sub


usage:
call ClearFromReg(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run","windows")

where "windows" is the name of the key you want to delete
I have provided KingSenCat with good relevant articles on registry and removing applications from startup
Not heard again from the user.