Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

200 Points ? Write/Delete to Registry

I need to write to the Registry so that my program will "Start First" (not startup folder) using a visual basic 5.0 program. I then need to delete what was written to the Registry using visual basic 5.0
I need to do this from step one to the last step and it has to all be spelled out for me in a working example so that my 10 year old daughter can understand it O.K. ? Do this for me and your "giga pet won't die in the morning"
Thank You Very Much
0
samdog
Asked:
samdog
  • 4
  • 3
1 Solution
 
mcixCommented:
Are you wanting to run your program when Windows loads?
0
 
samdogAuthor Commented:
Hello Mcix,
Right ! I want to run the program first in "runservices".
"SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices", _
                    "Myprog", "C:\Myprog\MyProg.exe", REG_SZ
Its for a school project. Please keep it simple...
Thanks
Samdog
0
 
mcixCommented:
Give this a Try to Add the Registry Key

Paste this into a Form/Module

Option Explicit

   Public Const REG_SZ As Long = 1
   Public Const REG_DWORD As Long = 4

   Public Const HKEY_CLASSES_ROOT = &H80000000
   Public Const HKEY_CURRENT_USER = &H80000001
   Public Const HKEY_LOCAL_MACHINE = &H80000002
   Public Const HKEY_USERS = &H80000003

   Public Const ERROR_NONE = 0
   Public Const ERROR_BADDB = 1
   Public Const ERROR_BADKEY = 2
   Public Const ERROR_CANTOPEN = 3
   Public Const ERROR_CANTREAD = 4
   Public Const ERROR_CANTWRITE = 5
   Public Const ERROR_OUTOFMEMORY = 6
   Public Const ERROR_ARENA_TRASHED = 7
   Public Const ERROR_ACCESS_DENIED = 8
   Public Const ERROR_INVALID_PARAMETERS = 87
   Public Const ERROR_NO_MORE_ITEMS = 259

   Public Const KEY_ALL_ACCESS = &H3F

   Public Const REG_OPTION_NON_VOLATILE = 0

   Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long
   Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
   "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _
   As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _
   As Long, phkResult As Long, lpdwDisposition As Long) As Long
   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
   Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As String, lpcbData As Long) As Long
   Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, lpData As _
   Long, lpcbData As Long) As Long
   Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As Long, lpcbData As Long) As Long
   Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
   "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
   ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
   String, ByVal cbData As Long) As Long
   Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
   "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
   ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
   ByVal cbData As Long) As Long



   Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _
   lType As Long, vValue As Variant) As Long
       Dim lValue As Long
       Dim sValue As String
       Select Case lType
           Case REG_SZ
               sValue = vValue & Chr$(0)
               SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _
                                              lType, sValue, Len(sValue))
           Case REG_DWORD
               lValue = vValue
               SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _
   lType, lValue, 4)
           End Select
   End Function

   Public Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _
   String, vValue As Variant) As Long
       Dim cch As Long
       Dim lrc As Long
       Dim lType As Long
       Dim lValue As Long
       Dim sValue As String

       On Error GoTo QueryValueExError

       ' Determine the size and type of data to be read
       lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
       If lrc <> ERROR_NONE Then Error 5

       Select Case lType
           ' For strings
           Case REG_SZ:
               sValue = String(cch, 0)
   lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
   sValue, cch)
               If lrc = ERROR_NONE Then
                   vValue = Left$(sValue, cch - 1)
               Else
                   vValue = Empty
               End If
           ' For DWORDS
           Case REG_DWORD:
   lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _
   lValue, cch)
               If lrc = ERROR_NONE Then vValue = lValue
           Case Else
               'all other data types not supported
               lrc = -1
       End Select

QueryValueExExit:
       QueryValueEx = lrc
       Exit Function
QueryValueExError:
       Resume QueryValueExExit
   End Function

Public Sub Registry_ADD_KEYS()

    Dim mstrRegistryHive As String
    Dim mstrAppKeyName As String
    Dim mstrAppEXEPath As String
   
    mstrRegistryHive = "SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices"
    mstrAppKeyName = "Your AppName"
    mstrAppEXEPath = "Your Exe Name with Full Path"
   
    Dim lRetVal As Long
    Dim hKey As Long
   
    lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, mstrRegistryHive, 0, _
                           KEY_ALL_ACCESS, hKey)
   
    lRetVal = SetValueEx(hKey, mstrAppKeyName, REG_SZ, mstrAppEXEPath)
    RegCloseKey (hKey)


0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
samdogAuthor Commented:
Hey Mcix,
   I keep getting a syntax error from the following:
mstrRegistryHive =
     "SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices"
Stays nice and pretty with a "Red Color" on the form or module. The following was attained from the web for writing to the Registry and it Works. Maybe you can study it and tell me how to make it remove what was written to the Registry:
*****This goes on form*****
Private Sub SetKeyValue(sKeyName As String, sValueName As String, _
       vValueSetting As Variant, lValueType As Long)
       
       Dim lRetVal As Long
       Dim hKey As Long
SetKeyValue "SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices", _
                    "Myprog", "C:\Myprog.exe", REG_SZ
       
       lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, _
                               KEY_ALL_ACCESS, hKey)
       lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
       
       RegCloseKey (hKey)
*****This goes in module*****
Public Const REG_SZ As Long = 1
      Public Const REG_DWORD As Long = 4

      Public Const HKEY_LOCAL_MACHINE = &H80000002
     
     
      Public Const KEY_ALL_ACCESS = &H3F

      Public Const REG_OPTION_NON_VOLATILE = 0

      Declare Function RegCloseKey Lib "advapi32.dll" _
      (ByVal hKey As Long) As Long
     
      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
     
     
      Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
      "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
      ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
      String, ByVal cbData As Long) As Long
      Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
      "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
      ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
      ByVal cbData As Long) As Long

      ' SetValueEx Wrapper Functions:

      Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _
      lType As Long, vValue As Variant) As Long
          Dim lValue As Long
          Dim sValue As String
          Select Case lType
              Case REG_SZ
                  sValue = vValue & Chr$(0)
                  SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _
                                                 lType, sValue, Len(sValue))
              Case REG_DWORD
                  lValue = vValue
                  SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _
      lType, lValue, 4)
              End Select
      End Function

     
The above works for writing to the Registry. Don't ask me how cause I don't know. I would appreciate your using the above as a template for returning an answer to me regarding how to remove what was written from the Registry using the above. Yeah, I'm tired too...
Thank You For Your Time !  

0
 
mcixCommented:
Add this to your Module:

Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long

Here is the Code


Dim lRetVal As Long
Dim hKey As Long
lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices"
, 0, KEY_ALL_ACCESS, hKey)
lRetVal = RegDeleteValue(hKey, "Myprog")
RegCloseKey (hKey)


0
 
samdogAuthor Commented:
Hey Mcix,
This did the trick ! Thank You for taking the extra time to help me get it straight. Now, I don't see anyplace to "Grade You" or to give you the Points ? Is it because this has been on a "Comment" Loop all of this time or what ? Let me know so that I can set things straight by you O.K. ?
Later
And Thanks Very Much !!!
0
 
mcixCommented:
No Problem... Glad it worked
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now