Solved

Password Protect Through The Registry ?

Posted on 1999-01-11
3
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 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