Read and write registry function source in VB

zhaozj2021-02-11  182

There is garbled in the following address stickers.

http://www.9cbs.net/develop/Article/8562.shtm

Now added as follows:

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 boolean '*************************************************** ************************************************************* The function 'function : 'registry key that is specified key is operated' 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 Sub Keys Path 'Key Settings' KeyValue Set the key value 'regkeytype Specify key value' regkeytype Description: '{' regtypebinary = & h00000001 'binary' regtypedword = & h000 00002 'DWORD' REGTYPESTRING = & H00000003 'String'} 'ID Function 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' regdLallKey = 115 'Delete non-final substem key' regdeletevalues ​​= 116 'Delete key value' regother = 120 '

Reserved Operation ID '}' Return Value: 'True Operation Success' Faled '(C) 2001.3.2' ********************************** *********************************************************** ************** DIM I as Longon Error Goto RegoptionError'iF rootkey tellect case ID '=============================================================================================================================================================================================================================================== ============================================================================================================================================================================================================= ========================================================================= ================ ============================================================================================================================================================================================================= ================ r = regopenkeyex (rootkey, subskey, 0, key_write, hkey) if = error_success dam {

Select Case RegkeyType '---------------------------------------------- ------------------------------------------ Case Regtypebinary '= & H00000001' binary

'This mode The parameter keyValue must be incorporated in a string, calling an instance:' sysregControl Reghkey_local_machine, "jadgekylin / jklpos", "pos", "jadgekylin@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' ==== ============================================================================================================================================================================================================= ===================== ============================================================================================================================================================================================= ============================================================================================================================================================================================================= ==================================================== ============================================================================================================================================================================================================= ========================= Case else '==

============================================================================================================================================================================================================= ================================================Nive Eden Goto 0SysRegcontrol = truexit functionRegoptionerror: 'Error handle process' if an error does accurr, and the user wants error message error, 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 FunctionResume

END FUNCTION

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

New Post(0)