nerdfever.com
nerdfever.com

Reputation: 1782

Windows x64 vs x86: Hardware vs. OS vs. process

I'm having trouble with things breaking based on x86 vs x64 in Python 3 on Windows.

I need to know if my Python program is running:

They are not the same thing (at all!).

AMD64 architecture processors can run either 64 or 32 bit operating systems.

And 64 bit operating systems can run either 64 or 32 bit processes.

I know that:

To forestall the inevitable "why do you care?" questions, it's because my Python program is automating configuration of Windows - things are in different places on x86 vs x64 Windows, but I don't know in advance if my program will be running on 32 or 64 bit Python.

So I need to figure that out.

Upvotes: 1

Views: 496

Answers (2)

nerdfever.com
nerdfever.com

Reputation: 1782

I believe this will work, but I haven't tested it on a 32-bit version of Windows:

import sys, os
x64_process = (sys.maxsize > 2**32)
x64_os = os.environ.get('ProgramW6432') is not None

And probably my most important use case - restarting explorer.exe after registry changes:

def restartExplorer():
    '''Restart explorer'''
    do(r'taskkill /f /im explorer.exe')
    if x64_os and not x64_process:
        do(os.environ['systemroot']+ r'\sysnative\cmd.exe /c start /B explorer.exe') # because this Python is in a 32 bit process
    else:
        do("start explorer.exe")

I won't give you the implementation of do() because it's pretty obvious. (But will if somebody asks.)

Upvotes: 0

AKX
AKX

Reputation: 168967

So your actual question is whether the Windows you're running on is x64? :)

Riffing off this and this, how about

import os
arch = (
    os.environ.get("PROCESSOR_ARCHITEW6432") or 
    os.environ.get("PROCESSOR_ARCHITECTURE")
)
# `arch` should now reliably be `x64` if the system is 64-bit.

Upvotes: 1

Related Questions