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

Get help with using AutoHotkey and its commands and hotkeys
Ovg
Posts: 23
Joined: 19 Feb 2017, 01:13

is exe file x64 or x86? Howto?

01 Mar 2017, 05:41

Is it possible using AHK script to determine whether the file (exe or dll) is x64 or x86?
Please give me advise howto?
It's impossible to lead us astray for we don't care even to choose the way.
User avatar
jNizM
Posts: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

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

01 Mar 2017, 05:57

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†
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Ovg
Posts: 23
Joined: 19 Feb 2017, 01:13

Re: is exe file x64 or x86? Howto?

01 Mar 2017, 06:08

2jNizM
It is great! Thank you so much!
Do I understand correctly - this is a Windows function call?
It's impossible to lead us astray for we don't care even to choose the way.
User avatar
jNizM
Posts: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: is exe file x64 or x86? Howto?

01 Mar 2017, 06:31

Yes. GetBinaryType is a Kernel32.dll function
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Ovg
Posts: 23
Joined: 19 Feb 2017, 01:13

Re: is exe file x64 or x86? Howto?

01 Mar 2017, 15:32

2jNizM
Unicode version of AHK requires call of GetBinaryTypeW

BTW, Thank you again :D
It's impossible to lead us astray for we don't care even to choose the way.
RickC
Posts: 249
Joined: 27 Oct 2013, 08:32

Re: is exe file x64 or x86? Howto?

01 Mar 2017, 19:22

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]
}
RickC
Posts: 249
Joined: 27 Oct 2013, 08:32

Re: is exe file x64 or x86? Howto?

01 Mar 2017, 19:32

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.
Ovg
Posts: 23
Joined: 19 Feb 2017, 01:13

Re: is exe file x64 or x86? Howto?

01 Mar 2017, 23:33

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
It's impossible to lead us astray for we don't care even to choose the way.
User avatar
jNizM
Posts: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: is exe file x64 or x86? Howto?

02 Mar 2017, 02:50

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]
}
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
RickC
Posts: 249
Joined: 27 Oct 2013, 08:32

Re: is exe file x64 or x86? Howto?

02 Mar 2017, 04:04

Thank you Ovg for the explanation; thank you jNizM for the MSDN link and revised example... it was all very helpful.
Ovg
Posts: 23
Joined: 19 Feb 2017, 01:13

Re: is exe file x64 or x86? Howto?

02 Mar 2017, 05:53

2jNizM
Thank you so much for examples, links and clarifications!
I really appreciate your help!
It's impossible to lead us astray for we don't care even to choose the way.

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 36 guests