Important alert: (current site time 7/16/2013 1:47:26 AM EDT)
 

VB icon

______A Edit Registry

Email
Submitted on: 8/7/2000 2:12:09 PM
By: Agent Smith 
Level: Intermediate
User Rating: By 22 Users
Compatibility: VB 5.0, VB 6.0
Views: 130488
author picture
(About the author)
 
     How to Read and Write to the Registry using all HKEYS
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :______A Edit Registry
'**************************************
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
#If Win32 Then
 
 Public Const HKEY_CLASSES_ROOT = &H80000000
 Public Const HKEY_CURRENT_USER = &H80000001
 Public Const HKEY_LOCAL_MACHINE = &H80000002
 Public Const HKEY_USERS = &H80000003
 Public Const KEY_ALL_ACCESS = &H3F
 Public Const REG_OPTION_NON_VOLATILE = 0&
 Public Const REG_CREATED_NEW_KEY = &H1
 Public Const REG_OPENED_EXISTING_KEY = &H2
 Public Const ERROR_SUCCESS = 0&
 Public Const REG_SZ = (1)
#End If
Type SECURITY_ATTRIBUTES
 
 nLength As Long
 lpSecurityDescriptor As Long
 bInheritHandle As Boolean
End Type
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: ______A Edit Registry
' Description:How to Read and Write to the Registry using all HKEYS
' By: Agent Smith
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=10479&lngWId=1'for details.'**************************************

Public Function bSetRegValue(ByVal hKey As Long, ByVal lpszSubKey As String, ByVal sSetValue As String, ByVal sValue As String) As Boolean
 
 On Error Resume Next
 Dim phkResult As Long
 Dim lResult As Long
 Dim SA As SECURITY_ATTRIBUTES
 Dim lCreate As Long
 RegCreateKeyEx hKey, lpszSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, SA, phkResult, lCreate
 lResult = RegSetValueEx(phkResult, sSetValue, 0, REG_SZ, sValue, CLng(Len(sValue) + 1))
 RegCloseKey phkResult
 bSetRegValue = (lResult = ERROR_SUCCESS)
 
End Function
Public Function bGetRegValue(ByVal hKey As Long, ByVal sKey As String, ByVal sSubKey As String) As String
 
 Dim lResult As Long
 Dim phkResult As Long
 Dim dWReserved As Long
 Dim szBuffer As String
 Dim lBuffSize As Long
 Dim szBuffer2 As String
 Dim lBuffSize2 As Long
 Dim lIndex As Long
 Dim lType As Long
 Dim sCompKey As String
 
 lIndex = 0
 lResult = RegOpenKeyEx(hKey, sKey, 0, 1, phkResult)
 Do While lResult = ERROR_SUCCESS And Not (bFound)
szBuffer = Space(255)
lBuffSize = Len(szBuffer)
szBuffer2 = Space(255)
lBuffSize2 = Len(szBuffer2)
lResult = RegEnumValue(phkResult, lIndex, szBuffer, lBuffSize, dWReserved, lType, szBuffer2, lBuffSize2)
If (lResult = ERROR_SUCCESS) Then
 sCompKey = Left(szBuffer, lBuffSize)
 If (sCompKey = sSubKey) Then
bGetRegValue = Left(szBuffer2, lBuffSize2 - 1)
 End If
End If
lIndex = lIndex + 1
 
 Loop
 RegCloseKey phkResult
End Function


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/13/2000 10:14:07 PMRob Wall

bFound is undeclared
(If this comment was disrespectful, please report it.)

 
11/19/2000 10:47:34 AMJordan

I suggest you try dimming it as a boolean, long, or integer. I'm not sure if that'll fix it because I'm not able to test my theory at the moment. If I remember when I get home I'll test it and post my results.
(If this comment was disrespectful, please report it.)

 
1/24/2001 6:50:13 PMChris

you should just put an "exit function" right after you set your return value. Then when the loop exit's you can set an error value stating you couldn't find the key they wanted.
(If this comment was disrespectful, please report it.)

 
4/24/2001 9:13:02 PMGary

I tried the code, but can't work out how to get the value from it. Please tell me how I can do this. If you know, e-mail me, gary@earsweb.fsnet.co.uk
Thanks
(If this comment was disrespectful, please report it.)

 
4/26/2001 12:59:42 PMRob Loach

This code is GREAT!

Cheers for the great work.
(If this comment was disrespectful, please report it.)

 
4/26/2001 1:01:54 PMRob Loach

But, how would you include an if error section?

I've been triing one out but not having any success.
(If this comment was disrespectful, please report it.)

 
6/14/2001 12:21:35 PMHao Peng

If you want to access more than three level, e.g. if you want to get the value of string
(If this comment was disrespectful, please report it.)

 
10/18/2001 9:59:55 AMjohn bond

i want to find this registry setting..


(If this comment was disrespectful, please report it.)

 
10/18/2001 10:01:18 AMjohn bond

dont know if your function will work.. i'm trying to get the value stored in this registry location..

HKEY_LOCAL_MACHINE\software\classes\clarify.document\shell\open\comman d\default

if
it can please shoot me the syntax of how to call this using your registry retrieve function.

tks john

(If this comment was disrespectful, please report it.)

 
10/19/2001 9:47:21 AMAustin

Three levels deep...hmmm...kinda renders this useless if you want to retrieve something deeper, doesn't it?? However if all you WANT is three levels, then the code is very well written.
(If this comment was disrespectful, please report it.)

 
11/19/2001 5:42:41 AMJohn

I tried the bGetRegValue function but it doesn't work(VB6.exe crashes). If you could help me i would be apreciated.
(If this comment was disrespectful, please report it.)

 
1/26/2002 4:37:35 PMDJ

HI, i just wanted to know how to, add a Value to the Run in wndows98
(If this comment was disrespectful, please report it.)

 
5/23/2002 6:17:07 PMo0o_Spectator_o0o

Dudez, Da Code is off da chain yO !
(If this comment was disrespectful, please report it.)

 
7/20/2002 8:23:23 AMMindsofts-Company

The picrure is yours?
(If this comment was disrespectful, please report it.)

 
8/23/2002 4:37:48 PMwutaag

Mindsofts-Company i take it u didn't see the Matrix ? ;)
(If this comment was disrespectful, please report it.)

 
10/24/2002 3:19:02 AM

What do you mean three levels deep? You use the HKEY_* constants for the hKey parameter (or other hKeys returned from previous calls to RegOpenKey/Ex), and the rest of the path (eg: 'Software\Microsoft\VBA\6.0\Common') as the lpszSubKey parameter, and the actual name of the key to set as sSetValue (eg: 'FolderView').
(If this comment was disrespectful, please report it.)

 
1/21/2003 11:52:42 AMSéan Connolly

Very good, but wouldn't have including the API declarations been a good idea ?.
(If this comment was disrespectful, please report it.)

 
1/21/2003 11:54:24 AMSéan Connolly

oops, you have on the copy friendly version. Sorry !.
(If this comment was disrespectful, please report it.)

 
2/22/2003 1:34:48 PM

hey, I don't know if it works, but can someone give examples of how to call these?

thanks
(If this comment was disrespectful, please report it.)

 
7/28/2003 1:26:12 PM

How does it works? How's the syntax like? Could you give any example?
(If this comment was disrespectful, please report it.)

 
9/11/2003 10:55:24 AM

ummm... for the newbies like me... that is like hell on earth :\
(If this comment was disrespectful, please report it.)

 
2/24/2004 1:57:46 AM

Very usefull code for programmer
thanks
(If this comment was disrespectful, please report it.)

 
3/13/2004 5:56:27 PM

well i am trying to save to hklmsoftwarmicrosoftInternet explorermain and want to make a string and then write to it and well... It looks to conpuzzling to me so if ne one can help me with saving to reg im me on yahoo : akravemas
(If this comment was disrespectful, please report it.)

 
3/13/2004 5:57:49 PM

sry i couldnt fit yahoo: akravemaster
aim: akravemaster1
Preferably yahoo
thanks
(If this comment was disrespectful, please report it.)

 
6/2/2004 8:16:32 AM

can you tell me how to add string value in registry using visual basic 6. i want to put my exe program in the registry in
(If this comment was disrespectful, please report it.)

 
6/2/2004 8:18:49 AM

hkey_local_machine/software/microsoft/windows/currentversion/run.

my email is codexman_sys@yahoo.com
(If this comment was disrespectful, please report it.)

 
1/25/2005 4:44:15 PM

i will check it soon and then i think i will be able to edit and insert registry keys through Vb so i can authenticate my application on a Pc.
(If this comment was disrespectful, please report it.)

 
5/18/2005 10:25:35 AMRyand833

This is AWESOME code. Exactly what I was looking for. Easy to use, and works great. 5 globes from me.
(If this comment was disrespectful, please report it.)

 
1/6/2008 12:03:55 AMGUstaf

Good Code :_D
(If this comment was disrespectful, please report it.)

 
8/2/2008 4:20:29 AMrizen

Thanks regards
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.