Jump to content

Enviroment Variable %ProgramFiles% - is this a bug in AutoIt?


Recommended Posts

Hi,

My OS is Win7 Pro. x64. This being the case there are two paths to where programs get installed to:

1) Program Files

2) Program Files (x86)

As part of a script that I am writing I decided to try and access the paths to these folders using the EvnGet instruction. I also got the names of the Environment Variables from Wikipedia: https://en.wikipedia.org/wiki/Environment_variable#cite_note-Schulz_2014_Ordner-7

There it says that using the environment variable %ProgramFiles% should return the path to 'Program Files'. It also says that using the environment variable %ProgramFiles(x86)% should return the path to 'Program Files (x86)'. However, when I try this in a script it doesn't work - both return the path to 'Program Files (x86)'.

$sEnvVar = EnvGet("ProgramFiles")
MsgBox(0, "Env. Variable - Program Files", $sEnvVar)

$sEnvVar = EnvGet("ProgramFiles(x86)")
MsgBox(0, "Env. Variable - Program Files (x86)", $sEnvVar)

 

Is this a bug in AutoIt or am I doing something wrong?

 

 

Edited by Radish

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

A script run as a 32 bit process can't access the environment variable for x64 programs. So if you run it as a 32 bit process, both will return "C:\Program Files (x86)" instead of "C:\Program Files" and "C:\Program Files (x86)"

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Well, in your System Properties (rightclick computer -> properties), under Advanced System Settings (don't remember if that's the same in W7), in tab 'advanced', there should be a button called "Environment Variables".

Alternatively, run this in a command prompt (which I find easier):

C:\Users\me> set | findstr /B "ProgramFiles"
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Oh, had a quick look on Wikipedia and found out how to check the enviroment variables from a CMD window. It correctly reports the following for these variables (thus):

ProgramFiles=C:\Program Files

ProgramFiles(x86)=C:\Program Files (x86)

Which I suppose means that the system sees them as okay.

Edited by Radish

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

BrewManNH

A script run as a 32 bit process can't access the environment variable for x64 programs. So if you run it as a 32 bit process, both will return "C:\Program Files (x86)" instead of "C:\Program Files" and "C:\Program Files (x86)"

 It seems you are correct. This from Wikipedia:

The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).

So it seems that I'm using AutoIt 32-bit (sort of thing). How do I force it to be 64-bit? Or is there some other way round this problem?

 

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

Then you are probably running it in x86 mode. Are you running from SciTE or from a compiled script? If you are running from a compiled script, check the compiler settings in the Compile window, and/or use the correct compiler from the commandline (aut2exe_x64.exe).

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks SadBunny. I compiled it as x64 and it works fine. Is there anything I need to do as far SciTE is concerned to get the script to run okay in there? (Sorry if my questions seem stupid but I really am new to all this.)

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

Works fine for me in Scite without any further changes. I can make it stop working by adding #AutoIt3Wrapper_UseX64=n, which forces the script to run in 32-bit mode. But it should default to X64 mode automatically if you're running on a 64-bits OS. Have you already tried to run it from SciTE?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Yes, SadBunny, I have tried running it in SciTE and in there it doesn't work (my original complaint/query).  However, given what you are saying I'm thinking that maybe I did something wrong, or made a wrong choice when I installed AutoIt. During the install routine there is a question if the user wants to use the x86 or x64 version. There is a warning issued with this that if you select the x64 version then your scripts might not work as expected. On that basis I chose to install the x86 version. So, maybe that is why I'm experiencing this difficulty.

Should I have installed the x64 version? Really what is the difference between these two versions?

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

Oh, I just tried running it again in SciTI and it ran fine. Then I looked at the top of the window and see that SciTI added a bit of code at the start of the script - this appeared after I compiled the script to x64.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****


Going by this I think I might have made a mistake in the way I installed autoIt, given that I'm on x64 operating system.

 

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

an attempt for a workaround
it works even from x86 SciTE

#include <AutoItConstants.au3>

$sEnvVar = _EnvGet("ProgramFiles")
MsgBox(0, "Env. Variable - Program Files", $sEnvVar)

$sEnvVar = _EnvGet("ProgramFiles(x86)")
MsgBox(0, "Env. Variable - Program Files (x86)", $sEnvVar)

Func _EnvGet($sEnvVar)
    Local $Out = ""
    DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
    $pid = Run(@ComSpec & ' /c echo %' & $sEnvVar & '%', "", @SW_HIDE, $STDOUT_CHILD)
    DllCall("kernel32.dll", "int", "Wow64EnableWow64FsRedirection", "int", 1)
    Do
        $Out &= StdoutRead($pid)
    Until @error
    Return $Out
EndFunc   ;==>_EnvGet

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I don't know what the differences are between those installation modes. Maybe it just sets config values, maybe it omits a couple of libraries/binaries? Someone else will probably know more.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Wow! Thanks Chimp, it does work as you say. For a beginner like me it does seem complex though, you know your stuff. I'll have to study the code to get it to make some kind of sense to me but I'm sure some it will lodge in the back of my mind and will surface again when needed.

Thanks SadBunny. I think I'll start another thread asking that question when I get some more time.

Thanks to all who replied.

Operating System: Windows 7 Pro. x64 SP1

Link to comment
Share on other sites

Something else, maybe useful :

MsgBox(0, "", EnvGet("ProgramW6432") )
; or
MsgBox (0, "",  RegRead("HKEY_LOCAL_MACHINE64\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir") )
Edited by jguinch
Link to comment
Share on other sites


Wow64DisableWow64FsRedirection is in the AutoIt includes, so no need for low-level DllCalls.
 

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites


Wow64DisableWow64FsRedirection is in the AutoIt includes, so no need for low-level DllCalls.
 

​thanks for the hint

#include <WinAPIFiles.au3>

$sEnvVar = _EnvGet("ProgramFiles")
MsgBox(0, "Env. Variable - Program Files", $sEnvVar)

$sEnvVar = _EnvGet("ProgramFiles(x86)")
MsgBox(0, "Env. Variable - Program Files (x86)", $sEnvVar)

Func _EnvGet($sEnvVar)
    Local $Out = ""
    _WinAPI_Wow64EnableWow64FsRedirection(False)
    $pid = Run(@ComSpec & ' /c echo %' & $sEnvVar & '%', "", @SW_HIDE, $STDOUT_CHILD)
    _WinAPI_Wow64EnableWow64FsRedirection(True)
    Do
        $Out &= StdoutRead($pid)
    Until @error
    Return $Out
EndFunc   ;==>_EnvGet

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...