Solved

Writing to Registry

Posted on 1999-01-06
5
183 Views
Last Modified: 2012-05-04
I can place my program into the registry by hand doing the following: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices. I then "right click" for New and select string value as well as path. Now my program will "Run First". Is there a way to write this information into the Registry from my visual basic program ? Please advise for the "complete idiot"...Thanks

0
Comment
Question by:sierra7
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
spenner earned 100 total points
ID: 1454342
Here's the complete code I use for my registry manipulation

In your case, you would only need to use the setkeyvalue function
SetKeyValue "\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices\MyProgexe", "StringValue", "C:\it\MyProgExe.exe", REG_SZ

You don't actually need most of the code I sent below, but I sent the whole set along in case you wanted it.

'@@@@@@@@ Insert this Code into a Module @@@@@@@@

   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

   ' SetValueEx and QueryValueEx 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

   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



'@@@@@@@@ Insert this Code into a Form @@@@@@@@

'*********************************************************************
' Create a new Registry Key
' - Call it like this
'   CreateNewKey "TestKey\SubKey1\SubKey2", HKEY_LOCAL_MACHINE
'
' This will create three-nested keys beginning with TestKey
' immediately under HKEY_LOCAL_MACHINE, SubKey1 subordinate
' to TestKey, and SubKey3 under SubKey2.
'

Private Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
    Dim hNewKey As Long         'handle to the new key
    Dim lRetVal As Long         'result of the RegCreateKeyEx function
   
    lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _
                 vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _
                 0&, hNewKey, lRetVal)
    RegCloseKey (hNewKey)
End Sub

'*********************************************************************
' Set a Registry Key Value
' - Call it like this
'   SetKeyValue "TestKey\SubKey1", "StringValue", "Hello", REG_SZ
'
'  This will create a value of type REG_SZ called "SubKey1" with
'  the setting of "Hello." This value will be associated with the
'  key SubKey1 of "TestKey."
'

Private Sub SetKeyValue(sKeyName As String, sValueName As String, _
    vValueSetting As Variant, lValueType As Long)
   
    Dim lRetVal As Long         'result of the SetValueEx function
    Dim hKey As Long         'handle of open key

    'open the specified key
    lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, _
                            KEY_ALL_ACCESS, hKey)
    lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
    RegCloseKey (hKey)
End Sub

'*********************************************************************
' Query the value of a key
' - Call it like this
'   QueryValue "TestKey\SubKey1", "StringValue"
'
'  This will display a message box with the current setting of
'  the "StringValue" value, and assumes that "StringValue" exists
'  in the "TestKey\SubKey1" key.  If the Value that you query
'  does not exist then QueryValue will return an error code of
'  2 - 'ERROR_BADKEY'.
'

Private Sub QueryValue(sKeyName As String, sValueName As String)
    Dim lRetVal As Long         'result of the API functions
    Dim hKey As Long         'handle of opened key
    Dim vValue As Variant      'setting of queried value

    lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, _
        KEY_ALL_ACCESS, hKey)
    lRetVal = QueryValueEx(hKey, sValueName, vValue)
    MsgBox vValue             ' vValue stores the returned value
    RegCloseKey (hKey)
End Sub
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1454343
See also the following question I put up a while ago with an accepted answer by "waty"

http://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10108254


0
 

Author Comment

by:sierra7
ID: 1454344
I must be doing something wrong. I pasted the information into a module and then I pasted the following into a form:SetKeyValue
   "\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices\MyProgexe",
   "StringValue", "C:\it\MyProgExe.exe", REG_SZ
It still won't write to the Registry. Could you send instructions from A to Z for the Complete Idiot. I am a Novice !
Thank You For Your Time...
0
 
LVL 2

Expert Comment

by:spenner
ID: 1454345
Sorry - When I typed it out I accidentaly changed it - doh!
The real command you want to use is

   SetKeyValue "SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices", _
               "MyProgexe", "C:\it\MyProgExe.exe", REG_SZ

0
 

Author Comment

by:sierra7
ID: 1454346
Thanks Spenner. The revised code worked very well. You are "Da One" ! I wish to thank caraf_g for the imput too. I'm impressed with both of you as teachers for the complete idiots of the world. Keep up the good work...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

14 Experts available now in Live!

Get 1:1 Help Now