Registry Key Manipulation

  • Thread starter Thread starter Newbie Coder
  • Start date Start date
N

Newbie Coder

Hello Newsgroup Readers

I would like to know how to go & do the following:

I have a certain registry key that has sub values

Example:

Key1 http://www.microsoft.com
Key2 http://www.sarc.com
Key3 http://www.someurl.com

Say, I deleted Key1 I need to rename Key2 to Key1 & Key3 to Key2 with the
same values they have

There could say 40 or more in the list. So, if there are 40 & I delete Key38
then I will need to rename Key 39 to Key38 & Key40 to Key 39...

I could delete 1 or more of those Key values. So, how do I fill in the
spaces of the Keys deleted?

Please - no Google links etc, but some real code

I am using VB.NET 2003

Any help would be wonderful!

TIA

Newbie Coder
 
Use the function below in Visual Basic 6.0, put it in a module.

If you do not have VB 6.0 then I recomend that you upgrade VB.NET 2003
to Visual Basic 6.0 (classic).

Hope this helps
The Grand Master


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 HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&


Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As
Long


Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA"
(ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As
Long


Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA"
(ByVal Hkey As Long, ByVal lpSubKey As String) As Long


Declare Function RegDeleteValue Lib "advapi32.dll" Alias
"RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As
Long


Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"
(ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As
Long


Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
"RegQueryValueExA" (ByVal Hkey As Long, ByVal lpValueName As String,
ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As
Long) As Long


Declare Function RegSetValueEx Lib "advapi32.dll" Alias
"RegSetValueExA" (ByVal Hkey As Long, ByVal lpValueName As String,
ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal
cbData As Long) As Long
Public Const REG_SZ = 1 ' Unicode nul terminated String
Public Const REG_DWORD = 4 ' 32-bit number


Public Sub savekey(Hkey As Long, strPath As String)
Dim keyhand&
r = RegCreateKey(Hkey, strPath, keyhand&)
r = RegCloseKey(keyhand&)
End Sub


Public Function getstring(Hkey As Long, strPath As String, strValue As
String)
'EXAMPLE:
'
'text1.text = getstring(HKEY_CURRENT_USE
' R, "Software\VBW\Registry", "String")
'
Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(Hkey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal
0&, lDataBufSize)


If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal
strBuf, lDataBufSize)


If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))


If intZeroPos > 0 Then
getstring = Left$(strBuf, intZeroPos - 1)
Else
getstring = strBuf
End If
End If
End If
End Function


Public Sub savestring(Hkey As Long, strPath As String, strValue As
String, strdata As String)
'EXAMPLE:
'
'Call savestring(HKEY_CURRENT_USER, "Sof
' tware\VBW\Registry", "String", text1.tex
' t)
'
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata,
Len(strdata))
r = RegCloseKey(keyhand)
End Sub


Function getdword(ByVal Hkey As Long, ByVal strPath As String, ByVal
strValueName As String) As Long
'EXAMPLE:
'
'text1.text = getdword(HKEY_CURRENT_USER
' , "Software\VBW\Registry", "Dword")
'
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
' Get length/data type
lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType,
lBuf, lDataBufSize)


If lResult = ERROR_SUCCESS Then


If lValueType = REG_DWORD Then
getdword = lBuf
End If
'Else
'Call errlog("GetDWORD-" & strPath, Fals
' e)
End If
r = RegCloseKey(keyhand)
End Function


Function SaveDword(ByVal Hkey As Long, ByVal strPath As String, ByVal
strValueName As String, ByVal lData As Long)
'EXAMPLE"
'
'Call SaveDword(HKEY_CURRENT_USER, "Soft
' ware\VBW\Registry", "Dword", text1.text)
'
'
Dim lResult As Long
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD,
lData, 4)
'If lResult <> error_success Then
' Call errlog("SetDWORD", False)
r = RegCloseKey(keyhand)
End Function


Public Function DeleteKey(ByVal Hkey As Long, ByVal strKey As String)
'EXAMPLE:
'
'Call DeleteKey(HKEY_CURRENT_USER, "Soft
' ware\VBW")
'
Dim r As Long
r = RegDeleteKey(Hkey, strKey)
End Function


Public Function DeleteValue(ByVal Hkey As Long, ByVal strPath As
String, ByVal strValue As String)
'EXAMPLE:
'
'Call DeleteValue(HKEY_CURRENT_USER, "So
' ftware\VBW\Registry", "Dword")
'
Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)
End Function
 
Cor

I am using the registry class already to get the values, delete them...

What I actually need is VB.NET 2003 code which actually does what I
originally asked for

That link just outputs keys to the console window & just gets the valsues.
So, if I deleted Key2 that page code would just miss it out getting Key1
then Key3, ehich is NOT what I asked about. I need to rename all the keys so
they count from Key1 to whatever the longest is in the list

Not being funny, but so-called MVP's don't write code, but point people to
pointless links. Hence I wrote no Google links

Yes, we are all here to help each other, but pointless links that are
absolutely wrong are of no use to anyone. Herfried is another perfect
example of this
 
Newbie,

That is your opininion, I can assure you that I have written a lot of code
in this newsgroup. Repeating it every time new is not so interesting you
know.

Cor
 
I forgot to tell, you can probably never add so much code to this newsgroup
as beside me Herfried did.

Cor
 
I go under a diffferent name now because I am restarting coding again after
a long break from it, but I have for years written my own code for this
newsgroup & many other programming forums & have for 9 years

If an answer takes 300 lines of code to complete & I have time to do it then
I will

So, you cannot tell me that you have written more code than I

I am sure we have crossed in gotdotnet etc. & I am sure you haven't been
programming as long as I have either

But due to time restrictions (& out of coding for 1.5 years) these day I am
unable to spend 6 hours a day in the forums answering coding/Windows
questions like I did

I've seen Herfried point people to Google, copy someones code & put it on
his site & I have also seen your website that is badly coded because it used
to repeat the frames when you refreshed the pages. So, you & the other MVP
who wrote that site should learn ASP.NET

After spending so much time answering questions & not getting any reward
like an MVP I decided to stop. People like myself deserve it, but never get
the recognition

Newbie Coder
 
I think that the point here is that a value in the registry is really
nothing more than a name/value pair, and it is fancy in the way the value
part is interpreted (REG_SZ, REG_DWORD etc.), and there is no 'inbuilt'
functionality for manipulating a 'collection' of values as a single entity.

In my view the best course of action is to read all the values into an array
of some description, sort the array by the name, delete the elements that
need to be deleted, delete all the existing values from the registry key
then write the content of the array back to the registry at the same time
renaming the values according to the position in the array.
 
Most of us fully appreciate the time and effort that
Cor and Herfried put in here helping people.

I can understand how they could get tired of posting
the same answers to the same questions -- answers that
can be found by searching Google.

I've only been hanging out here a couple of weeks.
I don't know as much as Cor or Herfried, so my ability
to help is not as expansive as theirs, but I've already
posted the same answer to the same question at least 3
times.

Just because someone refers you to Google doesn't mean you
have to be insulting to them. Receiving help here is a
*privilege*, not a *right*.

And if you have so many years of experience, why do you
call yourself a newbie?

Robin S.
 
This was completed a few weeks ago


Master Programmer said:
Use the function below in Visual Basic 6.0, put it in a module.

If you do not have VB 6.0 then I recomend that you upgrade VB.NET 2003
to Visual Basic 6.0 (classic).

Hope this helps
The Grand Master


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 HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&


Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As
Long


Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA"
(ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As
Long


Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA"
(ByVal Hkey As Long, ByVal lpSubKey As String) As Long


Declare Function RegDeleteValue Lib "advapi32.dll" Alias
"RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As
Long


Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"
(ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As
Long


Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
"RegQueryValueExA" (ByVal Hkey As Long, ByVal lpValueName As String,
ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As
Long) As Long


Declare Function RegSetValueEx Lib "advapi32.dll" Alias
"RegSetValueExA" (ByVal Hkey As Long, ByVal lpValueName As String,
ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal
cbData As Long) As Long
Public Const REG_SZ = 1 ' Unicode nul terminated String
Public Const REG_DWORD = 4 ' 32-bit number


Public Sub savekey(Hkey As Long, strPath As String)
Dim keyhand&
r = RegCreateKey(Hkey, strPath, keyhand&)
r = RegCloseKey(keyhand&)
End Sub


Public Function getstring(Hkey As Long, strPath As String, strValue As
String)
'EXAMPLE:
'
'text1.text = getstring(HKEY_CURRENT_USE
' R, "Software\VBW\Registry", "String")
'
Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(Hkey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal
0&, lDataBufSize)


If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal
strBuf, lDataBufSize)


If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))


If intZeroPos > 0 Then
getstring = Left$(strBuf, intZeroPos - 1)
Else
getstring = strBuf
End If
End If
End If
End Function


Public Sub savestring(Hkey As Long, strPath As String, strValue As
String, strdata As String)
'EXAMPLE:
'
'Call savestring(HKEY_CURRENT_USER, "Sof
' tware\VBW\Registry", "String", text1.tex
' t)
'
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata,
Len(strdata))
r = RegCloseKey(keyhand)
End Sub


Function getdword(ByVal Hkey As Long, ByVal strPath As String, ByVal
strValueName As String) As Long
'EXAMPLE:
'
'text1.text = getdword(HKEY_CURRENT_USER
' , "Software\VBW\Registry", "Dword")
'
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
' Get length/data type
lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType,
lBuf, lDataBufSize)


If lResult = ERROR_SUCCESS Then


If lValueType = REG_DWORD Then
getdword = lBuf
End If
'Else
'Call errlog("GetDWORD-" & strPath, Fals
' e)
End If
r = RegCloseKey(keyhand)
End Function


Function SaveDword(ByVal Hkey As Long, ByVal strPath As String, ByVal
strValueName As String, ByVal lData As Long)
'EXAMPLE"
'
'Call SaveDword(HKEY_CURRENT_USER, "Soft
' ware\VBW\Registry", "Dword", text1.text)
'
'
Dim lResult As Long
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD,
lData, 4)
'If lResult <> error_success Then
' Call errlog("SetDWORD", False)
r = RegCloseKey(keyhand)
End Function


Public Function DeleteKey(ByVal Hkey As Long, ByVal strKey As String)
'EXAMPLE:
'
'Call DeleteKey(HKEY_CURRENT_USER, "Soft
' ware\VBW")
'
Dim r As Long
r = RegDeleteKey(Hkey, strKey)
End Function


Public Function DeleteValue(ByVal Hkey As Long, ByVal strPath As
String, ByVal strValue As String)
'EXAMPLE:
'
'Call DeleteValue(HKEY_CURRENT_USER, "So
' ftware\VBW\Registry", "Dword")
'
Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)
End Function
 
Back
Top