Solved

200 Points ? Write/Delete to Registry

Posted on 1999-01-09
7
141 Views
Last Modified: 2010-05-03
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
Comment
Question by:samdog
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:mcix
ID: 1455083
Are you wanting to run your program when Windows loads?
0
 

Author Comment

by:samdog
ID: 1455084
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
 
LVL 4

Expert Comment

by:mcix
ID: 1455085
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:samdog
ID: 1455086
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
 
LVL 4

Expert Comment

by:mcix
ID: 1455087
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
 

Author Comment

by:samdog
ID: 1455088
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
 
LVL 4

Accepted Solution

by:
mcix earned 200 total points
ID: 1455089
No Problem... Glad it worked
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now