Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
11 KiB

  1. Attribute VB_Name = "Config"
  2. DefInt A-Z
  3. Option Explicit
  4. '========Public Config Vars and Structs========
  5. 'Main Form
  6. Public MainFormHeight As Long
  7. Public MainFormWidth As Long
  8. Public MainFormVDivider As Long
  9. Public MainFormHDivider As Long
  10. Public StatusBar As Boolean
  11. Public DataListIdColWidth As Long
  12. Public DataListNameColWidth As Long
  13. Public DataListAttrColWidth As Long
  14. Public DataListUTColWidth As Long
  15. Public DataListDTColWidth As Long
  16. Public DataListDataColWidth As Long
  17. Public ErrorListKeyColWidth As Long
  18. Public ErrorListPropColWidth As Long
  19. Public ErrorListIdColWidth As Long
  20. Public ErrorListSeverityColWidth As Long
  21. Public ErrorListDescColWidth As Long
  22. Public MaxKeySize As Long
  23. Public MaxPropSize As Long
  24. Public MaxNumErrors As Long
  25. Const MainFormHeightDefault = 8000
  26. Const MainFormWidthDefault = 12000
  27. Const MainFormHDividerDefault = 2500
  28. Const MainFormVDividerDefault = 2500
  29. Const StatusBarDefault = True
  30. Const DataListIdColWidthDefault = 800
  31. Const DataListNameColWidthDefault = 1500
  32. Const DataListAttrColWidthDefault = 1000
  33. Const DataListUTColWidthDefault = 700
  34. Const DataListDTColWidthDefault = 500
  35. Const DataListDataColWidthDefault = 1500
  36. Const ErrorListKeyColWidthDefault = 3000
  37. Const ErrorListPropColWidthDefault = 800
  38. Const ErrorListIdColWidthDefault = 800
  39. Const ErrorListSeverityColWidthDefault = 800
  40. Const ErrorListDescColWidthDefault = 3000
  41. Const MaxKeySizeDefault = 102400
  42. Const MaxPropSizeDefault = 1024
  43. Const MaxNumErrorsDefault = 100
  44. '========API Declarations========
  45. 'Load APIs for registry editing
  46. Private Type FILETIME
  47. dwLowDateTime As Long
  48. dwHighDateTime As Long
  49. End Type
  50. Private 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
  51. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  52. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" 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, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
  53. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  54. Private 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 ' Note that if you declare the lpData parameter as String, you must pass it By Value.
  55. Private 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 ' Note that if you declare the lpData parameter as String, you must pass it By Value.
  56. 'Origial Declaration, the reserved parameter was set up wrong and I set up the class parameter to always be double NULL
  57. 'Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
  58. Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As Long, ByVal lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
  59. Const ERROR_SUCCESS = 0&
  60. Const HKEY_CURRENT_USER = &H80000001
  61. Const HKEY_LOCAL_MACHINE = &H80000002
  62. Const STANDARD_RIGHTS_ALL = &H1F0000
  63. Const KEY_QUERY_VALUE = &H1
  64. Const KEY_SET_VALUE = &H2
  65. Const KEY_CREATE_SUB_KEY = &H4
  66. Const KEY_ENUMERATE_SUB_KEYS = &H8
  67. Const KEY_NOTIFY = &H10
  68. Const KEY_CREATE_LINK = &H20
  69. Const SYNCHRONIZE = &H100000
  70. Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE _
  71. Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS _
  72. Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
  73. Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
  74. Const REG_CREATED_NEW_KEY = &H1 ' New Registry Key created
  75. Const REG_OPENED_EXISTING_KEY = &H2 ' Existing Key opened
  76. Const REG_SZ = 1
  77. Sub LoadConfig()
  78. LoadMainFormConfig
  79. End Sub
  80. Sub LoadMainFormConfig()
  81. Dim Ret As Long
  82. Dim Disposition As Long
  83. Dim KeyHandle As Long
  84. 'Open/Create Key
  85. Ret = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\MetEdit", _
  86. 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, KeyHandle, Disposition)
  87. If Ret <> ERROR_SUCCESS Then Debug.Print "Error creating or opening MetEdit key in LoadMainFormConfig"
  88. 'Get Settings
  89. MainFormHeight = RegGetLong(KeyHandle, "Main Form Height", MainFormHeightDefault)
  90. MainFormWidth = RegGetLong(KeyHandle, "Main Form Width", MainFormWidthDefault)
  91. MainFormVDivider = RegGetLong(KeyHandle, "Main Form V Divider", MainFormVDividerDefault)
  92. MainFormHDivider = RegGetLong(KeyHandle, "Main Form H Divider", MainFormHDividerDefault)
  93. StatusBar = RegGetBoolean(KeyHandle, "Status Bar", StatusBarDefault)
  94. DataListIdColWidth = RegGetLong(KeyHandle, "Data List Id Col Width", DataListIdColWidthDefault)
  95. DataListNameColWidth = RegGetLong(KeyHandle, "Data List Name Col Width", DataListNameColWidthDefault)
  96. DataListAttrColWidth = RegGetLong(KeyHandle, "Data List Attr Col Width", DataListAttrColWidthDefault)
  97. DataListUTColWidth = RegGetLong(KeyHandle, "Data List UT Col Width", DataListUTColWidthDefault)
  98. DataListDTColWidth = RegGetLong(KeyHandle, "Data List DT Col Width", DataListDTColWidthDefault)
  99. DataListDataColWidth = RegGetLong(KeyHandle, "Data List Data Col Width", DataListDataColWidthDefault)
  100. ErrorListKeyColWidth = RegGetLong(KeyHandle, "Error List Key Col Width", ErrorListKeyColWidthDefault)
  101. ErrorListPropColWidth = RegGetLong(KeyHandle, "Error List Prop Col Width", ErrorListPropColWidthDefault)
  102. ErrorListIdColWidth = RegGetLong(KeyHandle, "Error List Id Col Width", ErrorListIdColWidthDefault)
  103. ErrorListSeverityColWidth = RegGetLong(KeyHandle, "Error List Severity Col Width", ErrorListSeverityColWidthDefault)
  104. ErrorListDescColWidth = RegGetLong(KeyHandle, "Error List Desc Col Width", ErrorListDescColWidthDefault)
  105. MaxKeySize = RegGetLong(KeyHandle, "Max Key Size", MaxKeySizeDefault)
  106. MaxPropSize = RegGetLong(KeyHandle, "Max Property Size", MaxPropSizeDefault)
  107. MaxNumErrors = RegGetLong(KeyHandle, "Max Number of Errors", MaxNumErrorsDefault)
  108. 'Close Key
  109. Ret = RegCloseKey(KeyHandle)
  110. End Sub
  111. Sub SaveConfig()
  112. SaveMainFormConfig
  113. End Sub
  114. Sub SaveMainFormConfig()
  115. Dim KeyHandle As Long
  116. Dim Ret As Long
  117. Dim Disposition As Long
  118. 'Open Key
  119. Ret = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\MetEdit", _
  120. 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, KeyHandle, Disposition)
  121. If Ret <> ERROR_SUCCESS Then Debug.Print "Error creating or opening MetEdit key in SaveMainFormConfig"
  122. 'Save Values
  123. RegSetLong KeyHandle, "Main Form Height", MainFormHeight
  124. RegSetLong KeyHandle, "Main Form Width", MainFormWidth
  125. RegSetLong KeyHandle, "Main Form V Divider", MainFormVDivider
  126. RegSetLong KeyHandle, "Main Form H Divider", MainFormHDivider
  127. RegSetBoolean KeyHandle, "Status Bar", StatusBar
  128. RegSetLong KeyHandle, "Data List Id Col Width", DataListIdColWidth
  129. RegSetLong KeyHandle, "Data List Name Col Width", DataListNameColWidth
  130. RegSetLong KeyHandle, "Data List Attr Col Width", DataListAttrColWidth
  131. RegSetLong KeyHandle, "Data List UT Col Width", DataListUTColWidth
  132. RegSetLong KeyHandle, "Data List DT Col Width", DataListDTColWidth
  133. RegSetLong KeyHandle, "Data List Data Col Width", DataListDataColWidth
  134. RegSetLong KeyHandle, "Error List Key Col Width", ErrorListKeyColWidth
  135. RegSetLong KeyHandle, "Error List Prop Col Width", ErrorListPropColWidth
  136. RegSetLong KeyHandle, "Error List Id Col Width", ErrorListIdColWidth
  137. RegSetLong KeyHandle, "Error List Severity Col Width", ErrorListSeverityColWidth
  138. RegSetLong KeyHandle, "Error List Desc Col Width", ErrorListDescColWidth
  139. RegSetLong KeyHandle, "Max Key Size", MaxKeySize
  140. RegSetLong KeyHandle, "Max Property Size", MaxPropSize
  141. RegSetLong KeyHandle, "Max Number of Errors", MaxNumErrors
  142. 'Close Key
  143. Ret = RegCloseKey(KeyHandle)
  144. End Sub
  145. Function ConvertCString(CString As String) As String
  146. 'Cleans up a C style string into a VB string
  147. Dim i As Integer
  148. Dim CharStr As String
  149. Dim NullStr As String
  150. Dim RetStr As String
  151. 'Find the first NULL
  152. NullStr = String(1, 0)
  153. i = 1
  154. Do
  155. CharStr = Mid(CString, i, 1)
  156. i = i + 1
  157. Loop While ((i <= Len(CString)) And (CharStr <> NullStr))
  158. 'If we found the null, keep the part before the null
  159. If (CharStr = NullStr) Then
  160. ConvertCString = Left(CString, i - 2)
  161. Else
  162. ConvertCString = CString
  163. End If
  164. End Function
  165. Sub RegNukeKey(KeyHnd As Long, SubKeyStr As String)
  166. 'Nukes a key and all subkeys, should work with both ninety-blah and NT
  167. Dim Ret As Long
  168. Dim SubKeyHnd As Long
  169. 'Open the subkey so we can look for sub keys
  170. Ret = RegOpenKeyEx(KeyHnd, SubKeyStr, 0, KEY_ALL_ACCESS, SubKeyHnd)
  171. If Ret <> ERROR_SUCCESS Then Exit Sub
  172. 'Recursivly nuke all of the subsubkeys
  173. Dim i As Long
  174. Dim SubSubKeyStr As String
  175. Dim LastWrite As FILETIME
  176. i = 0
  177. SubSubKeyStr = String(301, "X") 'Trick it into allocating memory
  178. Ret = RegEnumKeyEx(SubKeyHnd, i, SubSubKeyStr, 300, 0, 0, 0, LastWrite)
  179. Do While (Ret = ERROR_SUCCESS)
  180. SubSubKeyStr = RTrim(ConvertCString(SubSubKeyStr))
  181. RegNukeKey SubKeyHnd, SubSubKeyStr
  182. 'i = i + 1 Not needed since the next one becomes index 0
  183. SubSubKeyStr = String(301, "X") 'Trick it into reallocating memory
  184. Ret = RegEnumKeyEx(SubKeyHnd, i, SubSubKeyStr, 300, 0, 0, 0, LastWrite)
  185. Loop
  186. 'Close the target key
  187. Ret = RegCloseKey(SubKeyHnd)
  188. 'Delete the target key
  189. Ret = RegDeleteKey(KeyHnd, SubKeyStr)
  190. End Sub
  191. Function RegGetString(KeyHandle As Long, Var As String, MaxLen As Long, DefaultStr As String) As String
  192. Dim OutStr As String
  193. Dim Ret As Long
  194. OutStr = String(MaxLen + 1, "X") 'Trick it into allocating memory
  195. Ret = RegQueryValueEx(KeyHandle, Var, 0, REG_SZ, OutStr, MaxLen + 1)
  196. If Ret <> ERROR_SUCCESS Then
  197. 'If we didn't get it, set it to default
  198. OutStr = DefaultStr
  199. Ret = RegSetValueEx(KeyHandle, Var, 0, REG_SZ, OutStr, Len(OutStr) + 1)
  200. If Ret <> ERROR_SUCCESS Then Error.Print "Error setting " & Var & " value"
  201. Else
  202. 'If we got it, convert it to a VB String
  203. OutStr = Left(Trim(ConvertCString(OutStr)), MaxLen)
  204. End If
  205. RegGetString = OutStr
  206. End Function
  207. Sub RegSetString(KeyHandle As Long, Var As String, Val As String)
  208. Dim Ret As Long
  209. Ret = RegSetValueEx(KeyHandle, Var, 0, REG_SZ, Val + String(1, 0), Len(Val) + 1)
  210. If Ret <> ERROR_SUCCESS Then Error.Print "Error setting registry Var=" & Var & " Val=" & Val
  211. End Sub
  212. Function RegGetBoolean(KeyHandle As Long, Var As String, Default As Boolean) As Boolean
  213. Dim BoolStr As String
  214. If Default = True Then
  215. BoolStr = RegGetString(KeyHandle, Var, 2, "1")
  216. Else
  217. BoolStr = RegGetString(KeyHandle, Var, 2, "0")
  218. End If
  219. If BoolStr = "1" Then
  220. RegGetBoolean = True
  221. Else
  222. RegGetBoolean = False
  223. End If
  224. End Function
  225. Sub RegSetBoolean(KeyHandle As Long, Var As String, Val As Boolean)
  226. If Val Then
  227. RegSetString KeyHandle, Var, "1"
  228. Else
  229. RegSetString KeyHandle, Var, "0"
  230. End If
  231. End Sub
  232. Function RegGetLong(KeyHandle As Long, Var As String, Default As Long) As Long
  233. Dim NumStr As String
  234. NumStr = RegGetString(KeyHandle, Var, 20, Str(Default))
  235. RegGetLong = CLng(NumStr)
  236. End Function
  237. Sub RegSetLong(KeyHandle As Long, Var As String, Val As Long)
  238. RegSetString KeyHandle, Var, Str(Val)
  239. End Sub