Hi all, I have tried a few times to get the windows regapi working but I cant.
I need to be able to make, save and delete Keys and Values.
Please help me with this...
MvH Mattias Holmström(Sweden)
Cimperiali
August 27th, 2001, 08:01 AM
'form api-guide, a free tool you can download
'following this
'URL: http://www.allapi.net/
'This program needs 3 buttons
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CURRENT_USER = &H80000001
private Declare Function RegCloseKey Lib "advapi32.dll" (byval hKey as Long) as Long
private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (byval hKey as Long, byval lpSubKey as string, phkResult 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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (byval hKey as Long, byval lpSubKey as string, phkResult 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, lpType as Long, lpData as Any, 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, lpData as Any, byval cbData as Long) as Long
Function RegQueryStringValue(byval hKey as Long, byval strValueName as string) as string
Dim lResult as Long, lValueType as Long, strBuf as string, lDataBufSize as Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, byval 0, lDataBufSize)
If lResult = 0 then
If lValueType = REG_SZ then
'Create a buffer
strBuf = string(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, byval strBuf, lDataBufSize)
If lResult = 0 then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY then
Dim strData as Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey as Long, strPath as string, strValue as string)
Dim Ret
'Open the key
RegOpenKey hKey, strPath, Ret
'get the key's content
GetString = RegQueryStringValue(Ret, strValue)
'Close the key
RegCloseKey Ret
End Function
Sub SaveString(hKey as Long, strPath as string, strValue as string, strData as string)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Save a string to the key
RegSetValueEx Ret, strValue, 0, REG_SZ, byval strData, len(strData)
'close the key
RegCloseKey Ret
End Sub
Sub SaveStringLong(hKey as Long, strPath as string, strValue as string, strData as string)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'set the key's value
RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey Ret
End Sub
Sub DelSetting(hKey as Long, strPath as string, strValue as string)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Delete the key's value
RegDeleteValue Ret, strValue
'close the key
RegCloseKey Ret
End Sub
private Sub Command1_Click()
Dim strString as string
'Ask for a value
strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 then
MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
Exit Sub
End If
'Save the value to the registry
SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
End Sub
private Sub Command2_Click()
'get a string from the registry
Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
If Ret = "" then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
End Sub
private Sub Command3_Click()
'Delete the setting from the registry
DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
End Sub
private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Command1.Caption = "set Value"
Command2.Caption = "get Value"
Command3.Caption = "Delete Value"
End Sub
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
Iouri
August 27th, 2001, 08:27 AM
Sub RegCreate(RegKey As String, RegValue As String)
' Create the RegEdit Object
Set RegEdit = CreateObject("WScript.Shell")
' Set The Value
RegEdit.RegWrite RegKey, RegValue
End Sub
Private Sub cmdRegCreate_Click()
' Create The Key
'to run with window start
'RegCreate "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\RegRun", "" & CurDir & "\" & App.EXEName & ".exe"
End Sub
Sub RegDelete(RegKey As String)
' Create the RegEdit Object
Set RegEdit = CreateObject("WScript.Shell")
' Delete Key or Folder
RegEdit.RegDelete RegKey
End Sub
Private Sub cmdRegDelete_Click()
' Delete the Key RegRun
RegDelete "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\RegRun"
End Sub
Iouri Boutchkine
iouri@hotsheet.com
Moretic
August 27th, 2001, 09:57 AM
The code you 2 posted here where good, I think... I didnt get them to work but then again I usally dont get API calls to work. It complained about some strange thing, if you ppl can make me 3 function wich gets, saves and deletes
in all 4 diffrent types of values and it works for me, then I whould be happy.
dont get me wrong, just the fact that you answered makes me happy, I didnt think anyone where going to answer...
MvH Mattias Holmström(Sweden)
Cimperiali
August 28th, 2001, 06:54 AM
>complained about some strange thing
may you post the "strange thing"?
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
Cimperiali
August 28th, 2001, 07:41 AM
Here: this code is similar to above, but I modified it a little to enable
you write binary or string value. I think you can clean it for your purpose using just the string values...
Copy this to WordPad, then copy from wordpad to vb
option Explicit
'form api-guide, a free tool you can download
'following this
'URL: http://www.allapi.net/
'This program needs 3 buttons
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CURRENT_USER = &H80000001
private Declare Function RegCloseKey Lib "advapi32.dll" (byval hKey as Long) as Long
private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (byval hKey as Long, byval lpSubKey as string, phkResult 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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (byval hKey as Long, byval lpSubKey as string, phkResult 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, lpType as Long, lpData as Any, 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, lpData as Any, byval cbData as Long) as Long
Dim theValue as string
Function RegQueryStringValue(byval hKey as Long, byval strValueName as string) as string
Dim lResult as Long, lValueType as Long, strBuf as string, lDataBufSize as Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, byval 0, lDataBufSize)
If lResult = 0 then
If lValueType = REG_SZ then
'Create a buffer
strBuf = string(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, byval strBuf, lDataBufSize)
If lResult = 0 then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY then
Dim strData as Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey as Long, strPath as string, strValue as string)
Dim ret
'Open the key
RegOpenKey hKey, strPath, ret
'get the key's content
GetString = RegQueryStringValue(ret, strValue)
'Close the key
RegCloseKey ret
End Function
Sub SaveString(hKey as Long, strPath as string, strValue as string, strData as string)
Dim ret
'Create a new key
RegCreateKey hKey, strPath, ret
'Save a string to the key
RegSetValueEx ret, strValue, 0, REG_SZ, byval strData, len(strData)
'close the key
RegCloseKey ret
End Sub
Sub SaveStringLong(hKey as Long, strPath as string, strValue as string, strData as string)
Dim ret
'Create a new key
RegCreateKey hKey, strPath, ret
'set the key's value
RegSetValueEx ret, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey ret
End Sub
Sub DelSetting(hKey as Long, strPath as string, strValue as string)
Dim ret
'Create a new key
RegCreateKey hKey, strPath, ret
'Delete the key's value
RegDeleteValue ret, strValue
'close the key
RegCloseKey ret
End Sub
private Sub Command1_Click()
Dim strString as string
'Ask for a value
strString = InputBox("Please enter a string or a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
If Not IsNumeric(strString) Or strString = "" Or Val(strString) > 255 Or Val(strString) < 0 then
'save the value as string
SaveString HKEY_CURRENT_USER, "KPD-Team", "StringValue", strString & Chr(0)
theValue = "StringValue"
else
'Save the value to the registry as binary
SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
theValue = "BinaryValue"
End If
End Sub
private Sub Command2_Click()
Dim ret
'get a string from the registry
ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
If ret <> "" then
MsgBox "The value is " + ret, vbOKOnly + vbInformation, App.Title
else
'try the string value
ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "StringValue")
If ret <> "" then
MsgBox "The value is " & ret, vbOKOnly + vbInformation, App.Title
else
MsgBox "No value found!"
End If
End If
End Sub
private Sub Command3_Click()
'Delete the setting from the registry
DelSetting HKEY_CURRENT_USER, "KPD-Team", theValue '"BinaryValue"
MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
End Sub
private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Command1.Caption = "set Value"
Command2.Caption = "get Value"
Command3.Caption = "Delete Value"
End Sub
'Some informations about the constants you may use:
'REG_SZ
'Const REG_SZ = 1
' A null-terminated string. It will be a Unicode or ANSI string depending on
'whether you use the Unicode or ANSI functions.
'REG_BINARY= 3
'binary data in any form.
'REG_DWORD
'Const REG_DWORD = 4
' A 32-bit number.
'REG_DWORD_LITTLE_ENDIAN
' A 32-bit number in little-endian format (same as REG_DWORD). In little-endian
'format, the most significant byte of a word is the high-order byte. This is
'the most common format for computers running Windows NT and Windows 95.
'REG_DWORD_BIG_ENDIAN
' A 32-bit number in big-endian format. In big-endian format, the most
'significant byte of a word is the low-order byte.
'REG_EXPAND_SZ
' A null-terminated string that contains unexpanded references to environment
'variables (for example, “%PATH%”). It will be a Unicode or ANSI string
'depending on whether you use the Unicode or ANSI functions.
'REG_LINK
' A Unicode symbolic link.
'REG_MULTI_SZ
' An array of null-terminated strings, terminated by two null characters.
'REG_NONE
' No defined value type.
'REG_RESOURCE_LIST
' A device-driver resource list.
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
Slapstiq
August 28th, 2001, 02:47 PM
Hello,
I usually don't bother with the Windows API if I only have to delete, create or modify registry keys directly. I usually use the windows script host which comes with internet explorer. The ocx is very useful for things like that and very easy to use. However, the api does come in handy when you want to enumerate reg keys to search for particular ones. Also if you don't have windows explorer 5.x Im not sure if the ocx would be present. Just thought I would inform you.
Moretic
August 30th, 2001, 02:49 AM
I have ie 5.something
Slapstiq
September 4th, 2001, 07:20 AM
Then you should be able to reference the wshom.ocx in you project. Then create an object like this
Dim objShell As IWshRuntimeLibrary.IWshShell_Class
Set objShell = New IWshRuntimeLibrary.IWshShell_Class
then use the objShell objects
regRead(Path)
regWrite(path, Value, TYPE)
regDelete(Path)
sorry it took me a long time I was on vacation
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.