home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 4_2005-2006.ISO / data / Zips / Registry_E1917277252005.psc / mRecovery.bas < prev    next >
BASIC Source File  |  2005-07-25  |  11KB  |  266 lines

  1. Attribute VB_Name = "mRecovery"
  2. Option Explicit
  3.  
  4. Private Type SMGRSTATUS
  5.     nStatus                                   As Long
  6.     llSequenceNumber                          As Currency
  7. End Type
  8.  
  9. Private Const REG_SZ                      As Long = &H1
  10. Private Const REG_DWORD                   As Long = &H4
  11. Private Const HKEY_CLASSES_ROOT           As Long = &H80000000
  12. Private Const HKEY_CURRENT_USER           As Long = &H80000001
  13. Private Const HKEY_LOCAL_MACHINE          As Long = &H80000002
  14. Private Const HKEY_USERS                  As Long = &H80000003
  15.  
  16. Private Const TOKEN_QUERY                 As Long = &H8&
  17. Private Const TOKEN_ADJUST_PRIVILEGES     As Long = &H20&
  18. Private Const SE_PRIVILEGE_ENABLED        As Long = &H2
  19. Private Const SE_RESTORE_NAME             As String = "SeRestorePrivilege"
  20. Private Const SE_BACKUP_NAME              As String = "SeBackupPrivilege"
  21. Private Const REG_FORCE_RESTORE           As Long = 8&
  22. Private Const READ_CONTROL                As Long = &H20000
  23. Private Const SYNCHRONIZE                 As Long = &H100000
  24. Private Const STANDARD_RIGHTS_ALL         As Long = &H1F0000
  25. Private Const KEY_QUERY_VALUE             As Long = &H1
  26. Private Const KEY_SET_VALUE               As Long = &H2
  27. Private Const KEY_CREATE_SUB_KEY          As Long = &H4
  28. Private Const KEY_ENUMERATE_SUB_KEYS      As Long = &H8
  29. Private Const KEY_NOTIFY                  As Long = &H10
  30. Private Const KEY_CREATE_LINK             As Long = &H20
  31. Private Const KEY_ALL_ACCESS              As Double = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
  32.  
  33. Private Type LUID
  34.     lowpart                                   As Long
  35.     highpart                                  As Long
  36. End Type
  37.  
  38. Private Type LUID_AND_ATTRIBUTES
  39.     pLuid                                     As LUID
  40.     Attributes                                As Long
  41. End Type
  42.  
  43. Private Type TOKEN_PRIVILEGES
  44.     PrivilegeCount                            As Long
  45.     Privileges                                As LUID_AND_ATTRIBUTES
  46. End Type
  47.  
  48. Private Declare Function GetCurrentProcess Lib "Kernel32" () As Long
  49. Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
  50. Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, _
  51.                                                                             ByVal lpFile As String, _
  52.                                                                             lpSecurityAttributes As Any) As Long
  53.                                                                             
  54. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  55.  
  56. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, _
  57.                                                                                 ByVal lpSubKey As String, _
  58.                                                                                 ByVal ulOptions As Long, _
  59.                                                                                 ByVal samDesired As Long, _
  60.                                                                                 phkResult As Long) As Long
  61.                                                                                 
  62. Private Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, _
  63.                                                                                   ByVal lpFile As String, _
  64.                                                                                   ByVal dwFlags As Long) As Long
  65.                                                                                   
  66. Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, _
  67.                                                                    ByVal DisableAllPriv As Long, _
  68.                                                                    NewState As TOKEN_PRIVILEGES, _
  69.                                                                    ByVal BufferLength As Long, _
  70.                                                                    PreviousState As TOKEN_PRIVILEGES, _
  71.                                                                    ReturnLength As Long) As Long
  72.                                                                    
  73. Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As Any, _
  74.                                                                                                 ByVal lpName As String, _
  75.                                                                                                 lpLuid As LUID) As Long
  76.                                                                                                 
  77. Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, _
  78.                                                               ByVal DesiredAccess As Long, _
  79.                                                               TokenHandle As Long) As Long
  80.  
  81.  
  82. Private Sub Make_Directory(ByVal sFolder As String)
  83.  
  84.     MakeSureDirectoryPathExists sFolder
  85.  
  86. End Sub
  87.  
  88. Public Sub Backup_Key()
  89.  
  90. Dim sPath As String
  91. Dim hKey  As Long
  92.  
  93.         '//backup used by monitor sub
  94.     sPath = App.Path & "\Recovery\"
  95.     Make_Directory sPath
  96.     sPath = sPath & sModmname & ".kbs"
  97.  
  98.     If EnablePrivilege(SE_BACKUP_NAME) Then
  99.         RegOpenKeyEx lModlhkey, sModskey, 0&, KEY_ALL_ACCESS, hKey
  100.         If LenB(Dir(sPath)) Then
  101.             Kill sPath
  102.         End If
  103.         RegSaveKey hKey, sPath, ByVal 0&
  104.         RegCloseKey hKey
  105.     End If
  106.  
  107. End Sub
  108.  
  109. Private Function EnablePrivilege(seName As String) As Boolean
  110.  
  111. Dim p_lngRtn           As Long
  112. Dim p_lngToken         As Long
  113. Dim p_lngBufferLen     As Long
  114. Dim p_typLUID          As LUID
  115. Dim p_typTokenPriv     As TOKEN_PRIVILEGES
  116. Dim p_typPrevTokenPriv As TOKEN_PRIVILEGES
  117.  
  118.     p_lngRtn = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, p_lngToken)
  119.     If p_lngRtn = 0 Then
  120.         Exit Function
  121.     ElseIf Err.LastDllError <> 0 Then
  122.         Exit Function
  123.     End If
  124.     p_lngRtn = LookupPrivilegeValue(0&, seName, p_typLUID)
  125.     If p_lngRtn = 0 Then
  126.         Exit Function
  127.     End If
  128.     With p_typTokenPriv
  129.         .PrivilegeCount = 1
  130.         .Privileges.Attributes = SE_PRIVILEGE_ENABLED
  131.         .Privileges.pLuid = p_typLUID
  132.     End With
  133.  
  134.     EnablePrivilege = (AdjustTokenPrivileges(p_lngToken, False, p_typTokenPriv, Len(p_typPrevTokenPriv), p_typPrevTokenPriv, p_lngBufferLen) <> 0)
  135.  
  136. End Function
  137.  
  138. Public Function Save_Key(ByVal sKey As String, _
  139.                          lKey As Long, _
  140.                          ByVal sName As String) As Boolean
  141.  
  142. Dim hKey     As Long
  143. Dim sPath    As String
  144. Dim sCurDate As String
  145. Dim RetVal   As Long
  146.  
  147.     sCurDate = Format$(Now, ("dd_mmm_yy"))
  148.     sPath = App.Path & "\Recovery\Backup-" & sCurDate & Chr$(92)
  149.     Make_Directory sPath
  150.     sName = Trim$(String_Convert(sName)) & ".kbs"
  151.     sPath = sPath & sName
  152.     'Debug.Print sPath
  153.     '//needed user proofing, installing to
  154.     '//wrong key should fail, but you never know..
  155.     '//conversion needed to save file name
  156.     '//but must be a better way, if you know one, post it and email me
  157.     If EnablePrivilege(SE_BACKUP_NAME) Then
  158.         RegOpenKeyEx lKey, sKey, 0&, KEY_ALL_ACCESS, hKey
  159.         If LenB(Dir(sPath)) Then
  160.             Kill sPath
  161.         End If
  162.         RetVal = RegSaveKey(hKey, sPath, ByVal 0&)
  163.         RegCloseKey hKey
  164.     End If
  165.  
  166.     If RetVal = 0 Then
  167.         Save_Key = True
  168.     End If
  169.  
  170. End Function
  171.  
  172. Public Function Deploy_Key(ByVal sImage As String) As Boolean
  173.  
  174. Dim hKey   As Long
  175. Dim sKey   As String
  176. Dim lHKey  As Long
  177. Dim sHkey  As String
  178. Dim sPath  As String
  179. Dim RetVal As Long
  180.  
  181.     sPath = sImage
  182.     sImage = Mid$(sImage, InStrRev(sImage, Chr$(92)) + 1)
  183.     sImage = String_Convert(sImage)
  184.     sHkey = Left$(sImage, InStr(sImage, Chr$(92)) - 1)
  185.     sKey = Mid$(sImage, InStr(sImage, Chr$(92)) + 1)
  186.     sKey = Left$(sKey, InStr(sKey, Chr$(46)) - 1)
  187.     '//a lot of work just to user proof
  188.     '//a better way would be to give the
  189.     '//file a generic name, and write the path
  190.     '//in the file header info, or to the file
  191.     '//itself.. just a quick fix here though
  192.     Select Case sHkey
  193.     Case "HKEY_CLASSES_ROOT"
  194.         lHKey = HKEY_CLASSES_ROOT
  195.     Case "HKEY_CURRENT_USER"
  196.         lHKey = HKEY_CURRENT_USER
  197.     Case "HKEY_LOCAL_MACHINE"
  198.         lHKey = HKEY_LOCAL_MACHINE
  199.     Case "HKEY_USERS"
  200.         lHKey = HKEY_USERS
  201.     Case "HKEY_CURRENT_CONFIG"
  202.         lHKey = HKEY_CURRENT_CONFIG
  203.     Case Else
  204.         MsgBox "Invalid Root Key!" & vbNewLine & _
  205.        "Ex. HKEY_CURRENT_USER\AppEvents", vbExclamation, "Invalid Key!"
  206.     End Select
  207.  
  208.     If EnablePrivilege(SE_RESTORE_NAME) Then
  209.         RegOpenKeyEx lHKey, sKey, 0&, KEY_ALL_ACCESS, hKey
  210.         RetVal = RegRestoreKey(hKey, sPath, REG_FORCE_RESTORE)
  211.         RegCloseKey hKey
  212.     End If
  213.  
  214.     If RetVal = 0 Then
  215.         Deploy_Key = True
  216.     End If
  217.  
  218. End Function
  219.  
  220. Private Function String_Convert(ByVal sName As String) As String
  221.  
  222. '//found this on psc, but you should make something better
  223. Dim LookUpTable(0 To 255) As Byte
  224. Dim i                     As Integer
  225. Dim c                     As Long
  226. Dim newstr()              As Byte
  227.  
  228.     For i = 0 To 255
  229.         LookUpTable(i) = i
  230.     Next i
  231.     LookUpTable(92) = 45
  232.     LookUpTable(45) = 92
  233.     newstr() = sName
  234.  
  235.     For i = 0 To UBound(newstr) Step 2
  236.         If LookUpTable(newstr(i)) <> 0 Then
  237.             newstr(c) = LookUpTable(newstr(i))
  238.             c = c + 2
  239.         End If
  240.     Next i
  241.     ReDim Preserve newstr(c)
  242.     String_Convert = newstr()
  243.  
  244. End Function
  245.  
  246. Public Sub Restore_Key()
  247.  
  248. Dim sPath As String
  249. Dim hKey  As Long
  250.  
  251.     '//restore key via monitor sub
  252.     sPath = App.Path & "\Recovery\"
  253.     sPath = sPath & sModmname & ".kbs"
  254.  
  255.     If Not FileExists(sPath) Then
  256.         MsgBox "Can Not Restore Key!" & vbNewLine & "Backup Key File is Missing!", vbExclamation, "No .kbs File!"
  257.     Else
  258.         If EnablePrivilege(SE_RESTORE_NAME) Then
  259.             RegOpenKeyEx lModlhkey, sModskey, 0&, KEY_ALL_ACCESS, hKey
  260.             RegRestoreKey hKey, sPath, REG_FORCE_RESTORE
  261.             RegCloseKey hKey
  262.         End If
  263.     End If
  264.  
  265. End Sub
  266.