'*************************************************************************'**模 块 名:ModRW_Reg'**创 建 人:嗷嗷叫的老马'**日 期:2003年11月17日'**描 述:本模块是有关注册表操作的'**版 本:V1.0'*************************************************************************'
'使用示例:'新建串值'SetStringValue "HKEY_LOCAL_MACHINE", "String Value", "Hello Visual Basic programmer"'SetStringValue "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "Internat", "internat.exe"'新建二进制值'SetBinaryValue "HKEY_LOCAL_MACHINE", "Binary Value", Chr$(&H1) + Chr$(&H2) + Chr$(&H3) + Chr$(&H4)'新建 DWORD 值'SetDWORDValue "HKEY_LOCAL_MACHINE", "DWORD Value", "1"'读取串值'GetStringValue("HKEY_LOCAL_MACHINE", "String Value")'读取二进制值'GetBinaryValue("HKEY_LOCAL_MACHINE", "Binary Value")'If rtn = Chr$(&H1) + Chr$(&H2) + Chr$(&H3) + Chr$(&H4) Then'读取 DWORD 值'GetDWORDValue("HKEY_LOCAL_MACHINE", "DWORD Value")'删除键值'DelValue("HKEY_LOCAL_MACHINE", "String Value")'新建主键'CreateKey "HKEY_LOCAL_MACHINE\Registry Editor"'删除主键'DeleteKey "HKEY_LOCAL_MACHINE\Registry Editor"Type FILETIME
lLowDateTimeAsLonglHighDateTimeAsLongEndTypePrivateDeclareFunctionRegOpenKeyEx Lib"advapi32.dll"Alias"RegOpenKeyExA"_
(ByVal hKeyAsLong, _
ByVal lpSubKeyAsString, _
ByVal ulOptionsAsLong, _
ByVal samDesiredAsLong, phkResultAsLong)AsLongPrivateDeclareFunctionRegCloseKey Lib"advapi32.dll"_
(ByVal hKeyAsLong)AsLongPrivateDeclareFunctionRegCreateKey Lib"advapi32.dll"Alias"RegCreateKeyA"_
(ByVal hKeyAsLong, _
ByVal lpSubKeyAsString, phkResultAsLong)AsLongPrivateDeclareFunctionRegDeleteKey Lib"advapi32.dll"Alias"RegDeleteKeyA"_
(ByVal hKeyAsLong, _
ByVal lpSubKeyAsString)AsLongPrivateDeclareFunctionRegQueryValueEx Lib"advapi32.dll"Alias"RegQueryValueExA"_
(ByVal hKeyAsLong, _
ByVal lpValueNameAsString, _
ByVal lpReservedAsLong, lpTypeAsLong, _
ByVal lpDataAsString, lpcbDataAsLong)AsLongPrivateDeclareFunctionRegQueryValueExA Lib"advapi32.dll"_
(ByVal hKeyAsLong, _
ByVal lpValueNameAsString, _
ByVal lpReservedAsLong, lpTypeAsLong, _
ByRef lpDataAsLong, lpcbDataAsLong)AsLongPrivateDeclareFunctionRegSetValueEx Lib"advapi32.dll"Alias"RegSetValueExA"_
(ByVal hKeyAsLong, _
ByVal lpValueNameAsString, _
ByVal ReservedAsLong, _
ByVal dwTypeAsLong, _
ByVal lpDataAsString, _
ByVal cbDataAsLong)AsLongPrivateDeclareFunctionRegSetValueExA Lib"advapi32.dll"_
(ByVal hKeyAsLong, _
ByVal lpValueNameAsString, _
ByVal ReservedAsLong, _
ByVal dwTypeAsLong, _
ByRef lpDataAsLong, _
ByVal cbDataAsLong)AsLongPrivateDeclareFunctionRegSetValueExB Lib"advapi32.dll"Alias"RegSetValueExA"_
(ByVal hKeyAsLong, _
ByVal lpValueNameAsString, _
ByVal ReservedAsLong, _
ByVal dwTypeAsLong, _
ByRef lpDataAsByte, _
ByVal cbDataAsLong)AsLongPrivateDeclareFunctionRegDeleteValue Lib"advapi32.dll"Alias"RegDeleteValueA"_
(ByVal hKeyAsLong, _
ByVal lpValueNameAsString)AsLongConstERROR_SUCCESS=0&ConstERROR_BADDB=1009&ConstERROR_BADKEY=1010&ConstERROR_CANTOPEN=1011&ConstERROR_CANTREAD=1012&ConstERROR_CANTWRITE=1013&ConstERROR_OUTOFMEMORY=14&ConstERROR_INVALID_PARAMETER=87&ConstERROR_ACCESS_DENIED=5&ConstERROR_NO_MORE_ITEMS=259&ConstERROR_MORE_DATA=234&ConstREG_NONE=0&ConstREG_SZ=1&ConstREG_EXPAND_SZ=2&ConstREG_BINARY=3&ConstREG_DWORD=4&ConstREG_DWORD_LITTLE_ENDIAN=4&ConstREG_DWORD_BIG_ENDIAN=5&ConstREG_LINK=6&ConstREG_MULTI_SZ=7&ConstREG_RESOURCE_LIST=8&ConstREG_FULL_RESOURCE_DESCRIPTOR=9&ConstREG_RESOURCE_REQUIREMENTS_LIST=10&ConstKEY_QUERY_VALUE=&H1&ConstKEY_SET_VALUE=&H2&ConstKEY_CREATE_SUB_KEY=&H4&ConstKEY_ENUMERATE_SUB_KEYS=&H8&ConstKEY_NOTIFY=&H10&ConstKEY_CREATE_LINK=&H20&ConstREAD_CONTROL=&H20000ConstWRITE_DAC=&H40000ConstWRITE_OWNER=&H80000ConstSYNCHRONIZE=&H100000ConstSTANDARD_RIGHTS_REQUIRED=&HF0000ConstSTANDARD_RIGHTS_READ=READ_CONTROLConstSTANDARD_RIGHTS_WRITE=READ_CONTROLConstSTANDARD_RIGHTS_EXECUTE=READ_CONTROLConstKEY_READ=STANDARD_RIGHTS_READOrKEY_QUERY_VALUEOrKEY_ENUMERATE_SUB_KEYSOrKEY_NOTIFYConstKEY_WRITE=STANDARD_RIGHTS_WRITEOrKEY_SET_VALUEOrKEY_CREATE_SUB_KEYConstKEY_EXECUTE=KEY_READDimhKeyAsLong, MainKeyHandleAsLongDimrtnAsLong, lBufferAsLong, sBufferAsStringDimlBufferSizeAsLongDimlDataSizeAsLongDimByteArray()AsByte'This constant determins wether or not to display error messages to the'user. I have set the default value to False as an error message can and'does become irritating after a while. Turn this value to true if you want'to debug your programming code when reading and writing to your system'registry, as any errors will be displayed in a message box.ConstDisplayErrorMsg=FalseFunctionSetDWORDValue(SubKeyAsString, EntryAsString, ValueAsLong)CallParseKey(SubKey, MainKeyHandle)IfMainKeyHandleThenrtn=RegCreateKey(MainKeyHandle, SubKey, hKey)'open the keyIfrtn=ERROR_SUCCESSThen'if the key was open successfully thenrtn=RegSetValueExA(hKey, Entry,0, REG_DWORD, Value,4)'write the valueIfNotrtn=ERROR_SUCCESSThen'if there was an error writting the valueIfDisplayErrorMsg=TrueThen'if the user want errors displayedMsgBox ErrorMsg(rtn)'display the errorEndIfEndIfrtn=RegCloseKey(hKey)'close the keyElse'if there was an error opening the keyIfDisplayErrorMsg=TrueThen'if the user want errors displayedMsgBox ErrorMsg(rtn)'display the errorEndIfEndIfEndIfEnd FunctionFunctionGetDWORDValue(SubKeyAsString, EntryAsString)CallParseKey(SubKey, MainKeyHandle)IfMainKeyHandleThenrtn=RegCreateKey(MainKeyHandle, SubKey, hKey)'open the keyIfrtn=ERROR_SUCCESSThen'if the key could be opened thenrtn=RegQueryValueExA(hKey, Entry,0, REG_DWORD, lBuffer,4)'get the value from the registryIfrtn=ERROR_SUCCESSThen'if the value could be retreived thenrtn=RegCloseKey(hKey)'close the keyGetDWORDValue=lBuffer'return the valueElse'otherwise, if the value couldnt be retreivedGetDWORDValue="Error"'return Error to the userIfDisplayErrorMsg=TrueThen'if the user wants errors displayedMsgBox ErrorMsg(rtn)'tell the user what was wrongEndIfEndIfElse'otherwise, if the key couldnt be openedGetDWORDValue="Error"'return Error to the userIfDisplayErrorMsg=TrueThen'if the user wants errors displayedMsgBox ErrorMsg(rtn)'tell the user what was wrongEndIfEndIfEndIfEnd Function