home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD7449752000.psc / RegStuff.bas < prev    next >
Encoding:
BASIC Source File  |  2000-07-06  |  9.3 KB  |  186 lines

  1. Attribute VB_Name = "Module1"
  2. Dim getend As Integer
  3. Dim where As Integer
  4. Dim length As Integer
  5.  
  6. '______________________________'
  7. Public Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  8. Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
  9. Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
  10. Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
  11. Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
  12. Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
  13.  
  14. '---------------------------------------------------------------
  15. '- Registry Api Constants...
  16. '---------------------------------------------------------------
  17. ' Reg Data Types...
  18. Const REG_SZ = 1                         ' Unicode nul terminated string
  19. Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
  20. Const REG_DWORD = 4                      ' 32-bit number
  21.  
  22. ' Reg Create Type Values...
  23. Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
  24.  
  25. ' Reg Key Security Options...
  26. Const READ_CONTROL = &H20000
  27. Const KEY_QUERY_VALUE = &H1
  28. Const KEY_SET_VALUE = &H2
  29. Const KEY_CREATE_SUB_KEY = &H4
  30. Const KEY_ENUMERATE_SUB_KEYS = &H8
  31. Const KEY_NOTIFY = &H10
  32. Const KEY_CREATE_LINK = &H20
  33. Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
  34. Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
  35. Const KEY_EXECUTE = KEY_READ
  36. Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
  37.                        KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
  38.                        KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
  39.                      
  40. ' Reg Key ROOT Types...
  41. Public Const HKEY_CLASSES_ROOT = &H80000000
  42. Public Const HKEY_CURRENT_USER = &H80000001
  43. Public Const HKEY_LOCAL_MACHINE = &H80000002
  44. Public Const HKEY_USERS = &H80000003
  45. Public Const HKEY_PERFORMANCE_DATA = &H80000004
  46.  
  47. ' Return Value...
  48. Public Const ERROR_NONE = 0
  49. Public Const ERROR_BADKEY = 2
  50. Public Const ERROR_ACCESS_DENIED = 8
  51. Public Const ERROR_SUCCESS = 0
  52.  
  53. '---------------------------------------------------------------
  54. '- Registry Security Attributes TYPE...
  55. '---------------------------------------------------------------
  56. Private Type SECURITY_ATTRIBUTES
  57.     nLength As Long
  58.     lpSecurityDescriptor As Long
  59.     bInheritHandle As Boolean
  60. End Type
  61.  
  62.  
  63.  
  64.  
  65. Public Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubKeyValue As String) As Boolean
  66.     Dim rc As Long                                      ' Return Code
  67.     Dim hKey As Long                                    ' Handle To A Registry Key
  68.     Dim hDepth As Long                                  '
  69.     Dim lpAttr As SECURITY_ATTRIBUTES                   ' Registry Security Type
  70.     
  71.     lpAttr.nLength = 50                                 ' Set Security Attributes To Defaults...
  72.     lpAttr.lpSecurityDescriptor = 0                     ' ...
  73.     lpAttr.bInheritHandle = True                        ' ...
  74.  
  75.     '------------------------------------------------------------
  76.     '- Create/Open Registry Key...
  77.     '------------------------------------------------------------
  78.     rc = RegCreateKeyEx(KeyRoot, KeyName, _
  79.                         0, REG_SZ, _
  80.                         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
  81.                         hKey, hDepth)                   ' Create/Open //KeyRoot//KeyName
  82.     
  83.     If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Errors...
  84.     
  85.     '------------------------------------------------------------
  86.     '- Create/Modify Key Value...
  87.     '------------------------------------------------------------
  88.     If (SubKeyValue = "") Then SubKeyValue = " "        ' A Space Is Needed For RegSetValueEx() To Work...
  89.     
  90.     ' Create/Modify Key Value
  91.     rc = RegSetValueEx(hKey, SubKeyName, _
  92.                        0, REG_SZ, _
  93.                        SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode)))
  94.                        
  95.     If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Error
  96.     '------------------------------------------------------------
  97.     '- Close Registry Key...
  98.     '------------------------------------------------------------
  99.     rc = RegCloseKey(hKey)                              ' Close Key
  100.     
  101.     UpdateKey = True                                    ' Return Success
  102.     Exit Function                                       ' Exit
  103. CreateKeyError:
  104.     UpdateKey = False                                   ' Set Error Return Code
  105.     rc = RegCloseKey(hKey)                              ' Attempt To Close Key
  106. End Function
  107.  
  108. Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
  109.     Dim i As Long                                           ' Loop Counter
  110.     Dim rc As Long                                          ' Return Code
  111.     Dim hKey As Long                                        ' Handle To An Open Registry Key
  112.     Dim hDepth As Long                                      '
  113.     Dim sKeyVal As String
  114.     Dim lKeyValType As Long                                 ' Data Type Of A Registry Key
  115.     Dim tmpVal As String                                    ' Tempory Storage For A Registry Key Value
  116.     Dim KeyValSize As Long                                  ' Size Of Registry Key Variable
  117.     
  118.     ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
  119.     '------------------------------------------------------------
  120.     rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open Registry Key
  121.     
  122.     If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Error...
  123.     
  124.     tmpVal = String$(1024, 0)                             ' Allocate Variable Space
  125.     KeyValSize = 1024                                       ' Mark Variable Size
  126.     
  127.     '------------------------------------------------------------
  128.     ' Retrieve Registry Key Value...
  129.     '------------------------------------------------------------
  130.     rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
  131.                          lKeyValType, tmpVal, KeyValSize)    ' Get/Create Key Value
  132.                         
  133.     If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors
  134.       
  135.     tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)
  136.  
  137.     '------------------------------------------------------------
  138.     ' Determine Key Value Type For Conversion...
  139.     '------------------------------------------------------------
  140.     Select Case lKeyValType                                  ' Search Data Types...
  141.     Case REG_SZ, REG_EXPAND_SZ                              ' String Registry Key Data Type
  142.         sKeyVal = tmpVal                                     ' Copy String Value
  143.     Case REG_DWORD                                          ' Double Word Registry Key Data Type
  144.         For i = Len(tmpVal) To 1 Step -1                    ' Convert Each Bit
  145.             sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value Char. By Char.
  146.         Next
  147.         sKeyVal = Format$("&h" + sKeyVal)                     ' Convert Double Word To String
  148.     End Select
  149.     
  150.     GetKeyValue = sKeyVal                                   ' Return Value
  151.     rc = RegCloseKey(hKey)                                  ' Close Registry Key
  152.     Exit Function                                           ' Exit
  153.     
  154. GetKeyError:    ' Cleanup After An Error Has Occured...
  155.     GetKeyValue = vbNullString                              ' Set Return Val To Empty String
  156.     rc = RegCloseKey(hKey)                                  ' Close Registry Key
  157. End Function
  158.  
  159. Public Function WinDir(Optional ByVal AddSlash As Boolean = False) As String
  160.     Dim t As String * 255
  161.     Dim i As Long
  162.     i = GetWindowsDirectory(t, Len(t))
  163.     WinDir = Left(t, i)
  164.     If (AddSlash = True) And (Right(WinDir, 1) <> "\") Then
  165.         WinDir = WinDir & "\"
  166.     ElseIf (AddSlash = False) And (Right(WinDir, 1) = "\") Then
  167.         WinDir = Left(WinDir, Len(WinDir) - 1)
  168.     End If
  169. End Function
  170.  
  171. Public Function LastSlash(strData As String)
  172.     where = 4
  173.     Do While where <> 0
  174.         length = Len(strData)
  175.         where = InStr(strData, "\")
  176.         strData = Right$(strData, length - where)
  177.         getend = getend + where 'start at location of \ + 1 next loop so it wont get stuck at first \ and loop forever
  178.         DoEvents 'algorithm for finding the location of the last "\" in a string
  179.         'if you know where the last "\" is, then anything to to right is the filename
  180.         'and anything to the left is the directory name
  181.     Loop
  182.  LastSlash = getend
  183.  
  184. End Function
  185.  
  186.