Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Registry Values 2

Status
Not open for further replies.

sacsac

Programmer
Dec 10, 2000
182
GB
I have frequently used the SaveSetting/GetSetting functions for storing and retrieving data, but this appears to be limited to just one specific part of the registry. What code do I need to retrieve data from, for example:

HKEY_LOCAL_MACHINE\Software\MyCompany\MyApp\MyConfig
 
You can do this using the API's
Code:
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 RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long

You can also access them using Windows Scripting Host
Code:
Dim WshShell As Object

Dim strRootKey As String
Dim strVariable As String

Set WshShell = CreateObject("WScript.Shell")
strRootKey = "HKCU\Control Panel\Desktop\ForeGroundLockTimeOut"
On Error Resume Next
Err.Clear
strVariable = WshShell.RegRead(strRootKey)

If Err.Number <> 0 Then MsgBox strVariable

Set WshShell = Nothing
Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Sorry, typo in the last example, it should read:
Code:
If Err.Number [red]=[/red] 0 Then MsgBox strVariable

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thanks HarleyQuinn. The Windows Scripting Host code works just fine and easily gives me just what I need. Out of interest though, I can't work out how to use the API alternative. Exactly what code would I write to retrieve the value for a specfic key?
 
If you create a project using the wizard and include an about box, VS will generate the code that uses these API functions to retrieve information from the registry.

It will also create all necessary Const's etc you can use.

If you have a look at that (the function it creates is called GetKeyValue())it will detail how these functions are used.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
So following on from the Windows Scripting Host method of retrieving a Registry Key with the sample above, is there an equivalent way of writing a key? i.e. the 'reverse' of .RegRead
 
>'reverse' of .RegRead

Of course, there is a RegWrite method; and there is also a RegDelete method for deleting registry keys. You should add "Windows Script Host Object Model" to project references and examine these methods in Object Browser to see their syntax.

If you want to explore API functions, do a keyword search in this forum on "RegCloseKey". This function is used with all registry related APIs and returns useful results.

As for the "About Box", I remember I also used it to learn registry access functions for the very first times ... long time back.
 
I have now got my code working successfully using RegRead, RegDelete and RegWrite - and all works fine in programming environment. However, when compiled into .exe file only the RegRead succeeds because the other two methods appear (quite understandably) to require admin privileges. Is there any way round this in code, or is the only way to run the app with Admin privileges?
 
You should be able to change the specific Registry key's permissions to allow it to be edited by non Admin users (generally only advisible if you're using your own custom keys). You can do this by right clicking the key's folder in regedit and selecting "Permissions..."

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thanks HarleyQuinn. I know that I can alter the various Registry permissions on my own machine by judicious editing of the Registry, but I was trying to discover whether I could do it in code so that my end user's application (on their PC) could manipulate the registry entries without having to be run in Admin status.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top