Jump to content
wakillon

Windows Environment Variables Viewer

Recommended Posts

wakillon

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
  • Like 3

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
wakillon

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
UEZ

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
wakillon

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.

  • Like 1

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
wakillon

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
coffeeturtle

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
wakillon

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.

  • Like 1

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
coffeeturtle

Thank you wakillon!

Share this post


Link to post
Share on other sites
wakillon

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

    • Pricehacker
      By Pricehacker
      Hello!
      Im wondering if it is possible to 'empty' the variable value to save memory, for example i often use variable as a onetime use thing and would prefer to 'forget' it after is is used
      Maybe it is just as easy as to setting $vVar = Null, but i wanted to make sure that this is the case
    • CarlD
      By CarlD
      Is there a reliable way to ensure that data assigned to variables in a script is overwritten or deleted when the script exits? I have scripts that encrypt/decrypt data and would like to ensure, if possible, that the encryption keys and decrypted data do not stay in memory after the script exits. Thanks.
    • Skeletor
      By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
    • ShakeelAhmad
      By ShakeelAhmad
      I've to upload different number of photos online in different assignments. There is a problem that all photos cannot be selected at once to upload. I have to choose and upload photos one by one. For this purpose I have made a script to automate whatever I have to do manually for choosing and uploading photos one by one.
      Please see my script below and check the last "MouseClick" command. This command clicks the button to choose and upload next photo. The problem I'm facing is; the last "MouseClick" works 1 step extra when all photos have been selected and uploaded. I mean if 7 photos are to be uploaded, this command opens the box from where next photo is selected and uploaded then it again opens the box and next photo is choosen and so on.... when last photo is selected and uploaded, this button once again opens the box. When all photos are uploaded, it should not click the button to select next photo. Please suggest how can I resolve this issue.
      #include <AutoItConstants.au3> Sleep(200) HotKeySet("{ESC}","Quit") ;Press ESC key to quit Send("{ALT DOWN}") Send("{TAB}") Send("{ALT UP}") Sleep(200) Local $photos = InputBox("Question", "How many photos to upload?", "#", "", _ - 1, -1, 0, 0) ; How many photos to upload Local $selector = 0 While $photos <> $selector MouseClick("Left", 281, 238, 1) ; mouse click on very first photo in the box. if $selector = 0 Then Send("{ENTER}") ; for selecting very first photo from "open" window. Sleep(800) Else sleep(200) Send("{RIGHT " & $selector & "}") ; for selecting 2nd to onward photos from "open" window. sleep(1000) Send("{ENTER}") sleep(1000) EndIf MouseClick("Left", 495, 198, 1) ; for clicking a button to choose next photo to upload $selector = $selector + 1 WEnd Beep(1500, 300) ; beep when all photos uploaded Exit Regards,
       
      Shakeel
    • Gringo
      By Gringo
      Hi,
      I'm trying to create a shortcut playing with variables but I can't figure out what's wrong
      I got 2 variables joined in one with for instance
      Local $path = FileOpenDialog($message, "C:" & "", "Select your executable (*.exe)", 1 + 4)
      Local $elev = 'c:\windows\System32\cmd.exe /c start "runhigh" /high '
      $target = $elev & $path
      FileCreateShortcut($target, @DesktopCommonDir & "\linked.lnk", StringLeft($target,StringInStr($target,"\",0,-1)) , "" , "" , "c:\i.ico")
      The problem is that a shortcut is created but instead of the target area I got the start in filled with my variable
      My second problem is that when I do a shell execute of the result of $target = $elev & $path, Note that if I do a batch with the variable written manually, it's working.
×