Solved

Password Protect Through The Registry ?

Posted on 1999-01-11
3
159 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
ID: 1455608
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
ID: 1455609
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
ID: 1455610
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 119
Visual Studio 2005 text editor 10 38
vbModal 12 51
Advice in Xamarin 21 78
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

775 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