Solved

Password Protect Through The Registry ?

Posted on 1999-01-11
3
157 Views
Last Modified: 2011-09-20
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
Comment
Question by:dropfork
3 Comments
 
LVL 3

Accepted Solution

by:
HATCHET earned 50 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:jjmartin
Comment Utility
Take a look at GetSetting and SaveSetting in the VB help.  It is a very simple way to write to the registry.
0
 

Author Comment

by:dropfork
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now