Solved

changing registry settings using vb

Posted on 2003-11-13
7
182 Views
Last Modified: 2010-05-18
i am working on some desktop applications and i require to enter the registry(regedit) for some settings..... can i do this with the help of vb?if yes then please give example commands!
0
Comment
Question by:devdasvirus
7 Comments
 
LVL 7

Accepted Solution

by:
_agj_ earned 60 total points
Comment Utility
To open keys, edit em etc u need to use the api calls like RegOpenKey etc.

http://www.mentalis.org/apilist/RegOpenKey.shtml

check out the api listing for the otehr registry stuff in it. ALso there shud be a gud example there.

Wihtout usign the api , u can use inbuilt vb fns like:

GetSetting
DeleteSetting
SaveSetting

these however, only allow u to use keys under the VB applications section.

use api for full acess.
0
 
LVL 1

Expert Comment

by:atp_expert
Comment Utility
Use
RegOpenKey to open the key
RegSetValueEx to write value to registry
RegqueryValuEx to read the registry
regclosekey to close the regsitry handle.
0
 
LVL 4

Expert Comment

by:wulfshayde
Comment Utility
Do you need to edit existing registry keys? Or is your purpose to make and maintain program based ones?
0
 
LVL 1

Expert Comment

by:RocketMan801
Comment Utility
if you need to read or writte in specific areas in the windows registry you need to use api calls, for example, if you want to get the path where warcraft3 is installed, knowing that u have to read the value:
InstallPath
stored in:
HKEY_CURRENT_USER\Software\Blizzard Entertainment\Warcraft III

you can use this func:

'///// API declaration ans stuff
Public Const HKEY_LOCAL_MACHINE As Long = &H80000002
Public Const HKEY_CURRENT_USER = &H80000001

'Return codes from Registration functions
Public Const ERROR_SUCCESS As Long = 0
Public Const ERROR_BADDB As Long = 1009
Public Const ERROR_BADKEY As Long = 1010
Public Const ERROR_CANTOPEN As Long = 1011
Public Const ERROR_CANTREAD As Long = 1012
Public Const ERROR_CANTWRITE As Long = 1013
Public Const ERROR_OUTOFMEMORY As Long = 14
Public Const ERROR_INVALID_PARAMETER As Long = 87
Public Const ERROR_ACCESS_DENIED As Long = 5
Public Const ERROR_MORE_DATA As Long = 234
Public Const ERROR_NO_MORE_ITEMS As Long = 259
Public Const KEY_ALL_ACCESS As Long = &H3F
Public Const REG_OPTION_NON_VOLATILE As Long = 0

Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal sSubKey As String, hKey As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal sKeyValue As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, nSizeData As Long) As Long

'//// the function ifself
Public Function Getw3Path() As String
 
   Dim hKey As Long
   Dim nSizeData As Long
   Dim Data As String
   
   Const sSubKey = "Software\Blizzard Entertainment\Warcraft III"
   Const sKeyValue = "InstallPath"

   If RegOpenKey(HKEY_CURRENT_USER, sSubKey, hKey) = ERROR_SUCCESS Then
   
      Data = Space(260)
      nSizeData = Len(Data)
     
      If RegQueryValueEx(hKey, sKeyValue, 0&, 0&, ByVal Data, nSizeData) = ERROR_SUCCESS Then
         Getw3Path = TrimNull(Data)
      End If
     
      Call RegCloseKey(hKey)
   
   End If

End Function

'/required function
Private Function TrimNull(startstr As String) As String

  Dim pos As Integer

  pos = InStr(startstr, Chr$(0))
  If pos Then
      TrimNull = Left$(startstr, pos - 1)
      Exit Function
  End If
  TrimNull = startstr
 
End Function
'// i recomend u http://www.mvps.org/vbnet to get more info and example projects

BUT,  if what u need is simpler than that, and you just want to store your app settings on the reg, then the easiest way to do so is:
'//save value
SaveSetting "App1", "config", "Value1", Text1.Text 'or what you want

'//get the value
Text1.Text = GetSetting("App1", "config", "Value1")
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Moderator, my recommended disposition is:

    Accept _agj_'s comment(s) as an answer.

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

728 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

10 Experts available now in Live!

Get 1:1 Help Now