How to operate the registry in VB.

zhaozj2021-02-11  164

In VB, the system provides two functions for registry operations. But they can only operate a specific key. It is often unquestable to use it. The following function can implement all the operations of the registry. And have a standard VB function. Generality and ease of use. Please give pointers..

Public Function SysRegControl (Optional ByVal RootKey As RegRootKey = regHKEY_LOCAL_MACHINE, Optional ByVal SubKey As String = "", Optional ByVal Key As String = "QiLin", Optional ByRef KeyValue As Variant = "", Optional regKeyType As regKeyTypes = regTypeString, Optional ByVal id As regontrolid = regsetKeyValue) as booleanattribute sysregControl.vb_description = "'setRegKey function / r / n' function: / r / n 'Specify the key button in the registry / R / N' parameter: / r / n 'rootkey root key / r / n'RootKey Description / r / n '{regHKEY_CLASSES_ROOT = & H80000000 / r / n' regHKEY_CURRENT_USER = & H80000001 / r / n 'regHKEY_LOCAL_MACHINE = & H80000002 / r / n' regHKEY_USERS = & H80000003 / r / n 'regHKEY_PERFORMANCE_DATA = & H80000004 / R / N 'REGHKEY_CURRENT_CONFIG = & H800005 / R / N' REGHKEY_DYN_DATA = & H80000006 / R / N '} / r / n' Subkey sub-key path / r / n 'key set key name / r / n' keyValue set key value / r / n 'regkeytype specified type / R / N'RegKeyType Description: / r / n' {/ r / n 'regtypebinary = & h00000001' binary / r / n 'Regtypedword = & h00000002' DWORD / R / N 'regtypeString = & H00000003' String / R / N '} / r / n' ID Function function number / R / n 'function ID Description: / r / n' {regsetKeyValue = 111 Set key value / r / n 'reggetKeyValue = 112' 取 取 值 值 值 值 r r "" 取 取*********************************************************** ************* 'SetRegKey Function' Features: 'Configuring the key button in the registry "parameters:'

RootKey root key 'RootKey described' {regHKEY_CLASSES_ROOT = & H80000000 'regHKEY_CURRENT_USER = & H80000001' regHKEY_LOCAL_MACHINE = & H80000002 'regHKEY_USERS = & H80000003' regHKEY_PERFORMANCE_DATA = & H80000004 'regHKEY_CURRENT_CONFIG = & H80000005' regHKEY_DYN_DATA = & H80000006 '}' SubKey subkey path 'Key Set key name' KeyValue key set 'type regKeyType the key' 'regKeyType Description:' { 'regTypeBinary = & H00000001' Binary 'regTypeDword = & H00000002' DWORD 'regTypeString = & H00000003' String '}' ID function operation function number "function ID Description: '{ RegsetKeyValue = 111 'Set key value' reggetKeyvalue = 112 '取 值 value' regreatkey = 113 'creation sub-key' regdeleteKeys = 114 'Remove the last level key' regdlexKey = 115 'Delete non-final substem key' regdeletevalues ​​= 116 'delete Key value 'regother = 120' reserved operation ID '}' Return Value: 'True Operation Success' FALSE Operation Failure '(c) 2001.3.2' *************************************************** ****************************************************** DIM i As Longon Error Goto RegoptionError'iff Rootkey Then

SELECT case ID '=============================================== =============================================================41 Key value '================================================= ========================================== r = regopenkeyex (rootkey, Subkey, 0, Key_Write, HKEY) IF RTN = Error_Success Ten '{SELECT CASE REGKEYTYPE' --------------------------------- -------------------------------------------------- ----- Case Regtypebinary '= & H00000001' Binary

'The parameter keyvalue in this mode must be in the form of string, call example:' sysregControl Reghkey_local_machine, "jadgekylin / jklpos", "jadgekylin01@yesky.com", regtypebinary, regsetkeyvalue "------- -------------------------------------------------- ------------------------------- if Vartype (keyvalue) <> vbstring the 'parameter illegal = error_success 1' EXIT SETELECT ELSE LDATASIZE = LEN (KeyValue) Redim ByteArray (LDataSize) for i = 1 To LDataSize ByteArray (i) = ASC (MID $ (KeyValue, I, 1)) Next RTN = RegSetValueExb (HKEY, Key, 0, Reg_binary, ByteArray (1), LDataSize) 'Write the value endiff f ------------------------------------ -------------------------------------------------- - Case RegtypedWord '= & H00000002' DWORD 'Call instance:' sysregControl Reghkey_local_machine, "jadgekylin / jklpos", "pos", 1, regtyped, regsetkeyvalue "------------ -------------------------------------------------- ----------------------------

If VarType (KeyValue) <> vbLong And VarType (KeyValue) <> vbInteger Then rtn = ERROR_SUCCESS 1 'exit select Else rtn = RegSetValueExA (hKey, Key, 0, REG_DWORD, KeyValue, 4)' write the value End If'- -------------------------------------------------- ------------------------------------- Case RegtypeString '= & H00000003' String

'Call example:' sysregControl REGHKEY_LOCAL_MACHINE, "Jadgekylin / Jklpos", "POS", "1", RegtypeString, RegsetKeyValue '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- -------------- if Vartype (KeyValue) <> vbstring the 'parameter illegal RTN = Error_Success 1' EXIT SETELECT ELSE RTN = RegSetValueex (HKEY, Key, 0, Reg_SZ, Byval KeyValue , Len (KeyValue)) 'Write the Value End IF' ------------------------------------- -------------------------------------------------- - End Select '} If Not rtn = ERROR_SUCCESS Then' if the was an error writting the value rtn = RegCloseKey (hKey) SysRegControl = False 'call failed Exit Function End If rtn = RegCloseKey (hKey)' close the key

End if 'rtn = error_suCcess' ========================================== ============================================================================================================================================================================================================= 112 '取 值 value' =============================================== ============================================= r = regopenkeyex (rootkey , Subkey, 0, Key_Read, HKEY) IF RTN = Error_Success Ten 'if the key could be opened' {

Select Case RegkeyType '---------------------------------------------- ------------------------------------------ Case Regtypebinary '= & H00000001' binary ' KeyValue obtains key values ​​as a pilot variable, call example: 'Dim A AS String'sysRegcontrol Reghkey_local_machine, "Jadgekylin / JKLPOS", "POS", A, Regtypebinary, ReggetKeyValue "------------- -------------------------------------------------- ------------------------- RTN = RegQueryValueex (HKEY, KEY, 0, REG_BINARY, 0, LBUFFERSIZE) 'GET The value from the registry sbuffer = Space (lBufferSize) rtn = RegQueryValueEx (hKey, Key, 0, REG_BINARY, sBuffer, lBufferSize) 'get the value from the registry If Not rtn = ERROR_SUCCESS Then' if the was an error writting the value rtn = RegCloseKey (hKey) SysRegControl = False 'call failed Exit function else keyvalue = sbuffer end if rtn = reglosekey (HKEY)' CLO SE the key

'------------------------------------- --------------------------------------- Case RegtypedWord '= & H00000002' DWord''KeyValue as Value Variables Get key values, call examples: 'Dim A As Long_Machine, "Jadgekylin / JKLPOS", "POS", A, RegtypeString, ReggetKeyValue "--------------- -------------------------------------------------- ----------------------- RTN = RegQueryValueexa (HKEY, Key, 0, Reg_dword, lbuffer, 4) 'get the value from the registry if not = ERROR_SUCCESS Then 'if the was an error writting the value rtn = RegCloseKey (hKey) SysRegControl = False' call failed Exit Function Else KeyValue = lBuffer End If rtn = RegCloseKey (hKey) 'close the key' -------- -------------------------------------------------- ------------------------------ Case RegtypeString '= & H00000003' String

'KeyValue obtains key value as a pilot variable, call example:' Dim A AS String'sysRegControl Reghkey_local_machine, "Jadgekylin / JKLPOS", "POS1", A, RegtypeString, ReggetKeyValue "------------ -------------------------------------------------- -------------------------- SBuffer = Space (255) 'make a buffer lbuffersize = len (sbuffer) RTN = RegQueryValueex (HKEY, KEY, 0, REG_SZ, sBuffer, lBufferSize) 'get the value from the registry sBuffer = Trim (sBuffer) sBuffer = Left (sBuffer, Len (sBuffer) - 1)' return the value to the user If Not rtn = ERROR_SUCCESS Then 'if the Was an error writting the value = regclosekey (HKEY) SYSREGCONTROL = false 'call failed exit function else keyvalue = sbuffer end if = regclosekey (HKEY)' Close the key '------------- -------------------------------------------------- ---------------------- ---

End select '} end if' rtn = error_success

'==================================================== ==========================================================1

'Subkey is an object, key, keyvalue is a reserved word, called example:' sysregControl reghkey_local_machine, "jadgekylin / jklpos / pos", "," 0, regtyped, regreatkey '=========================================================================================================================== ============================================================================================================================================================================================================= ========================== r = regreatekey (RootKey, Subkey, HKEY) 'Create the key if not = error_success kiln = error_success dam Was created the = reglosekey (HKEY) 'close the key sysregControl = false exit function endiff

'==================================================== ================================================================================================================================================================================================================================================================= # 子Key with RegdLallKey

'The key is specified for the next level of Subkey, which is deleted. Subkey can be "" "" "" ",", " "", regTypeBinary, regDeleteKeys'SysRegControl regHKEY_LOCAL_MACHINE, "jadgekylin", "", "", regTypeBinary, regDeleteKeys'SysRegControl regHKEY_LOCAL_MACHINE, "", "jadgekylin", "", regTypeBinary, regDeleteKeys' ========= ============================================================================================================================================================================================================= ============================== r = regopenkeyex (rootkey, subskey, 0, key_write, hkey) 'Open the key if rtn = Error_suCcess Ten '= regdeletekey (HKEY, KEY)' DELETE The key else = regclosekey (HKEY) 'close the key sysregControl = false exit function endiff

'==================================================== ========================================================================================================================= # 非 非 级Sub key, temporary with regdeleteKeys' =========================================== ============================================== r = regopenkeyex RootKey, SubKey, 0, KEY_WRITE, hKey) 'open the key If rtn = ERROR_SUCCESS Then' if the key could be opened then rtn = RegDeleteKey (hKey, Key) 'delete the key Else rtn = RegCloseKey (hKey)' close the key SysregControl = false exit function end if '=========================================== =====================================

========== Case regdeletevalues' = 116 'Delete key value' 'here KeyValue, regkeytype is a reserved word, can be set to arbitrary value, call example:' sysregControl REGHKEY_LOCAL_MACHINE, "Jadgekylin / JKLPOS", " POS ", 0, regtypedword, regdeletevalues' ========================================== =================================================

rtn = RegOpenKeyEx (RootKey, SubKey, 0, KEY_WRITE, hKey) 'open the key If rtn = ERROR_SUCCESS Then rtn = RegDeleteValue (hKey, Key) Else rtn = RegCloseKey (hKey) SysRegControl = False Exit Function End If' ==== ============================================================================================================================================================================================================= ===================== ============================================================================================================================================================================================= ============================================================================================================================================================================================================= =================================== Add their own processing in here, '======= ============================================================================================================================================================================================================= =======================================================================================================================================================

CASE ELSE '=============================================== =================================================== sysregControl = false exit function end select ' end if 'RootKeyOn error GoTo 0SysRegControl = TrueExit FunctionRegOptionError:' error handling procedure is not called in this context, may be necessary to add their own processing .'If an error does accurr, and the user wants error messages displayed, then'display one of The Following Error Messages

Dim lErrorCode As LongDim GetErrorMsg As StringlErrorCode = Err () Select Case lErrorCode Case 1009, 1015 GetErrorMsg = "The Registry Database is corrupt!" Case 2, 1010 GetErrorMsg = "Bad Key Name" Case 1011 GetErrorMsg = "Can not Open Key" Case 4, 1012 getError = "can't read key" case 5 getErrorMsg = "Access to this key is denied" case 1013 getError = "can't write key" case 8, 14 getErrorMsg = "out of memory" case 87 getErrorMSG = "Invalid Parameter" Case 234 getErrorMsg = "There is more data Than the buffer HAS been allocated to hold." Case Else getError = chr (13) & chr (10) & error (Err ()) end selectmsgbox "error:" & Err () & gerrorMsgexit FunctionResumeEnd Function

The above function is one of the methods I made, and interested friends can ask me this control..

Jadgekylin01@yesky.com

转载请注明原文地址:https://www.9cbs.com/read-5405.html

New Post(0)