home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / RadioStrea2133471182008.psc / modRegistry.bas < prev    next >
BASIC Source File  |  2008-11-05  |  14KB  |  356 lines

  1. Attribute VB_Name = "modRegistry"
  2. Global WinAmpIsInstalled As Boolean
  3. Global sWinAmpLocation As String
  4. Type FILETIME
  5.     lLowDateTime    As Long
  6.     lHighDateTime   As Long
  7. End Type
  8.  
  9. Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  10. Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  11. Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  12. Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  13. Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
  14. Declare Function RegQueryValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As Long, lpcbData As Long) As Long
  15. Declare Function RegSetValueEx Lib "advapi32.dll" 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
  16. Declare Function RegSetValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Long, ByVal cbData As Long) As Long
  17. Declare Function RegSetValueExB Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Byte, ByVal cbData As Long) As Long
  18.  
  19. Const ERROR_SUCCESS = 0&
  20. Const ERROR_BADDB = 1009&
  21. Const ERROR_BADKEY = 1010&
  22. Const ERROR_CANTOPEN = 1011&
  23. Const ERROR_CANTREAD = 1012&
  24. Const ERROR_CANTWRITE = 1013&
  25. Const ERROR_OUTOFMEMORY = 14&
  26. Const ERROR_INVALID_PARAMETER = 87&
  27. Const ERROR_ACCESS_DENIED = 5&
  28. Const ERROR_NO_MORE_ITEMS = 259&
  29. Const ERROR_MORE_DATA = 234&
  30.  
  31. Const REG_NONE = 0&
  32. Const REG_SZ = 1&
  33. Const REG_EXPAND_SZ = 2&
  34. Const REG_BINARY = 3&
  35. Const REG_DWORD = 4&
  36. Const REG_DWORD_LITTLE_ENDIAN = 4&
  37. Const REG_DWORD_BIG_ENDIAN = 5&
  38. Const REG_LINK = 6&
  39. Const REG_MULTI_SZ = 7&
  40. Const REG_RESOURCE_LIST = 8&
  41. Const REG_FULL_RESOURCE_DESCRIPTOR = 9&
  42. Const REG_RESOURCE_REQUIREMENTS_LIST = 10&
  43.  
  44. Const KEY_QUERY_VALUE = &H1&
  45. Const KEY_SET_VALUE = &H2&
  46. Const KEY_CREATE_SUB_KEY = &H4&
  47. Const KEY_ENUMERATE_SUB_KEYS = &H8&
  48. Const KEY_NOTIFY = &H10&
  49. Const KEY_CREATE_LINK = &H20&
  50. Const READ_CONTROL = &H20000
  51. Const WRITE_DAC = &H40000
  52. Const WRITE_OWNER = &H80000
  53. Const SYNCHRONIZE = &H100000
  54. Const STANDARD_RIGHTS_REQUIRED = &HF0000
  55. Const STANDARD_RIGHTS_READ = READ_CONTROL
  56. Const STANDARD_RIGHTS_WRITE = READ_CONTROL
  57. Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
  58. Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
  59. Const KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
  60. Const KEY_EXECUTE = KEY_READ
  61.  
  62. Dim hKey As Long, MainKeyHandle As Long
  63. Dim rtn As Long, lBuffer As Long, sBuffer As String
  64. Dim lBufferSize As Long
  65. Dim lDataSize As Long
  66. Dim ByteArray() As Byte
  67.  
  68. 'This constant determins wether or not to display error messages to the
  69. 'user. I have set the default value to False as an error message can and
  70. 'does become irritating after a while. Turn this value to true if you want
  71. 'to debug your programming code when reading and writing to your system
  72. 'registry, as any errors will be displayed in a message box.
  73.  
  74. Const DisplayErrorMsg = False
  75.  
  76.  
  77. Function SetDWORDValue(SubKey As String, Entry As String, Value As Long)
  78.  
  79. Call ParseKey(SubKey, MainKeyHandle)
  80.  
  81. If MainKeyHandle Then
  82.    rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_WRITE, hKey) 'open the key
  83.    If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
  84.       rtn = RegSetValueExA(hKey, Entry, 0, REG_DWORD, Value, 4) 'write the value
  85.       If Not rtn = ERROR_SUCCESS Then   'if there was an error writting the value
  86.          If DisplayErrorMsg = True Then 'if the user want errors displayed
  87.             MsgBox ErrorMsg(rtn)        'display the error
  88.          End If
  89.       End If
  90.       rtn = RegCloseKey(hKey) 'close the key
  91.    Else 'if there was an error opening the key
  92.       If DisplayErrorMsg = True Then 'if the user want errors displayed
  93.          MsgBox ErrorMsg(rtn) 'display the error
  94.       End If
  95.    End If
  96. End If
  97.  
  98. End Function
  99. Function GetDWORDValue(SubKey As String, Entry As String)
  100.  
  101. Call ParseKey(SubKey, MainKeyHandle)
  102.  
  103. If MainKeyHandle Then
  104.    rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_READ, hKey) 'open the key
  105.    If rtn = ERROR_SUCCESS Then 'if the key could be opened then
  106.       rtn = RegQueryValueExA(hKey, Entry, 0, REG_DWORD, lBuffer, 4) 'get the value from the registry
  107.       If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
  108.          rtn = RegCloseKey(hKey)  'close the key
  109.          GetDWORDValue = lBuffer  'return the value
  110.       Else                        'otherwise, if the value couldnt be retreived
  111.          GetDWORDValue = "Error"  'return Error to the user
  112.          If DisplayErrorMsg = True Then 'if the user wants errors displayed
  113.             MsgBox ErrorMsg(rtn)        'tell the user what was wrong
  114.          End If
  115.       End If
  116.    Else 'otherwise, if the key couldnt be opened
  117.       GetDWORDValue = "Error"        'return Error to the user
  118.       If DisplayErrorMsg = True Then 'if the user wants errors displayed
  119.          MsgBox ErrorMsg(rtn)        'tell the user what was wrong
  120.       End If
  121.    End If
  122. End If
  123.  
  124. End Function
  125. Function SetBinaryValue(SubKey As String, Entry As String, Value As String)
  126.  
  127. Call ParseKey(SubKey, MainKeyHandle)
  128.  
  129. If MainKeyHandle Then
  130.    rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_WRITE, hKey) 'open the key
  131.    If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
  132.       lDataSize = Len(Value)
  133.       ReDim ByteArray(lDataSize)
  134.       For i = 1 To lDataSize
  135.       ByteArray(i) = Asc(Mid$(Value, i, 1))
  136.       Next
  137.       rtn = RegSetValueExB(hKey, Entry, 0, REG_BINARY, ByteArray(1), lDataSize) 'write the value
  138.       If Not rtn = ERROR_SUCCESS Then   'if the was an error writting the value
  139.          If DisplayErrorMsg = True Then 'if the user want errors displayed
  140.             MsgBox ErrorMsg(rtn)        'display the error
  141.          End If
  142.       End If
  143.       rtn = RegCloseKey(hKey) 'close the key
  144.    Else 'if there was an error opening the key
  145.       If DisplayErrorMsg = True Then 'if the user wants errors displayed
  146.          MsgBox ErrorMsg(rtn) 'display the error
  147.       End If
  148.    End If
  149. End If
  150.  
  151. End Function
  152.  
  153.  
  154. Function GetBinaryValue(SubKey As String, Entry As String)
  155.  
  156. Call ParseKey(SubKey, MainKeyHandle)
  157.  
  158. If MainKeyHandle Then
  159.    rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_READ, hKey) 'open the key
  160.    If rtn = ERROR_SUCCESS Then 'if the key could be opened
  161.       lBufferSize = 1
  162.       rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, 0, lBufferSize) 'get the value from the registry
  163.       sBuffer = Space(lBufferSize)
  164.       rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, sBuffer, lBufferSize) 'get the value from the registry
  165.       If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
  166.          rtn = RegCloseKey(hKey)  'close the key
  167.          GetBinaryValue = sBuffer 'return the value to the user
  168.       Else                        'otherwise, if the value couldnt be retreived
  169.          GetBinaryValue = "Error" 'return Error to the user
  170.          If DisplayErrorMsg = True Then 'if the user wants to errors displayed
  171.             MsgBox ErrorMsg(rtn)  'display the error to the user
  172.          End If
  173.       End If
  174.    Else 'otherwise, if the key couldnt be opened
  175.       GetBinaryValue = "Error" 'return Error to the user
  176.       If DisplayErrorMsg = True Then 'if the user wants to errors displayed
  177.          MsgBox ErrorMsg(rtn)  'display the error to the user
  178.       End If
  179.    End If
  180. End If
  181.  
  182. End Function
  183. Function DeleteKey(KeyName As String)
  184.  
  185. Call ParseKey(KeyName, MainKeyHandle)
  186.  
  187. If MainKeyHandle Then
  188.     rtn = RegDeleteKey(MainKeyHandle, KeyName) 'delete the key
  189. End If
  190.  
  191. End Function
  192.  
  193. Function GetMainKeyHandle(MainKeyName As String) As Long
  194.  
  195. Const HKEY_CLASSEs_ROOT = &H80000000
  196. Const HKEY_CURRENT_USER = &H80000001
  197. Const HKEY_LOCAL_MACHINE = &H80000002
  198. Const HKEY_USERS = &H80000003
  199. Const HKEY_PERFORMANCE_DATA = &H80000004
  200. Const HKEY_CURRENT_CONFIG = &H80000005
  201. Const HKEY_DYN_DATA = &H80000006
  202.    
  203. Select Case MainKeyName
  204.        Case "HKEY_CLASSES_ROOT"
  205.             GetMainKeyHandle = HKEY_CLASSEs_ROOT
  206.        Case "HKEY_CURRENT_USER"
  207.             GetMainKeyHandle = HKEY_CURRENT_USER
  208.        Case "HKEY_LOCAL_MACHINE"
  209.             GetMainKeyHandle = HKEY_LOCAL_MACHINE
  210.        Case "HKEY_USERS"
  211.             GetMainKeyHandle = HKEY_USERS
  212.        Case "HKEY_PERFORMANCE_DATA"
  213.             GetMainKeyHandle = HKEY_PERFORMANCE_DATA
  214.        Case "HKEY_CURRENT_CONFIG"
  215.             GetMainKeyHandle = HKEY_CURRENT_CONFIG
  216.        Case "HKEY_DYN_DATA"
  217.             GetMainKeyHandle = HKEY_DYN_DATA
  218. End Select
  219.  
  220. End Function
  221.  
  222. Function ErrorMsg(lErrorCode As Long) As String
  223.     
  224. 'If an error does accurr, and the user wants error messages displayed, then
  225. 'display one of the following error messages
  226.  
  227. Select Case lErrorCode
  228.        Case 1009, 1015
  229.             getErrorMsg = "The Registry Database is corrupt!"
  230.        Case 2, 1010
  231.             getErrorMsg = "Bad Key Name"
  232.        Case 1011
  233.             getErrorMsg = "Can't Open Key"
  234.        Case 4, 1012
  235.             getErrorMsg = "Can't Read Key"
  236.        Case 5
  237.             getErrorMsg = "Access to this key is denied"
  238.        Case 1013
  239.             getErrorMsg = "Can't Write Key"
  240.        Case 8, 14
  241.             getErrorMsg = "Out of memory"
  242.        Case 87
  243.             getErrorMsg = "Invalid Parameter"
  244.        Case 234
  245.             getErrorMsg = "There is more data than the buffer has been allocated to hold."
  246.        Case Else
  247.             getErrorMsg = "Undefined Error Code:  " & Str$(lErrorCode)
  248. End Select
  249.  
  250. End Function
  251.  
  252.  
  253.  
  254. Function GetStringValue(SubKey As String, Entry As String)
  255.  
  256. Call ParseKey(SubKey, MainKeyHandle)
  257.  
  258. If MainKeyHandle Then
  259.    rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_READ, hKey) 'open the key
  260.    If rtn = ERROR_SUCCESS Then 'if the key could be opened then
  261.       sBuffer = Space(255)     'make a buffer
  262.       lBufferSize = Len(sBuffer)
  263.       rtn = RegQueryValueEx(hKey, Entry, 0, REG_SZ, sBuffer, lBufferSize) 'get the value from the registry
  264.       If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
  265.          rtn = RegCloseKey(hKey)  'close the key
  266.          sBuffer = Trim(sBuffer)
  267.         If sBuffer <> "" Then
  268.             If InStr(1, sBuffer, Chr$(0)) > 0 Then
  269.                 GetStringValue = Left(sBuffer, Len(sBuffer) - 1) 'return the value to the user
  270.             Else
  271.                 GetStringValue = sBuffer
  272.             End If
  273.         End If
  274.       Else                        'otherwise, if the value couldnt be retreived
  275.          GetStringValue = "Error" 'return Error to the user
  276.          If DisplayErrorMsg = True Then 'if the user wants errors displayed then
  277.             MsgBox ErrorMsg(rtn)  'tell the user what was wrong
  278.          End If
  279.       End If
  280.    Else 'otherwise, if the key couldnt be opened
  281.       GetStringValue = "Error"       'return Error to the user
  282.       If DisplayErrorMsg = True Then 'if the user wants errors displayed then
  283.          MsgBox ErrorMsg(rtn)        'tell the user what was wrong
  284.       End If
  285.    End If
  286. End If
  287.  
  288. End Function
  289.  
  290. Private Sub ParseKey(KeyName As String, Keyhandle As Long)
  291.     
  292. rtn = InStr(KeyName, "\") 'return if "\" is contained in the Keyname
  293.  
  294. If Left(KeyName, 5) <> "HKEY_" Or Right(KeyName, 1) = "\" Then 'if the is a "\" at the end of the Keyname then
  295.    MsgBox "Incorrect Format:" + Chr(10) + Chr(10) + KeyName 'display error to the user
  296.    Exit Sub 'exit the procedure
  297. ElseIf rtn = 0 Then 'if the Keyname contains no "\"
  298.    Keyhandle = GetMainKeyHandle(KeyName)
  299.    KeyName = "" 'leave Keyname blank
  300. Else 'otherwise, Keyname contains "\"
  301.    Keyhandle = GetMainKeyHandle(Left(KeyName, rtn - 1)) 'seperate the Keyname
  302.    KeyName = Right(KeyName, Len(KeyName) - rtn)
  303. End If
  304.  
  305. End Sub
  306. Function CreateKey(SubKey As String)
  307.  
  308. Call ParseKey(SubKey, MainKeyHandle)
  309.  
  310. If MainKeyHandle Then
  311.    rtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'create the key
  312.    If rtn = ERROR_SUCCESS Then 'if the key was created then
  313.       rtn = RegCloseKey(hKey)  'close the key
  314.    End If
  315. End If
  316.  
  317. End Function
  318. Function SetStringValue(SubKey As String, Entry As String, Value As String)
  319.  
  320. Call ParseKey(SubKey, MainKeyHandle)
  321.  
  322. If MainKeyHandle Then
  323.    rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_WRITE, hKey) 'open the key
  324.    If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
  325.       rtn = RegSetValueEx(hKey, Entry, 0, REG_SZ, ByVal Value, Len(Value)) 'write the value
  326.       If Not rtn = ERROR_SUCCESS Then   'if there was an error writting the value
  327.          If DisplayErrorMsg = True Then 'if the user wants errors displayed
  328.             MsgBox ErrorMsg(rtn)        'display the error
  329.          End If
  330.       End If
  331.       rtn = RegCloseKey(hKey) 'close the key
  332.    Else 'if there was an error opening the key
  333.       If DisplayErrorMsg = True Then 'if the user wants errors displayed
  334.          MsgBox ErrorMsg(rtn)        'display the error
  335.       End If
  336.    End If
  337. End If
  338.  
  339. End Function
  340.  
  341.  
  342. Public Function IsWinampLocated() As Boolean
  343. Dim sString As String
  344. sString = GetStringValue("HKEY_CLASSES_ROOT\Applications\Winamp.exe\shell\open\command", "")
  345. If sString <> "" Then
  346.     sString = Left$(sString, InStr(1, sString, Chr$(0)) - 1)
  347.     sString = Replace$(sString, Chr$(34), "")
  348.     sString = Replace$(sString, "%1", "")
  349.     If Dir$(sString, vbNormal) <> "" Then
  350.         IsWinampLocated = True
  351.     End If
  352.     sWinAmpLocation = sString
  353. End If
  354. End Function
  355.  
  356.