wakillon

Windows Environment Variables Viewer

11 posts in this topic

#1 ·  Posted (edited)

There is several ways to get programmatically Windows Environment Variables :
- using "set" cmd line tool.
- using objWMIService with Win32_Environment.
- reading HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.

but also using the WinAPi GetEnvironmentStrings function.

Here is the Ansi version :

#NoTrayIcon

#Region    ;************ Includes ************
#Include <WindowsConstants.au3>
#Include <GUIConstantsEx.au3>
#Include <GuiListView.au3>
#Include <WinAPIMisc.au3>
#Include <GuiMenu.au3>
#EndRegion ;************ Includes ************

Opt ( 'GUIResizeMode', $GUI_DOCKAUTO )
Opt ( 'MustDeclareVars', 1 )

Global $hGui, $hListview, $iGuiWidth, $iGuiHeight, $aEnvVariables, $iIndex, $hLVMenu, $bRightClick = False
Global $iExport, $sExportFilePath, $sTxt, $hFile
Global Enum $iId_Copy = 3000, $Id_Save

$aEnvVariables = _WinApi_GetEnvironmentStringsA()
_Gui()
For $i = 0 To UBound ( $aEnvVariables ) -1
    $iIndex = _GUICtrlListView_AddItem ( $hListview, $aEnvVariables[$i][0], -1, 0 )
    _GUICtrlListView_AddSubItem ( $hListView, $iIndex, $aEnvVariables[$i][1], 1 )
Next

#Region ------ Main Loop ------------------------------
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete ( $hGui )
            Exit
        Case Else
            If $bRightClick = True Then
                $bRightClick = False
                $hLVMenu = _GUICtrlMenu_CreatePopup()
                If _GUICtrlMenu_IsMenu ( $hLVMenu ) Then
                    _GUICtrlMenu_InsertMenuItem ( $hLVMenu, 0, 'Copy Selected Variable Name', $iId_Copy )
                    _GUICtrlMenu_InsertMenuItem ( $hLVMenu, 1, 'Export Variables List', $Id_Save )
                    _GUICtrlMenu_SetMenuStyle ( $hLVMenu, BitOR ( $MNS_CHECKORBMP, $MNS_AUTODISMISS, $MNS_NOCHECK ) )
                    _GUICtrlMenu_TrackPopupMenu ( $hLVMenu, $hGui )
                    _GUICtrlMenu_DestroyMenu ( $hLVMenu )
                    $hLVMenu = 0
                EndIf
            EndIf
            If $iExport Then
                $iExport = 0
                $sExportFilePath = FileSaveDialog ( 'Export Variables List', '', 'Text Files (*.txt;*.csv)|All Files (*.*)', 2+16, 'Windows Environment Variables List', $hgui )
                If Not @error Then
                    $sTxt = ''
                    For $i = 0 To UBound ( $aEnvVariables ) -1
                        $sTxt &= StringStripWS ( $aEnvVariables[$i][0], 7 ) & ' : ' & StringStripWS ( $aEnvVariables[$i][1], 7 ) & @CRLF
                    Next
                    $hFile = FileOpen ( $sExportFilePath, 2+8+512 )
                    FileWrite ( $hFile, $sTxt )
                    FileClose ( $hFile )
                EndIf
            EndIf
    EndSwitch
    Sleep ( 10 )
WEnd
#EndRegion --- Main Loop ------------------------------

Func _Gui()
    $hGui = GUICreate ( 'Windows Environment Variables Viewer', 700, 600, -1, -1, BitOR ( $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SYSMENU, $WS_SIZEBOX ) )
    GUICtrlCreateListView ( 'Environment Variable Names|Values', 10, 10, 680, 555 )
    $hListview = GUICtrlGetHandle ( -1 )
    _GUICtrlListView_SetColumnWidth ( $hListview, 0, 220 )
    _GUICtrlListView_SetColumnWidth ( $hListview, 1, @DesktopWidth - 270 )
    Local $aPos = WinGetPos( $hGui )
    $iGuiWidth = $aPos[2]
    $iGuiHeight = $aPos[3]
    GUIRegisterMsg ( $WM_GETMINMAXINFO, '_WM_GETMINMAXINFO' )
    GUIRegisterMsg ( $WM_NOTIFY, '_WM_NOTIFY' )
    GUIRegisterMsg ( $WM_COMMAND, '_WM_COMMAND' )
    GUISetState()
EndFunc ;==> _Gui()

Func _WinApi_FreeEnvironmentStringsA ( $pEnv ) ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms683151(v=vs.85).aspx
    Local $aRet = DllCall ( 'kernel32.dll', 'int', 'FreeEnvironmentStringsA', 'ptr', $pEnv )
    If Not @error And $aRet[1] <> 0 Then Return SetError ( 0, @extended, $aRet[1] )
    Return SetError ( @error, 0, 0 )
EndFunc ;==> _WinApi_FreeEnvironmentStringsA()

Func _WinApi_GetEnvironmentStringsA() ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx
    Local $pEnvBlock, $iPtrStringLen, $tEnvString, $aSplit, $aRet, $sEnvString, $aEnvString[0][2]
    $aRet = DllCall ( 'kernel32.dll', 'ptr', 'GetEnvironmentStringsA' ) ; GetEnvironmentStringsA returns OEM characters.
    If @error Then Return SetError ( @error, @extended, '' )
    $pEnvBlock = $aRet[0] ; pointer to a block of memory that contains the environment variables.
    If Not IsPtr ( $pEnvBlock ) Then Return SetError ( -1, 0, '' )
    While 1
        $iPtrStringLen = _WinAPI_StringLenA ( $pEnvBlock )
        If $iPtrStringLen > 0 Then
            $tEnvString = DllStructCreate ( 'char[' & $iPtrStringLen + 1 & ']', $pEnvBlock )
            $sEnvString = _WinAPI_OemToChar ( DllStructGetData ( $tEnvString, 1 ) ) ; Convert Oem to Ansi.
            If StringLeft ( $sEnvString, 1 ) <> '=' Then
                $aSplit = StringSplit ( $sEnvString, '=', 1+2 )
                If Not @error Then
                    ReDim $aEnvString[UBound ( $aEnvString )+1][2]
                    $aEnvString[UBound ( $aEnvString )-1][0] = $aSplit[0] ; name
                    $aEnvString[UBound ( $aEnvString )-1][1] = $aSplit[1] ; value
                EndIf
            EndIf
            $pEnvBlock += $iPtrStringLen + 1
        Else
            _WinApi_FreeEnvironmentStringsA ( $pEnvBlock ) ; Free memory block.
            $tEnvString = 0
            Return SetError ( 0, 0, $aEnvString )
        EndIf
    WEnd
EndFunc ;==> _WinApi_GetEnvironmentStringsA()

Func _WM_COMMAND ( $hWnd, $iMsg, $wParam, $lParam )
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Switch $wParam
        Case $iId_Copy
            ClipPut ( _GUICtrlListView_GetItemText ( $hListview, _GUICtrlListView_GetSelectedIndices ( $hListview ), 0 ) )
        Case $Id_Save
            $iExport = 1
    EndSwitch
EndFunc ;==> _WM_COMMAND()

Func _WM_GETMINMAXINFO ( $hWnd, $iMsg, $wParam, $lParam )
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Switch $hWnd
        Case $hGui
            Local $tMinMaxInfo = DllStructCreate ( 'int;int;int;int;int;int;int;int', $lParam )
            DllStructSetData ( $tMinMaxInfo, 7, $iGuiWidth )  ; min w
            DllStructSetData ( $tMinMaxInfo, 8, $iGuiHeight ) ; min h
            $tMinMaxInfo = 0 ; Release resource.
    EndSwitch
EndFunc ;==> _WM_GETMINMAXINFO()

Func _WM_NOTIFY ( $hWnd, $iMsg, $wParam, $lParam )
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Local $hWndFrom, $iCode, $tNMHDR, $tInfo
    $tNMHDR = DllStructCreate ( $tagNMLISTVIEW, $lParam )
    $hWndFrom = HWnd ( DllStructGetData ( $tNMHDR, 'hWndFrom' ) )
    $iCode = DllStructGetData ( $tNMHDR, 'Code' )
    $tInfo = DllStructCreate ( $tagNMLISTVIEW, $lParam )
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_RCLICK
                    If DllStructGetData ( $tInfo, 'Item' ) >= 0 Then
                        If $hLVMenu <> 0 Then _GUICtrlMenu_DestroyMenu ( $hLVMenu )
                        $hLVMenu = 0
                        $bRightClick = True
                    EndIf
            EndSwitch
    EndSwitch
    $tInfo = 0
    $tNMHDR = 0
    Return $GUI_RUNDEFMSG
EndFunc ;==> _WM_NOTIFY()

Tested under Win XP SP3x86 and Win 8.1x64

Edited by wakillon
3 people like this

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites



Good stuff, wakillon. Works under W10/64bit too.

Thanks.


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

It would be good to have an option to copy the environment variable to clipboard. Nice to see how many entries have been found...


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

It would be good to have an option to copy the environment variable to clipboard. Nice to see how many entries have been found...

You're right.

Script updated.

Thanks.

1 person likes this

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Ok Thanks


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

You're right.

Script updated.

Thanks.

Great stuff! Thank you for sharing. Also, maybe, an option to export the entire list to a flat text file or csv file?

Share this post


Link to post
Share on other sites

Great stuff! Thank you for sharing. Also, maybe, an option to export the entire list to a flat text file or csv file?

You are right too.

Script Updated.

A contextual menu entry was added for save Windows Environment Variables List to a file.

Thanks.

1 person likes this

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Thank you wakillon!

Share this post


Link to post
Share on other sites

Thank you wakillon!

You're welcome.

Thanks.


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • AppSoftwareVld
      By AppSoftwareVld
      Hello,
       
      I need this for a project and I don't find a method for the next problem.
      I want when I click a button to create a variable ("$variable1") so if I press one more time to create one more ("$variable2") and create more and more how many times you press the button.
       
      Do you have an idea ? 
      Thank you for your attention !
    • Jefrey
      By Jefrey
      I've ported these two functions from PHP to AU3 to work with URLs.
      Made them for those who work with libraries like HTTP.au3 (not the one I coded), that needs passing the server domain, path, etc., instead of the full URL.
      Grab the lib here.
      ParseURL( $sURL )
      Parses the URL and splits it into defined parts. Returns an array:
      [0] = Full URL (same as $sURL) [1] = Protocol (i.e.: http, https, ftp, ws...) [2] = Domain [3] = Port (or null if not specified) [4] = Path (or null if not specified) [5] = Query string (everything after the ? - or null if not specified) Example:
      $aExample = ParseURL("https://google.com:8080/?name=doe") MsgBox(0, "Test", "URL: " & $aExample[0] & @CRLF & _ "Protocol: " & $aExample[1] & @CRLF & _ "Domain: " & $aExample[2] & @CRLF & _ "Port: " & $aExample[3] & @CRLF & _ "Path: " & $aExample[4] & @CRLF & _ "Query string: " & $aExample[5])  
      ParseStr( $sStr )
      Parses a query string (similar to the [5] of the previous function) and returns a multidimensional array, where:
      [0][0] = number of variables found [0][1] = ununsed [1][0] = key name of the first variable [1][1] = first variable value (already URL decoded) [n][0] = key name of the nth variable [n][1] = nth variable value (already URL decoded) Example:
      include <Array.au3> ; need only to do _ArrayDisplay, not needed by the lib _ArrayDisplay(ParseStr("foo=bar&test=lol%20123")) #cs Result is: [0][0] = 2 [0][1] = ununsed [1][0] = foo [1][1] = bar [2][0] = test [2][1] = lol 123 #ce Feel free to fork!
    • CurtimusPrime
      By CurtimusPrime
      #include <Date.au3> #include <TrayConstants.au3> HotKeySet("^d", "WhatIsToday") Global $Today = _Date_Time_GetSystemTime While 1 Sleep(100) WEnd Func WhatIsToday() TrayTip("Today's date", "Today is... " & $Today, 8) Sleep(8000) TrayTip("", "", 0) EndFunc ;==>WhatIsToday all it outputs into tray bubble is:  Today is...
      without getting the system time as text
    • TheDcoder
      By TheDcoder
      I just thought of this... Do Constants and Variables have any differences other than Constants cannot be changed? I know that Python does not have such things like Constants... But is there a difference between them? any advantages when using Constants whenever we can?
      The uses of Constants which I have discovered so far are:
      Coding practices (You know what things are not supposed to be changed while the program is running). Preventing code from modifying their value. So, is there anything more than what meets the eye? I think the developers of AutoIt can answer this question.
    • Reizvoller
      By Reizvoller
      Hello!
      So I have a little script here 
      Func getThe () Local $nearPix = 0123 Local $winPos = WinGetPos ($workWin) FFSaveBMP ("yBarPosArea", "True" , 600, 239, 600, 555, 30, $workWin) $nearPix = FFNearestPixel (600, 220, "0xC1C1C1" , False, $workWin) If $nearPix == 0123 Then ConsoleWrite ("No val for NearPix") ElseIf $nearPix <> 0123 Then ConsoleWrite ("Val is there") ConsoleWrite ($nearPix[0]&","&$nearPix[1]&@CR) EndIf EndFunc And when it comes time to run it I get a return of 
      When it says "non-accessible variable" , what is it meaning by that? The scope is "Local" and it is within the same function. The variable was even referenced in the step right before the one throwing the error. Any insight would be thoroughly appreciated. Thank you!