Inevitably encounter the case of writing a registry, wrote a practical point of the RegWrite function. In order to reduce the amount of code, Wscript.Shell replaced the API to achieve.
The way it's used is in the comments, and it's not too much to explain. PS: Comments are much richer than implementation code, m (-.-) m
Code:
Private FunctionRegWrite (ByValName_ as String,OptionalValue_ as String,OptionalType_ as String="REG_SZ") as Integer'@Author: Lichmama'@Whatfor: Offer a simply -to-write registry'@Arguments:'name_, String'Registry key/value Path, eg. Hkey_current_user\environment (\?)'Value_, String, Optional'Registry Value Name'Type_, String, Optional and default with "REG_SZ"'Registry Data Type, eg. REG_SZ, REG_BINARY, ...'@Usage:'Create new subitem:'Call RegWrite ("hkey_current_user\enviroment\newsubitem\")'Create new subitem and set the default value:'Call RegWrite ("hkey_current_user\enviroment\newsubitem\", "Default-value-data")'Create new value:'Call RegWrite ("Hkey_current_user\enviroment\newsubitem\newvalue", "New-value-data", "REG_EXPAND_SZ")'Overwrite value:'Call RegWrite ("Hkey_current_user\enviroment\newsubitem\newvalue", "Overwrite-value-data", "REG_SZ")'@Return: Integer, zero means successful, and Non-zero means failed on Error GoToError_Handler:DimObjshell as Object SetObjshell =CreateObject("Wscript.Shell") IfIsMissing (Value_) Then PagerObjshell. RegWrite (name_)Else PagerObjshell. RegWrite (Name_, Value_, Type_)End IfError_handler:regwrite=Err.NumberSetObjshell = NothingEnd Function
Use the above method to write the registry to modify the environment variables to take effect immediately:
Private Declare FunctionSendMessageTimeoutLib "User32" Alias "Sendmessagetimeouta" _ (ByValhwnd as Long, _ ByValMsg as Long, _ ByValWParam as Long, _ ByRefLParam asAny , _ByValFuflags as Long, _ ByValUtimeout as Long, _ ByRefLpdwresult as Long) as LongPrivate ConstHwnd_broadcast = &HFFFF&Private ConstWm_wininichange = &h1aPrivate ConstWm_settingchange =Wm_wininichangePrivate ConstSmto_normal = &H0Private Subupdatesystemenvironment ()'@http://www.bczlw.com/article/faq/bianchengyuyan/vc-mfc/2007-3-5/2007030523033000.html PagerSendMessageTimeout (Hwnd_broadcast, Wm_settingchange,0&,ByVal "Environment", Smto_normal, +&,0&)End Sub
VB6 Write the Registration form