• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Password Protect Through The Registry ?

I heard that you can Password Protect a Program or use a Password Program and store the Password itself in the Registry. If this is true on either above account, Please list some code for me to do this. Be Nice, I am far from being an Expert ! I will need plenty of "this is you and this is visual basic guidance".
Thank You For Inquiring !
0
dropfork
Asked:
dropfork
1 Solution
 
HATCHETCommented:
dropfork,

To Write & Read to & from the registry, place the following in a Module:
________________________________________________________________

Option Explicit

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&
Public Const REG_SZ = 1
Public Const REG_DWORD = 4

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 Sub SaveKey(Hkey As Long, strPath As String)
On Error Resume Next

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

Public Function DeleteKey(ByVal Hkey As Long, ByVal strKey As String)
On Error Resume Next

  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)
On Error Resume Next

  Dim keyhand As Long
  Dim r As Long
 
  r = RegOpenKey(Hkey, strPath, keyhand)
  r = RegDeleteValue(keyhand, strValue)
  r = RegCloseKey(keyhand)

End Function

Public Function GetString(Hkey As Long, strPath As String, strValue As String)
On Error Resume Next

  Dim keyhand As Long
  Dim datatype As Long
  Dim lResult As Long
  Dim strBuf As String
  Dim lDataBufSize As Long
  Dim intZeroPos As Integer
  Dim r As Long
  Dim lValueType As Long
 
  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)
On Error Resume Next

  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

Public Function GetDWORD(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
On Error Resume Next

  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, False)
  End If

  r = RegCloseKey(keyhand)
   
End Function

Public Function SaveDWORD(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
On Error Resume Next

   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

________________________________________________________________

To READ a value from the registry, use the following example for syntax - This example will read what version of Windows 95 you're running from the registry:
________________________________________________________________


Dim CheckUsersName As String
CheckUsersName = GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", "VersionNumber")

________________________________________________________________

To WRITE a value from the registry, use the following example for syntax - This example will write the string "Hello" to an entry of "My Value" under HKEY_CURRENT_USER\Software\!A Test\:
________________________________________________________________


Call SaveString(HKEY_CURRENT_USER, "Software\!A Test", "My Value", "Hello")

________________________________________________________________

It is also possible to write Binary and DWORD values to the registry which can come in handy for things, but that's a little more advanced then you need, or can go now.  If you need more info on this, just post it and I'll be watching and will post a response.

Go to START -> Run and type "RegEdit" and hit <ENTER>.  This will bring up a utility that will let you browse your registry to find the best spots to write information to, and will show you where to get information from in the registry.

HATCHET
0
 
jjmartinCommented:
Take a look at GetSetting and SaveSetting in the VB help.  It is a very simple way to write to the registry.
0
 
dropforkAuthor Commented:
Now I know why they call you the "Hatchet". You sure cut through this little "wooden head". You are da man. Thank You for a well documented example to my question. I wish to thank jjmartin for the comment also. I will take his advice and do some poking around in this area too.
Thank You !
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now