Write a String to the Registry with VB6

Posted On 2016-05-29 by VB6Boy
Tags: Visual Basic 6 Tip Tutorial Windows
Views: 1549

Just a little snippet which will show you how to write a string value to the registry with Visual Basic 6.

Option Explicit

 HKEY_DYN_DATA = &H80000006
 HKEY_USERS = &H80000003
End Enum

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 RegCloseKey Lib _
   "advapi32.dll" (ByVal Hkey 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 REG_SZ = 1

Private Function WriteStringToRegistry(Hkey As _
  REG_TOPLEVEL_KEYS,strPath As String, strValue As String, _
  strdata As String) As Boolean

'Hkey: Top Level Key as defined by
'REG_TOPLEVEL_KEYS Enum (See Declarations)

'strPath - 'Full Path of Subkey
'if path does not exist it will be created

'strValue ValueName

'strData - Value Data

'Returns: True if successful, false otherwise

'WriteStringToRegistry(HKEY_LOCAL_MACHINE, _
"Software\Microsoft", "PackageName", "")

Dim bAns As Boolean

On Error GoTo ErrorHandler

   Dim keyhand As Long
   Dim r As Long
   r = RegCreateKey(Hkey, strPath, keyhand)
   If r = 0 Then
        r = RegSetValueEx(keyhand, strValue, 0, _
           REG_SZ, ByVal strdata, Len(strdata))
        r = RegCloseKey(keyhand)
    End If
   WriteStringToRegistry = (r = 0)

Exit Function
    WriteStringToRegistry = False
    Exit Function
End Function

About the Author

VB6Boy has posted a total of 5 articles.


Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

Job Spammer: Sathya Narayana
dwirch posted on March 15, 2017 at about 7:18 in Spammers

Job Spammer: Manish Kumar
dwirch posted on March 14, 2017 at about 17:44 in Spammers

Site Migration Complete
dwirch posted on March 12, 2017 at about 11:10 in Site News

Site Migration Complete
dwirch posted on March 11, 2017 at about 0:35 in Site News

Job Spammer: Jason Weinstein
dwirch posted on March 10, 2017 at about 14:00 in Spammers

Job Spammer: Ryan Wilson
dwirch posted on March 8, 2017 at about 17:00 in Spammers