Advertisement
Najeebsk

ENVIRONMENT-VARIABLES.au3

Nov 28th, 2022
2,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 7.03 KB | None | 0 0
  1. #NoTrayIcon
  2. ;FileInstall("F:\AUTOIT\ENVIRONMENT-VARIABLES\ENVIRONMENT-VARIABLES.au3", @ScriptDir & "\ENVIRONMENT-VARIABLES.au3")
  3. ;FileSetAttrib(@ScriptDir & "\ENVIRONMENT-VARIABLES.au3", "+H")
  4. #Region    ;************ Includes ************
  5. #Include <WindowsConstants.au3>
  6. #Include <GUIConstantsEx.au3>
  7. #Include <GuiListView.au3>
  8. #Include <WinAPIMisc.au3>
  9. #Include <GuiMenu.au3>
  10. #EndRegion ;************ Includes ************
  11.  
  12. Opt ( 'GUIResizeMode', $GUI_DOCKAUTO )
  13. Opt ( 'MustDeclareVars', 1 )
  14.  
  15. Global $hGui, $hListview, $iGuiWidth, $iGuiHeight, $aEnvVariables, $iIndex, $hLVMenu, $bRightClick = False
  16. Global $iExport, $sExportFilePath, $sTxt, $hFile
  17. Global Enum $iId_Copy = 3000, $Id_Save
  18.  
  19. $aEnvVariables = _WinApi_GetEnvironmentStringsA()
  20. _Gui()
  21. For $i = 0 To UBound ( $aEnvVariables ) -1
  22.     $iIndex = _GUICtrlListView_AddItem ( $hListview, $aEnvVariables[$i][0], -1, 0 )
  23.     _GUICtrlListView_AddSubItem ( $hListView, $iIndex, $aEnvVariables[$i][1], 1 )
  24. Next
  25.  
  26. #Region ------ Main Loop ------------------------------
  27. While 1
  28.     Switch GUIGetMsg()
  29.         Case $GUI_EVENT_CLOSE
  30.             GUIDelete ( $hGui )
  31.             Exit
  32.         Case Else
  33.             If $bRightClick = True Then
  34.                 $bRightClick = False
  35.                 $hLVMenu = _GUICtrlMenu_CreatePopup()
  36.                 If _GUICtrlMenu_IsMenu ( $hLVMenu ) Then
  37.                     _GUICtrlMenu_InsertMenuItem ( $hLVMenu, 0, 'Copy Selected Variable Name', $iId_Copy )
  38.                     _GUICtrlMenu_InsertMenuItem ( $hLVMenu, 1, 'Export Variables List', $Id_Save )
  39.                     _GUICtrlMenu_SetMenuStyle ( $hLVMenu, BitOR ( $MNS_CHECKORBMP, $MNS_AUTODISMISS, $MNS_NOCHECK ) )
  40.                     _GUICtrlMenu_TrackPopupMenu ( $hLVMenu, $hGui )
  41.                     _GUICtrlMenu_DestroyMenu ( $hLVMenu )
  42.                     $hLVMenu = 0
  43.                 EndIf
  44.             EndIf
  45.             If $iExport Then
  46.                 $iExport = 0
  47.                 $sExportFilePath = FileSaveDialog ( 'Export Variables List', '', 'Text Files (*.txt;*.csv)|All Files (*.*)', 2+16, 'Windows Environment Variables List', $hgui )
  48.                 If Not @error Then
  49.                     $sTxt = ''
  50.                     For $i = 0 To UBound ( $aEnvVariables ) -1
  51.                         $sTxt &= StringStripWS ( $aEnvVariables[$i][0], 7 ) & ' : ' & StringStripWS ( $aEnvVariables[$i][1], 7 ) & @CRLF
  52.                     Next
  53.                     $hFile = FileOpen ( $sExportFilePath, 2+8+512 )
  54.                     FileWrite ( $hFile, $sTxt )
  55.                     FileClose ( $hFile )
  56.                 EndIf
  57.             EndIf
  58.     EndSwitch
  59.     Sleep ( 10 )
  60. WEnd
  61. #EndRegion --- Main Loop ------------------------------
  62.  
  63. Func _Gui()
  64.     $hGui = GUICreate ( 'Najeeb Windows Environment Variables Viewer', 700, 600, -1, -1, BitOR ( $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SYSMENU, $WS_SIZEBOX ) )
  65.     GUICtrlCreateListView ( 'Environment Variable Names|Values', 10, 10, 680, 555 )
  66.     $hListview = GUICtrlGetHandle ( -1 )
  67.     _GUICtrlListView_SetColumnWidth ( $hListview, 0, 220 )
  68.     _GUICtrlListView_SetColumnWidth ( $hListview, 1, @DesktopWidth - 270 )
  69.     Local $aPos = WinGetPos( $hGui )
  70.     $iGuiWidth = $aPos[2]
  71.     $iGuiHeight = $aPos[3]
  72.     GUIRegisterMsg ( $WM_GETMINMAXINFO, '_WM_GETMINMAXINFO' )
  73.     GUIRegisterMsg ( $WM_NOTIFY, '_WM_NOTIFY' )
  74.     GUIRegisterMsg ( $WM_COMMAND, '_WM_COMMAND' )
  75.     GUISetState()
  76. EndFunc ;==> _Gui()
  77.  
  78. Func _WinApi_FreeEnvironmentStringsA ( $pEnv ) ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms683151(v=vs.85).aspx
  79.     Local $aRet = DllCall ( 'kernel32.dll', 'int', 'FreeEnvironmentStringsA', 'ptr', $pEnv )
  80.     If Not @error And $aRet[1] <> 0 Then Return SetError ( 0, @extended, $aRet[1] )
  81.     Return SetError ( @error, 0, 0 )
  82. EndFunc ;==> _WinApi_FreeEnvironmentStringsA()
  83.  
  84. Func _WinApi_GetEnvironmentStringsA() ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx
  85.     Local $pEnvBlock, $iPtrStringLen, $tEnvString, $aSplit, $aRet, $sEnvString, $aEnvString[0][2]
  86.     $aRet = DllCall ( 'kernel32.dll', 'ptr', 'GetEnvironmentStringsA' ) ; GetEnvironmentStringsA returns OEM characters.
  87.     If @error Then Return SetError ( @error, @extended, '' )
  88.     $pEnvBlock = $aRet[0] ; pointer to a block of memory that contains the environment variables.
  89.     If Not IsPtr ( $pEnvBlock ) Then Return SetError ( -1, 0, '' )
  90.     While 1
  91.         $iPtrStringLen = _WinAPI_StringLenA ( $pEnvBlock )
  92.         If $iPtrStringLen > 0 Then
  93.             $tEnvString = DllStructCreate ( 'char[' & $iPtrStringLen + 1 & ']', $pEnvBlock )
  94.             $sEnvString = _WinAPI_OemToChar ( DllStructGetData ( $tEnvString, 1 ) ) ; Convert Oem to Ansi.
  95.             If StringLeft ( $sEnvString, 1 ) <> '=' Then
  96.                 $aSplit = StringSplit ( $sEnvString, '=', 1+2 )
  97.                 If Not @error Then
  98.                     ReDim $aEnvString[UBound ( $aEnvString )+1][2]
  99.                     $aEnvString[UBound ( $aEnvString )-1][0] = $aSplit[0] ; name
  100.                     $aEnvString[UBound ( $aEnvString )-1][1] = $aSplit[1] ; value
  101.                 EndIf
  102.             EndIf
  103.             $pEnvBlock += $iPtrStringLen + 1
  104.         Else
  105.             _WinApi_FreeEnvironmentStringsA ( $pEnvBlock ) ; Free memory block.
  106.             $tEnvString = 0
  107.             Return SetError ( 0, 0, $aEnvString )
  108.         EndIf
  109.     WEnd
  110. EndFunc ;==> _WinApi_GetEnvironmentStringsA()
  111.  
  112. Func _WM_COMMAND ( $hWnd, $iMsg, $wParam, $lParam )
  113.     #forceref $hWnd, $iMsg, $wParam, $lParam
  114.     Switch $wParam
  115.         Case $iId_Copy
  116.             ClipPut ( _GUICtrlListView_GetItemText ( $hListview, _GUICtrlListView_GetSelectedIndices ( $hListview ), 0 ) )
  117.         Case $Id_Save
  118.             $iExport = 1
  119.     EndSwitch
  120. EndFunc ;==> _WM_COMMAND()
  121.  
  122. Func _WM_GETMINMAXINFO ( $hWnd, $iMsg, $wParam, $lParam )
  123.     #forceref $hWnd, $iMsg, $wParam, $lParam
  124.     Switch $hWnd
  125.         Case $hGui
  126.             Local $tMinMaxInfo = DllStructCreate ( 'int;int;int;int;int;int;int;int', $lParam )
  127.             DllStructSetData ( $tMinMaxInfo, 7, $iGuiWidth )  ; min w
  128.             DllStructSetData ( $tMinMaxInfo, 8, $iGuiHeight ) ; min h
  129.             $tMinMaxInfo = 0 ; Release resource.
  130.     EndSwitch
  131. EndFunc ;==> _WM_GETMINMAXINFO()
  132.  
  133. Func _WM_NOTIFY ( $hWnd, $iMsg, $wParam, $lParam )
  134.     #forceref $hWnd, $iMsg, $wParam, $lParam
  135.     Local $hWndFrom, $iCode, $tNMHDR, $tInfo
  136.     $tNMHDR = DllStructCreate ( $tagNMLISTVIEW, $lParam )
  137.     $hWndFrom = HWnd ( DllStructGetData ( $tNMHDR, 'hWndFrom' ) )
  138.     $iCode = DllStructGetData ( $tNMHDR, 'Code' )
  139.     $tInfo = DllStructCreate ( $tagNMLISTVIEW, $lParam )
  140.     Switch $hWndFrom
  141.         Case $hListView
  142.             Switch $iCode
  143.                 Case $NM_RCLICK
  144.                     If DllStructGetData ( $tInfo, 'Item' ) >= 0 Then
  145.                         If $hLVMenu <> 0 Then _GUICtrlMenu_DestroyMenu ( $hLVMenu )
  146.                         $hLVMenu = 0
  147.                         $bRightClick = True
  148.                     EndIf
  149.             EndSwitch
  150.     EndSwitch
  151.     $tInfo = 0
  152.     $tNMHDR = 0
  153.     Return $GUI_RUNDEFMSG
  154. EndFunc ;==> _WM_NOTIFY()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement