offline
JScript
#1


JScript
Administradores
Administradores
Sáb 23 Jul 2011, 20:01
UDF para criar/atualizar variáveis de ambiente do sistema operacional...

Exemplo:
Código:

; #Example# =====================================================================================================================
_EnvUpdate("VERSION", "7.07.0110.2600")
MsgBox(4096, @error, EnvGet("VERSION"))
_EnvUpdate("VERSION", "", True, True)
MsgBox(4096, @error, EnvGet("VERSION"))
; ===============================================================================================================================

Código fonte:
Código:

#include-once
; #INDEX# =======================================================================================================================
; Title .........: Environment Update
; AutoIt Version.: 3.2.12++
; Language.......: English
; Description ...: Refreshes the OS environment.
; Author ........: João Carlos (jscript)
; Support .......: trancexx, PsaltyDS, KaFu
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_EnvUpdate
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
; ===============================================================================================================================

; #VARIABLES# ===================================================================================================================
Global $MAX_VALUE_NAME = 1024
Global $HWND_BROADCAST = 0xffff
Global $WM_SETTINGCHANGE = 0x001A
Global $SMTO_ABORTIFHUNG = 0x0002
Global $SMTO_NORMAL = 0x0000
Global $MSG_TIMEOUT = 5000

; #FUNCTION# ====================================================================================================================
; Name...........: _EnvUpdate
; Description ...: Refreshes the OS environment.
; Syntax.........: _EnvUpdate( ["envvariable" [, "value" [, CurrentUser [, Machine ]]]] )
; Parameters ....: envvariable  - [optional] Name of the environment variable to set. If no variable, refreshes all variables.
;                  value        - [optional] Value to set the environment variable to. If a value is not used the environment
;                                  variable will be deleted.
;                  CurrentUser  - [optional] Sets the variable in current user environment.
;                  Machine      - [optional] Sets the variable in the machine environment.
; Return values .: Success      - None
;                  Failure      - Sets @error to 1.
; Author ........: João Carlos (jscript)
; Support .......: trancexx, PsaltyDS, KaFu
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; _EnvUpdate("TEMP", @SystemDir & "TEMP", True, True)
; ===============================================================================================================================
Func _EnvUpdate($sEnvVar = "", $vValue = "", $fCurrentUser = True, $fMachine = False)
    Local $sREG_TYPE = "REG_SZ", $iRet1, $iRet2

    If $sEnvVar <> "" Then
        If StringInStr($sEnvVar, "\") Then $sREG_TYPE = "REG_EXPAND_SZ"
        If $vValue <> "" Then
            If $fCurrentUser Then RegWrite("HKCU\Environment", $sEnvVar, $sREG_TYPE, $vValue)
            If $fMachine Then RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager\Environment", $sEnvVar, $sREG_TYPE, $vValue)
        Else
            If $fCurrentUser Then RegDelete("HKCU\Environment", $sEnvVar)
            If $fMachine Then RegDelete("HKLM\System\CurrentControlSet\Control\Session Manager\Environment", $sEnvVar)
        EndIf
        ; http://msdn.microsoft.com/en-us/library/ms686206%28VS.85%29.aspx
        $iRet1 = DllCall("Kernel32.dll", "BOOL", "SetEnvironmentVariable", "str", $sEnvVar, "str", $vValue)
        If $iRet1[0] = 0 Then Return SetError(1)
    EndIf
    ; http://msdn.microsoft.com/en-us/library/ms644952%28VS.85%29.aspx
    $iRet2 = DllCall("user32.dll", "lresult", "SendMessageTimeoutW", _
            "hwnd", $HWND_BROADCAST, _
            "dword", $WM_SETTINGCHANGE, _
            "ptr", 0, _
            "wstr", "Environment", _
            "dword", $SMTO_ABORTIFHUNG, _
            "dword", $MSG_TIMEOUT, _
            "dword_ptr*", 0)

    If $iRet2[0] = 0 Then Return SetError(1)
EndFunc  ;==>_EnvUpdate