PDA

View Full Version : Sleeper: Windows Registry



sheeeng
07-07-2005, 09:29 AM
Hi all,

Can anyone know how to read or write to windows registry using VBA?

Thx.

JKwan
07-07-2005, 09:32 AM
use SaveSetting and GetSetting to get at the Registry

sheeeng
07-07-2005, 09:34 AM
use SaveSetting and GetSetting to get at the Registry

Any code sample?

Hard for me to understand when u say like this...

Sorry.

Thx for reply..

Bob Phillips
07-07-2005, 09:34 AM
Can anyone know how to read or write to windows registry using VBA?

Playing with fire Sheeeng?

You can either use GetSetting/saveSetting, which only accesses a limited part of the registry



sRegKey = GetSetting(appname:="MyApp", section:="Startup", _
key:="Left", Default:="25")
SaveSetting appname:="MyApp", section:="Startup", _
key:="Top", setting:=75


or you can use the windows scripting host which opens it all to you



Dim oWSH As Object
Set oWSH = CreateObject("WScript.Shell")
sRegKey = oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", 1, "REG_BINARY"
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", "Careful!", "REG_SZ"

JKwan
07-07-2005, 09:36 AM
If you lookup SaveSetting in your VBA help, there are examples.

Ken Puls
07-07-2005, 12:38 PM
Hi Sheeeng,

As xld says... be careful with these. (Joost, you can stop laughing now!) ;)

Malcolm has a registry entry in the KB here: Save Settings to the Registry. There may be others in there too.

Ivan F Moala
07-07-2005, 01:46 PM
What is is you want to do?
There are examples in the help file for the settings Jkwan mentions.
Otherwise if you don't know what you are doing back up your regstry.

Ken Puls
07-07-2005, 02:23 PM
Otherwise if you don't know what you are doing back up your regstry.

... and probably even if you do. :*)

Ivan F Moala
07-07-2005, 07:51 PM
... and probably even if you do. :*)

yes :)

sheeeng
07-08-2005, 04:03 AM
Playing with fire Sheeeng?

You can either use GetSetting/saveSetting, which only accesses a limited part of the registry



sRegKey = GetSetting(appname:="MyApp", section:="Startup", _
key:="Left", Default:="25")
SaveSetting appname:="MyApp", section:="Startup", _
key:="Top", setting:=75


or you can use the windows scripting host which opens it all to you



Dim oWSH As Object
Set oWSH = CreateObject("WScript.Shell")
sRegKey = oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", 1, "REG_BINARY"
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", "Careful!", "REG_SZ"



How can we modify existing value?

(with the example above)

ps. Sorry for late reply....I'm really burn out in work these few days....

Justinlabenne
07-08-2005, 11:15 AM
This is from the regedit help: which if you are just modifying it for yourself on your computer, would be better to just do it manually, it doesn't take long,

to get to the registry editor,
Start menu > Run > Type in RegEdit



To change a value



Open Registry Editor (http://EXEC=,regedit.exe,,%20CHM=ntshared.chm%20FILE=alt_url_windows_component.htm ).
Select the entry you want to change.
On the Edit menu, click Modify.
In Value data, type the new data for the value, and then click OK.

Caution



Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on your computer.

Note



To open Registry Editor, click Start, click Run, type regedit, and then click OK.
If you make a mistake that results in your computer not starting properly, you can restore the registry. For instructions, see Related Topics.
To make changes to a registry key, you must have the appropriate permissions. For more information, see Related Topics.



or, if you know the key and values you are modifying, and insist on using vba to modify the registry, this long bit of API can delete keys or values, create or modify values, etc.. it is more overwhelming to look at than the get setting / save setting methods.


'Types Definition
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

'API Declarations
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" 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.dll" 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
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private 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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

'Types Enum Definition
Public Enum T_KeyClasses
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_CONFIG = &H80000005
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
End Enum

'Constants Definition
Private Const SYNCHRONIZE = &H100000
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_EVENT = &H1
Private Const KEY_NOTIFY = &H10
Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const KEY_EXECUTE = (KEY_READ)
Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Private Const REG_BINARY = 3
Private Const REG_CREATED_NEW_KEY = &H1
Private Const REG_DWORD = 4
Private Const REG_DWORD_BIG_ENDIAN = 5
Private Const REG_DWORD_LITTLE_ENDIAN = 4
Private Const REG_EXPAND_SZ = 2
Private Const REG_FULL_RESOURCE_DESCRIPTOR = 9
Private Const REG_LINK = 6
Private Const REG_MULTI_SZ = 7
Private Const REG_NONE = 0
Private Const REG_SZ = 1
Private Const REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
Private Const REG_NOTIFY_CHANGE_LAST_SET = &H4
Private Const REG_NOTIFY_CHANGE_NAME = &H1
Private Const REG_NOTIFY_CHANGE_SECURITY = &H8
Private Const REG_OPTION_BACKUP_RESTORE = 4
Private Const REG_OPTION_CREATE_LINK = 2
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const REG_OPTION_RESERVED = 0
Private Const REG_OPTION_VOLATILE = 1
Private Const REG_LEGAL_CHANGE_FILTER = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
Private Const REG_LEGAL_OPTION = (REG_OPTION_RESERVED Or REG_OPTION_NON_VOLATILE Or REG_OPTION_VOLATILE Or REG_OPTION_CREATE_LINK Or REG_OPTION_BACKUP_RESTORE)

'Delete e Registry Key with all his contained Values

Public Sub DeleteRegistryKey(rClass As T_KeyClasses, Path As String)
Dim res As Long
res = RegDeleteKey(rClass, Path)
End Sub

'Delete a Value from the Registry
Public Sub DeleteValue(rClass As T_KeyClasses, Path As String, sKey As String)
Dim hKey As Long
Dim res As Long
res = RegOpenKeyEx(rClass, Path, 0, KEY_ALL_ACCESS, hKey)
res = RegDeleteValue(hKey, sKey)
RegCloseKey hKey
End Sub

'Creates a New Registry Key
Public Sub CreateRegistryKey(rClass As T_KeyClasses, Path As String)
Dim hKey As Long
Dim res As Long
Dim y As SECURITY_ATTRIBUTES
Dim Operation As Long
res = RegCreateKeyEx(rClass, Path, 0, "", 0, KEY_ALL_ACCESS, y, hKey, Operation)
RegCloseKey hKey
End Sub

'Get a specific Registry Value (to access the Default Registry Key Value set sKey parameter as "")
Public Function GetRegValue(KeyRoot As T_KeyClasses, Path As String, sKey As String) As String
Dim hKey As Long
Dim KeyValType As Long
Dim KeyValSize As Long
Dim KeyVal As String
Dim tmpVal As String
Dim res As Long
Dim i As Integer
res = RegOpenKeyEx(KeyRoot, Path, 0, KEY_ALL_ACCESS, hKey)
If res <> 0 Then GoTo Errore
tmpVal = String(1024, 0)
KeyValSize = 1024
res = RegQueryValueEx(hKey, sKey, 0, KeyValType, tmpVal, KeyValSize)
If res <> 0 Then GoTo Errore
If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then
tmpVal = Left(tmpVal, KeyValSize - 1)
Else
tmpVal = Left(tmpVal, KeyValSize)
End If
Select Case KeyValType
Case REG_SZ
KeyVal = tmpVal
Case REG_DWORD
For i = Len(tmpVal) To 1 Step -1
KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1)))
Next
KeyVal = Format("&h" + KeyVal)
End Select
GetRegValue = KeyVal
RegCloseKey hKey
Exit Function
Errore:
GetRegValue = ""
RegCloseKey hKey
End Function

'Create or Modify a Registry Value (to access the Default Registry Key Value set sKey parameter as "")
Public Function SetRegValue(KeyRoot As T_KeyClasses, Path As String, sKey As String, NewValue As String) As Boolean
Dim hKey As Long
Dim KeyValType As Long
Dim KeyValSize As Long
Dim KeyVal As String
Dim tmpVal As String
Dim res As Long
Dim i As Integer
Dim x As Long
res = RegOpenKeyEx(KeyRoot, Path, 0, KEY_ALL_ACCESS, hKey)
If res <> 0 Then GoTo Errore
tmpVal = String(1024, 0)
KeyValSize = 1024
res = RegQueryValueEx(hKey, sKey, 0, KeyValType, tmpVal, KeyValSize)
Select Case res
Case 2
KeyValType = REG_SZ
Case Is <> 0
GoTo Errore
End Select
Select Case KeyValType
Case REG_SZ
tmpVal = NewValue
Case REG_DWORD
x = Val(NewValue)
tmpVal = ""
For i = 0 To 3
tmpVal = tmpVal & Chr(x Mod 256)
x = x \ 256
Next
End Select
KeyValSize = Len(tmpVal)
res = RegSetValueEx(hKey, sKey, 0, KeyValType, tmpVal, KeyValSize)
If res <> 0 Then GoTo Errore
SetRegValue = True
RegCloseKey hKey
Exit Function
Errore:
SetRegValue = False
RegCloseKey hKey
End Function

or again with the save settings, which I don't believe can modify existing, at least not that I know of...
SaveSetting "MyExcelApp", "Excel Settings", "Stored", "Yes"
from the ms Excel help on SaveSettings.
Saves or creates an application entry in the application's entry in the Windows registry (http://javascript%3Cb%3E%3C/b%3E:hhobj_4.Click%28%29) or (on the Macintosh) information in the application?s initialization file.

SaveSetting appname, section, key, setting


If you want to modify an existing entry, do it manually, or backup your registry and try the API, I am not sure about modifiy existing registry values without using it. And use extreme caution, one mistake can mess up your system. Or use the old school method of creating a .ini file.

Bob Phillips
07-08-2005, 12:59 PM
or, if you know the key and values you are modifying, and insist on using vba to modify the registry, this long bit of API can delete keys or values, create or modify values, etc.. it is more overwhelming to look at than the get setting / save setting methods.

Uh, why?



Dim oWSH As Object
Set oWSH = CreateObject("WScript.Shell")
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", "Careful!", "REG_SZ"
MsgBox oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", "Were you careful?", "REG_SZ"
MsgBox oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")
oWSH.Regdelete "HKCU\Software\myKey\mySubkey\myKeyValue"
MsgBox oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")

MOS MASTER
07-08-2005, 01:02 PM
Great to see you still remember Ken!...That'll teach yah! :boxer2:

Justin please use the line continuation character ( _ ) to break of your functions and API Calls...My neck hurts from reading your post. :yes

Justinlabenne
07-08-2005, 01:48 PM
Sorry Joost, It came from my Vb.Net archive, I very rarely use it, so never bothered to format the sucker. It was laying around collecting dust, didn't think about it.

Oh, and I have only once ever done anythign involving registry manipulation, beside basic save/get setting, so what you posted xld, I really had no idea, never studied up on much of anything registry wise..never had the interest too, so my method was a bit long winded, and neck cranking from the sounds of it, :devil:

mdmackillop
07-08-2005, 01:50 PM
I'm going to get a second monitor just for these wide posts!

Ivan F Moala
07-09-2005, 05:06 AM
Uh, why?



Dim oWSH As Object
Set oWSH = CreateObject("WScript.Shell")
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", "Careful!", "REG_SZ"
MsgBox oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")
oWSH.RegWrite "HKCU\Software\myKey\mySubkey\myKeyValue", "Were you careful?", "REG_SZ"
MsgBox oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")
oWSH.Regdelete "HKCU\Software\myKey\mySubkey\myKeyValue"
MsgBox oWSH.RegRead("HKCU\Software\myKey\mySubkey\myKeyValue")



because API's or more specifically the underlying Dlls are always available in Win32 OS ..... with Wsh I have found instances where it won't operate or has been disabled by MIS or setup configurations.

JonPeltier
07-09-2005, 07:19 PM
or again with the save settings, which I don't believe can modify existing, at least not that I know of...


Sure it can. If you specify a value for an existing key, the new value overwrites the old.

Justinlabenne
07-09-2005, 07:43 PM
Thanks for the info Jon. I just wasn't sure :dunno

Personally, I have never bothered to study up on manipulating the registry beyond the Bullen/Bovey/Green Professional Excel Development methods of storing / re-storing a users Excel settings by saving them in the registry.

My question is, what is the purpose modifying an "Existing" entries value using VBA? I have seen ways to "tweak" the environment like in this link (http://www.kellys-korner-xp.com/xp_reg_edits.htm), but I am just curious what reason there may be to do so from Excel. Not a big deal, just curious..:think:

JonPeltier
07-09-2005, 08:54 PM
My question is, what is the purpose modifying an "Existing" entries value using VBA? I have seen ways to "tweak" the environment like in this link (http://www.kellys-korner-xp.com/xp_reg_edits.htm), but I am just curious what reason there may be to do so from Excel. Not a big deal, just curious..:think:
It's not just modifying the environment. Suppose you wanted to keep a running tally of times a workbook was opened, or recall the last time it was used, or store the most recent invoice number (so you know what the next number will be). You just keep modifying a value every time.

Justinlabenne
07-09-2005, 09:32 PM
Interesting. Thank you Jon. I always avoided using it beyond reading, based on a lot of negative things I had read, or had seen posted like this:


Remember, you can destroy part or all of the running configuration with an incorrect change.

Just remember: If you hack at will in the Registry, you can permanently corrupt your system.

from this link (http://www.windowsitpro.com/Article/ArticleID/2268/2268.html?Ad=1)

or even what xld mentioned:

Playing with fire Sheeeng?

Have to practice a bit, probably won't "hack at will" though.

sheeeng
07-10-2005, 05:34 AM
Playing with fire Sheeeng?


Nop. I was trying to expand my knowledge on connecting VBA with Registry..

Very good feedback here....:thumb
Keep it up...:thumb

I haven't had time to experiment it....:doh:
Sorry...

JonPeltier
07-10-2005, 06:16 AM
If you use GetSetting/SaveSetting, the worst you'll do is mess up other programs that use VB and VBA. These methods only use a small specific region of the registry.

Using the API (and I think WSH) opens up the entire registry, so you have to be more careful.