I need to read and write from the registry as well as deleting entrys. Anyone got a simple example on how to do it.
I also need to know which encryption method I should use to store passwords in the registry?
Printable View
I need to read and write from the registry as well as deleting entrys. Anyone got a simple example on how to do it.
I also need to know which encryption method I should use to store passwords in the registry?
Mentalis has a good example.
Doesn't show how to create sub-directorys in the registry. Do I just add the / for the path?
like
vb Code:
RegCreateKeyEx HKEY_CURRENT_USER, "Sim-Chat", 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret RegCreateKeyEx HKEY_CURRENT_USER, "Sim-Chat/Users", 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret
guess i'll give it a try. bbl. =)
Use this..."\"...not "/".
Other thing..I recommend in order to write REG_DWORD values to use this code style example:
To use something like this:vb Code:
Public Function WriteRegDWORD(ByVal h_Key As Long, ByVal lpcszPath As String, ByVal lpcszField As String, ByVal dwData As Long) As Boolean Dim hKey As Long Dim bRet As Boolean If RegCreateKeyEx(h_Key, lpcszPath, 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, hKey, 0) <> ERROR_SUCCESS Then WriteRegDWORD = False Else If RegSetValueEx(hKey, lpcszField, 0, REG_DWORD, dwData, Len(dwData)) = ERROR_SUCCESS Then bRet = True Else bRet = False End If RegCloseKey hKey End If WriteRegDWORD = bRet End Function
Remember: this is an example..change it according to your needs.vb Code:
WriteRegDWORD HKEY_LOCAL_MACHINE, "Software\Joel", "dwValueToWrite", 69
Thanks Kal. Great example.
I keep getting a type mismatch and can't figure out why. I copyed your function line for line =). any ideas why? I also have all constants and api functions correct.
vb Code:
If chkSavePassword.Value = vbChecked Then If WriteRegDWORD(HKEY_LOCAL_MACHINE, "Software\Sim-Chat\Users\" & MyUserName & "\Login", "Password", txtPassword.Text) = False Then MsgBox "error" End If Else If WriteRegDWORD(HKEY_LOCAL_MACHINE, "Software\Sim-Chat\Users\" & MyUserName & "\Login", "Password", "") = False Then MsgBox "error" End If End If
got it fixed ;]
We'll I thought I had it. Now I cant insert value's into the Keys. =(
nevermind. got that fixed now too. kek
Now I can't read my values.
my lValueType is a DWORD. returns a #.Code: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
Dim strData As Integer
'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
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
ElseIf lValueType = REG_DWORD Then
'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
When reading values you must use RegOpenKeyEx.
I must be missing something here. keeps returning 0 when i useCode:Function ReadRegDWORD(path As String) As Long
RegOpenKeyEx HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, ReadRegDWORD
End Function
ReadRegDWORD ("Software\Sim-Chat\Users\" & cmbUserName.Text & "\Login\Password") Where Password is the DWORD and not a Key
if I use
ReadRegDWORD ("Software\Sim-Chat\Users\" & cmbUserName.Text & "\Login")
it returns a #
No, sir....RegOpenKeyEx is only step one of three:
RegOpenKeyEx
RegQueryValueEx
RegCloseKey
See an example.
Also try google sometime :)
1 last question. swear =)
WHen it inserts the data into the registry. it encrypts it and when I receive it, it doesnt decrypt it. How am I suppose to decrypt it?
Ok..
Windows encrypts registry values for security reasons..but using the advapi functions like RegQueryValueEx, you shouldn't botther decrypt them your self.
If you want to encrypt entries you'll have to write your own algorithm for encrypt and decrypt.
no. what i'm saying is when I insert a value into the registry, it encrypts it. but when I use RegQueryValueEx. it returns the encrypted entry, it doesn't decrypt it.
Wait! That's no true! :eek:Quote:
Originally Posted by psychotomus
You must getting wrong values or something went wrong...
Wow you guys are doing it the hard way. All you need to use is this code. Put this in the declarations section
To write to the registryCode:Dim SScript
Set SScript = CreateObject("WScript.Shell")
To read:Code:SScript.RegWrite "KEY", "DATA"
Code:label1.caption=SScript.RegRead("KEY")
(I do not take credit for this code. Jazz00006 posted this on another site)
Quote:
Originally Posted by Mxjerrett
where at does it write that data at in the registry?
SScript.RegWrite "KEY", "DATA" writes it to the registry. Replace KEY with the location and DATA with the data you want to supply.
where at in the registry i am asking?
Where ever you specify. You replace KEY with where you want it and wallah, your data is displayed in the registry, where ever you told it to.