Page 1 of 1

is exe file x64 or x86? Howto?

Posted: 01 Mar 2017, 05:41
by Ovg
Is it possible using AHK script to determine whether the file (exe or dll) is x64 or x86?
Please give me advise howto?

Re: is exe file x64 or x86? Howto?  Topic is solved

Posted: 01 Mar 2017, 05:57
by jNizM
msdn wrote:Determines whether a file is an executable (.exe) file, and if so, which subsystem runs the executable file.

Code: Select all

MsgBox % GetBinaryType("C:\Windows\System32\calc.exe")    ; -> 32BIT

GetBinaryType(Application)
{
    static Type := {0 : "32BIT", 1: "DOS", 2: "WOW", 3: "PIF", 4: "POSIX", 5: "OS216", 6: "64BIT"}
    DllCall("GetBinaryType", "str", Application, "uint*", BinaryType)
    return Type[BinaryType]
}
Ref:
GetBinaryType function (msdn)

To check if a .dll is 32 or 64-Bit you can try to load the .dll with LoadLibrary

Ref:
LoadLibrary function (msdn)

Or read the file header:
32-Bit: PE L
64-Bit: PE d†

Re: is exe file x64 or x86? Howto?

Posted: 01 Mar 2017, 06:08
by Ovg
2jNizM
It is great! Thank you so much!
Do I understand correctly - this is a Windows function call?

Re: is exe file x64 or x86? Howto?

Posted: 01 Mar 2017, 06:31
by jNizM
Yes. GetBinaryType is a Kernel32.dll function

Re: is exe file x64 or x86? Howto?

Posted: 01 Mar 2017, 15:32
by Ovg
2jNizM
Unicode version of AHK requires call of GetBinaryTypeW

BTW, Thank you again :D

Re: is exe file x64 or x86? Howto?

Posted: 01 Mar 2017, 19:22
by RickC
I'm using AHK 1.1.24.4 in Windows 7 Pro x64 and I'm a bit confused. In the code below, both of the first two supposedly 64-bit exes show as 32-bit and I don't understand why. I thought at first that it was because I have the 32-bit Unicode version of AHK installed. To test this I installed the 64-bit version of AHK 1.1.24.5 (i.e. the latest) in a new Windows 7 Pro x64 VM and ran the code.

Both MsgBox's showed 32BIT... yet Calibre isn't even installed.

So I added a 3rd MsgBox pointing at a fictitious executable. When I run the code it shows 3 MsgBox's, all showing 32BIT.

Code: Select all

MsgBox % GetBinaryType("C:\Program Files\AutoHotkey\AutoHotkeyU64.exe")    ; Shows as 32BIT
MsgBox % GetBinaryType("C:\Program Files\Calibre2\calibre.exe")    ; Shows as 32BIT, even when not installed
MsgBox % GetBinaryType("C:\This\is a\fictitious\filepath.exe")    ; Shows as 32BIT despite filepath not existing

GetBinaryType(Application)
{
    static Type := {0 : "32BIT", 1: "DOS", 2: "WOW", 3: "PIF", 4: "POSIX", 5: "OS216", 6: "64BIT"}
    DllCall("GetBinaryType", "str", Application, "uint*", BinaryType)
    return Type[BinaryType]
}

Re: is exe file x64 or x86? Howto?

Posted: 01 Mar 2017, 19:32
by RickC
Hmmm... I've just seen Ovg's last post and changed the DllCall in line 8 as follows:

Code: Select all

MsgBox % GetBinaryType("C:\Program Files\AutoHotkey\AutoHotkeyU64.exe")    ; Shows as 32BIT
MsgBox % GetBinaryType("C:\Program Files\Calibre2\calibre.exe")    ; Shows as 32BIT
MsgBox % GetBinaryType("C:\This\is a\fictitious\filepath.exe")    ; Shows as 32BIT

GetBinaryType(Application)
{
    static Type := {0 : "32BIT", 1: "DOS", 2: "WOW", 3: "PIF", 4: "POSIX", 5: "OS216", 6: "64BIT"}
    DllCall("GetBinaryTypeW", "str", Application, "uint*", BinaryType)
    return Type[BinaryType]
}
The first 2 MsgBox's now display as I would expect (hope), i.e. 64BIT... but the third still shows 32BIT, even though it's a fictitious filepath. This means that if you make a typo in the filepath and don't spot it then you'll assume that whatever you're checking is 32-bit. I don't understand why there's not an error.

Re: is exe file x64 or x86? Howto?

Posted: 01 Mar 2017, 23:33
by Ovg
2RickC

MSDN wrote:
".... If the file is not executable, or if the function fails, the return value is zero.
To get extended error information, call GetLastError."

We need to check value returned by the DLLCall
MSDN - jNizM's link

Re: is exe file x64 or x86? Howto?

Posted: 02 Mar 2017, 02:50
by jNizM
Sorry my mystake

Code: Select all

MsgBox % GetBinaryType("C:\Windows\System32\notepad.exe")    ; -> 64BIT
MsgBox % GetBinaryType("C:\Windows\SysWOW64\notepad.exe")    ; -> 32BIT
MsgBox % GetBinaryType("C:\Temp\ThisFileDoesNotExist.exe")   ; -> 0

GetBinaryType(Application)
{
    static GetBinaryType := "GetBinaryType" (A_IsUnicode ? "W" : "A")
    static Type := {0 : "32BIT", 1: "DOS", 2: "WOW", 3: "PIF", 4: "POSIX", 5: "OS216", 6: "64BIT"}
    if !(DllCall(GetBinaryType, "str", Application, "uint*", BinaryType))
        return 0
    return Type[BinaryType]
}

Re: is exe file x64 or x86? Howto?

Posted: 02 Mar 2017, 04:04
by RickC
Thank you Ovg for the explanation; thank you jNizM for the MSDN link and revised example... it was all very helpful.

Re: is exe file x64 or x86? Howto?

Posted: 02 Mar 2017, 05:53
by Ovg
2jNizM
Thank you so much for examples, links and clarifications!
I really appreciate your help!