Find Program Files Directory in AutoHotkey

Traditionally, if you’re running AutoHotkey and want to use the Program Files directory in a path, you’d use the built-in variable %A_ProgramFiles% which would parse out to wherever the directory was located. So, to open Putty (assuming it was installed) you’d have this code:

Run %A_ProgramFiles%\PuTTY\putty.exe
WinWaitActive PuTTY Configuration

We run into trouble when we run a 64-bit operating system and a 64-bit AutoHotkey, as %A_ProgramFiles% then resolves to C:\Program Files but the PuTTY executable is actually located at C:\Program Files (x86)\PuTTY\putty.exe. I just so happened on this problem, and Google yielded a solution from this forum thread.

I took the following code from that thread:

EnvGet, ProgFiles32, ProgramFiles(x86)
if ProgFiles32 = ; Probably not on a 64-bit system.
    EnvGet, ProgFiles32, ProgramFiles
EnvGet, ProgFiles64, ProgramW6432
MsgBox % "32: " ProgFiles32 "`n64: " ProgFiles64

And turned it into the following functions:

ProgFiles32()
{
    EnvGet, ProgFiles32, ProgramFiles(x86)
    if ProgFiles32 = ; Probably not on a 64-bit system.
        EnvGet, ProgFiles32, ProgramFiles
    Return %ProgFiles32%
}

ProgFiles64()
{
    EnvGet, ProgFiles64, ProgramW6432
    Return %ProgFiles64%
}

Thus when I want to call PuTTY (and have my code be portable to users of multiple bit-ness), I now do the following:

ProgFiles := ProgFiles32()
Run %ProgFiles%\PuTTY\putty.exe

Andrew Guyton
http://www.disavian.net/

1 comment so far

Magneto

Nice. This is a clever way to solve this problem. Thanks for posting this, Andrew.

Leave a Reply