borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (what form on the screen)Next Topic (if...if...else/if elseif.../if...ifnot) New Topic New Poll Post Reply
AndreaVB Forum : Frequently Asked Questions : Regarding Registry and Visual Basic
Poster Message
loopers
Level: Guest


icon Regarding Registry and Visual Basic

I am a beginner in Visual Basic 6.0. I would like to know how can I add data to the registry using Visual Basic. I have read the article from http://www.andreavb.com/tip080001.html but still don't understand much.

The code that I saw once I run Visual Basic and double click on the form is

Private Sub Form_Load()

End Sub



Can anybody give me just a clear and simple example on how to add a registry on a particular location on a Windows 98 machine? Thanks.

21-04-2003 at 05:33 PM
| Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1658
icon Re: Regarding Registry and Visual Basic

The code at that address tells where to put it. To add/delete you just call the appropriate subroutine, with the required parameters.

The parameters are based on what/where you want to view/add.

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

21-04-2003 at 07:11 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
loopers
Level: Guest

icon Simpler Code Needed

I need a code that is more simple. I believe there is only a few lines to modify the registry. Can anybody pls show one very very simple example, pls?

Thanks.

22-04-2003 at 02:34 AM
| Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1658
icon Re: Regarding Registry and Visual Basic

A few lines? There's more than a few lines with everything, unless you just add lines for your app in a pre-designated key, then it's just the but you can only see the running app's registry with these:

SaveSetting appname, section, key, setting
GetSetting appname, section, key[, default]
GetAllSettings appname, section



____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

22-04-2003 at 03:29 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
mross01
Level: Trainee

Registered: 22-04-2003
Posts: 1
icon Re: Regarding Registry and Visual Basic

Me.WindowState = GetSetting(App.Title, Me.Name, "WindowState", vbMaximized)

Call SaveSetting(App.Title, Me.Name, "WindowState", Me.WindowState)

This code saves the windowstate (minimized, Size, etc) to the Registry and retrieves it.

Look up the parameters in VB Help (MSDN)

Good Luck

Marc

[Edited by mross01 on 21-04-2003 at 09:30 PM GMT]

22-04-2003 at 03:29 AM
View Profile Send Email to User Show All Posts | Quote Reply
loopers
Level: Guest

icon Re: Regarding Registry and Visual Basic

I appreciate all the guidance that you have given me in this forum. But I am truly a beginner in Visual Basic.

Ok, let us say I have double click on the button, here is what I see ...

Private Sub Command1_Click()
`What are the code I need to put here to add data to the registry?
End Sub

Thanks.

22-04-2003 at 04:00 AM
| Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1658
icon Re: Regarding Registry and Visual Basic


' where appname is the appname
' section is the section (folder) you want it stored in
' key the - the name of the key (variable)
' setting - the value
SaveSetting appname, section, key, setting
'Or like for the windowstate
SaveSetting App.Title, Me.Name, "WindowState", Me.WindowState


____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

22-04-2003 at 05:52 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
win_dir
Level: VB Guru

Registered: 04-08-2002
Posts: 390
icon Re: Regarding Registry and Visual Basic

If you want to modify  the registry as a beginner you may want to use the "ITM Registry Control", its only about 300k. Just make sure you READ THE DOCUMENTATION, otherwise you'll be there for hours figuring out how it works. You can read/download it here: http://download.com.com/3000-2070-2376961.html?tag=lst-0-6

However, if you just need to save some of your programs settings to the registry you can use the method highlighted by JLRodgers

[Edited by win_dir on 23-04-2003 at 07:39 AM GMT]

____________________________
We have a Mustek 5 M/Pix GSm@rt USB Digital Camera
for just £74.03 , offer only on until 30th February!

<AllDuck.com>      

Enquiries/Sales: 0845 430 9862
Fax: 0870 950 4532

23-04-2003 at 07:39 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
dcostelloe
Level: VB Guru

Registered: 11-06-2002
Posts: 74
icon Re: Regarding Registry and Visual Basic

Hi,
If you have VB 6.0 pro or enterprise under Add in Manager look for

1.VB 6 Template Manager
2. Select Loaded
3. Select Tools
4. select Add Code Snipet
5. select Registry Access

All required registry code will be added to your project
Including sample of how to use the functions.

Tip: if you look at the setup for the package manager you will find all possible code samples for pretty much everything  

If that don't help you here it is:
Throw this into a module

' This module reads and writes registry keys.  Unlike the
' internal registry access methods of VB, it can read and
' write any registry keys with string values.

'---------------------------------------------------------------
'-Registry API Declarations...
'---------------------------------------------------------------
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" 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, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

'---------------------------------------------------------------
'- Registry Api Constants...
'---------------------------------------------------------------
' Reg Data Types...
Const REG_SZ = 1                         ' Unicode nul terminated string
Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
Const REG_DWORD = 4                      ' 32-bit number

' Reg Create Type Values...
Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted

' Reg Key Security Options...
Const READ_CONTROL = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Const KEY_EXECUTE = KEY_READ
Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
                       KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
                       KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
                    
' Reg Key ROOT Types...
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004

' Return Value...
Const ERROR_NONE = 0
Const ERROR_BADKEY = 2
Const ERROR_ACCESS_DENIED = 8
Const ERROR_SUCCESS = 0

'---------------------------------------------------------------
'- Registry Security Attributes TYPE...
'---------------------------------------------------------------
Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Boolean
End Type



'-------------------------------------------------------------------------------------------------
'sample usage - Debug.Print UpodateKey(HKEY_CLASSES_ROOT, "keyname", "newvalue")
'-------------------------------------------------------------------------------------------------
Public Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubKeyValue As String) As Boolean
    Dim rc As Long                                      ' Return Code
    Dim hKey As Long                                    ' Handle To A Registry Key
    Dim hDepth As Long                                  '
    Dim lpAttr As SECURITY_ATTRIBUTES                   ' Registry Security Type
    
    lpAttr.nLength = 50                                 ' Set Security Attributes To Defaults...
    lpAttr.lpSecurityDescriptor = 0                     ' ...
    lpAttr.bInheritHandle = True                        ' ...

    '------------------------------------------------------------
    '- Create/Open Registry Key...
    '------------------------------------------------------------
    rc = RegCreateKeyEx(KeyRoot, KeyName, _
                        0, REG_SZ, _
                        REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
                        hKey, hDepth)                   ' Create/Open //KeyRoot//KeyName
    
    If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Errors...
    
    '------------------------------------------------------------
    '- Create/Modify Key Value...
    '------------------------------------------------------------
    If (SubKeyValue = "") Then SubKeyValue = " "        ' A Space Is Needed For RegSetValueEx() To Work...
    
    ' Create/Modify Key Value
    rc = RegSetValueEx(hKey, SubKeyName, _
                       0, REG_SZ, _
                       SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode)))
                      
    If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Error
    '------------------------------------------------------------
    '- Close Registry Key...
    '------------------------------------------------------------
    rc = RegCloseKey(hKey)                              ' Close Key
    
    UpdateKey = True                                    ' Return Success
    Exit Function                                       ' Exit
CreateKeyError:
    UpdateKey = False                                   ' Set Error Return Code
    rc = RegCloseKey(hKey)                              ' Attempt To Close Key
End Function

'-------------------------------------------------------------------------------------------------
'sample usage - Debug.Print GetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1CLSID", "")
'-------------------------------------------------------------------------------------------------
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
    Dim i As Long                                           ' Loop Counter
    Dim rc As Long                                          ' Return Code
    Dim hKey As Long                                        ' Handle To An Open Registry Key
    Dim hDepth As Long                                      '
    Dim sKeyVal As String
    Dim lKeyValType As Long                                 ' Data Type Of A Registry Key
    Dim tmpVal As String                                    ' Tempory Storage For A Registry Key Value
    Dim KeyValSize As Long                                  ' Size Of Registry Key Variable
    
    ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
    '------------------------------------------------------------
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open Registry Key
    
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Error...
    
    tmpVal = String$(1024, 0)                             ' Allocate Variable Space
    KeyValSize = 1024                                       ' Mark Variable Size
    
    '------------------------------------------------------------
    ' Retrieve Registry Key Value...
    '------------------------------------------------------------
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
                         lKeyValType, tmpVal, KeyValSize)    ' Get/Create Key Value
                        
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors
      
    tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)

    '------------------------------------------------------------
    ' Determine Key Value Type For Conversion...
    '------------------------------------------------------------
    Select Case lKeyValType                                  ' Search Data Types...
    Case REG_SZ, REG_EXPAND_SZ                              ' String Registry Key Data Type
        sKeyVal = tmpVal                                     ' Copy String Value
    Case REG_DWORD                                          ' Double Word Registry Key Data Type
        For i = Len(tmpVal) To 1 Step -1                    ' Convert Each Bit
            sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value Char. By Char.
        Next
        sKeyVal = Format$("&h" + sKeyVal)                     ' Convert Double Word To String
    End Select
    
    GetKeyValue = sKeyVal                                   ' Return Value
    rc = RegCloseKey(hKey)                                  ' Close Registry Key
    Exit Function                                           ' Exit
    
GetKeyError:    ' Cleanup After An Error Has Occured...
    GetKeyValue = vbNullString                              ' Set Return Val To Empty String
    rc = RegCloseKey(hKey)                                  ' Close Registry Key
End Function





quote:
loopers wrote:
I am a beginner in Visual Basic 6.0. I would like to know how can I add data to the registry using Visual Basic. I have read the article from http://www.andreavb.com/tip080001.html but still don't understand much.

The code that I saw once I run Visual Basic and double click on the form is

Private Sub Form_Load()

End Sub

Now thats what I call cool wizard  
Hope it helps


Can anybody give me just a clear and simple example on how to add a registry on a particular location on a Windows 98 machine? Thanks.



____________________________
Life is but a merry go round
around and around :-)

Visit us today:
http://www.welford-costelloe.com
25-04-2003 at 01:31 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 1052
icon Re: Regarding Registry and Visual Basic

Wow! Cool tip dude!
I never knew about that function of vb.. and i've been dropping in code/classes from other people for years.
nice one!
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

24-09-2005 at 02:53 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Frequently Asked Questions : Regarding Registry and Visual Basic
Previous Topic (what form on the screen)Next Topic (if...if...else/if elseif.../if...ifnot) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder