Ctypes python 64 bit

Detect 64bit OS (windows) in Python

>>> import ctypes, sys >>> i = ctypes.c_int() >>> kernel32 = ctypes.windll.kernel32 >>> process = kernel32.GetCurrentProcess() >>> kernel32.IsWow64Process(process, ctypes.byref(i)) gossamer-threads.com/lists/python/python/663523

When reading through the answers, be warned: some of them return the version (32/64bit) of installed Python, some the version of the processor architecture, and only some actually return the version (32/64bit) of the OS. Which is what the question asks for. Many of the answers confuse these.

23 Answers 23

I think the best solution to the problem has been posted by Mark Ribau.

The best answer to the question for Python 2.7 and newer is:

def is_os_64bit(): return platform.machine().endswith('64') 

On windows the cross-platform-function platform.machine() internally uses the environmental variables used in Matthew Scoutens answer.

I found the following values:

  • WinXP-32: x86
  • Vista-32: x86
  • Win7-64: AMD64
  • Debian-32: i686
  • Debian-64: x86_64
def is_windows_64bit(): if 'PROCESSOR_ARCHITEW6432' in os.environ: return True return os.environ['PROCESSOR_ARCHITECTURE'].endswith('64') 

To find the Python interpreter bit version I use:

def is_python_64bit(): return (struct.calcsize("P") == 8) 

This is the right answer. It is the only one that works also if you are using Python 32bit on 64bit Windows installation. +1

I don’t think this is always safe either: You could have a 32-bit OS running on 64-bit processor architecture.

Yes! The underlaying hardware has no impact. The functions have been tested with EM64T systems on all OSs listed by me.

I guess you should look in os.environ[‘PROGRAMFILES’] for the program files folder.

+1 for solving the problem instead of answering the question — not always a good thing, but in this case it is.

This is the right solution, rather than hardcoding a directory. However, this will not lead to the 32-bit program files on 64-bit Windows, if that’s what is needed.

Yes, it will lead to the correct Program Files on Win64. Here are my values on Win7 64: ProgramFiles=C:\Program Files (x86) ProgramFiles(x86)=C:\Program Files (x86) ProgramW6432=C:\Program Files

FYI: It will lead to the x86 version if you are running 32 bit python, but the x64 version if running 64 bit python.

@MikeGraham it is not the right solution because whether your program (i.e. in our case the Python interpreter) runs as WOW64 or native 64-bit program governs whether the WOW64 ( %SystemRoot%\SysWOW64\cmd.exe ) or 64-bit cmd.exe ( %SystemRoot%\System32\cmd.exe ) gets to run on a 64-bit Windows and thereby makes the output of this value relative to the environment.

platform module — Access to underlying platform’s identifying data

>>> import platform >>> platform.architecture() ('32bit', 'WindowsPE') 

On 64-bit Windows, 32-bit Python returns:

And that means that this answer, even though it has been accepted, is incorrect. Please see some of the answers below for options that may work for different situations.

This won’t work, because python chose to always return win32 for compatibility. This is also why there are only ‘hackish’ way to find out.

Came here searching for properly detecting if running on 64bit windows, compiling all the above into something more concise.

Below you will find a function to test if running on 64bit windows, a function to get the 32bit Program Files folder, and a function to get the 64bit Program Files folder; all regardless of running 32bit or 64bit python. When running 32bit python, most things report as if 32bit when running on 64bit, even os.environ[‘PROGRAMFILES’] .

import os def Is64Windows(): return 'PROGRAMFILES(X86)' in os.environ def GetProgramFiles32(): if Is64Windows(): return os.environ['PROGRAMFILES(X86)'] else: return os.environ['PROGRAMFILES'] def GetProgramFiles64(): if Is64Windows(): return os.environ['PROGRAMW6432'] else: return None 

Note: Yes, this is a bit hackish. All other methods that «should just work», do not work when running 32bit Python on 64bit Windows (at least for the various 2.x and 3.x versions I have tried).

Edits:
2011-09-07 — Added a note about why only this hackish method works properly.

Источник

Does python’s ctypes c_char work for windows 64?

I’m trying to load a process list, and it functions correctly on 32bit python. However, on 64bit, I can’t get the process name to list. The code is below. If I change szExeFile’s structure from a c_char, to c_int or long, the process list enumerate, but I have no way of seeing what pid belongs to what exe. How can I get this to function on x64?

from ctypes import * from ctypes.wintypes import * import sys # const variable # Establish rights and basic options needed for all process declartion / iteration TH32CS_SNAPPROCESS = 2 STANDARD_RIGHTS_REQUIRED = 0x000F0000 SYNCHRONIZE = 0x00100000 PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF) TH32CS_SNAPMODULE = 0x00000008 TH32CS_SNAPTHREAD = 0x00000004 ## Create object definitions to story information in class PROCESSENTRY32(Structure): _fields_ = [ ( 'dwSize' , c_uint ) , ( 'cntUsage' , c_uint) , ( 'th32ProcessID' , c_uint) , ( 'th32DefaultHeapID' , c_uint) , ( 'th32ModuleID' , c_uint) , ( 'cntThreads' , c_uint) , ( 'th32ParentProcessID' , c_uint) , ( 'pcPriClassBase' , c_long) , ( 'dwFlags' , c_uint) , ( 'szExeFile' , c_char * 260 ) ] CreateToolhelp32Snapshot= windll.kernel32.CreateToolhelp32Snapshot Process32First = windll.kernel32.Process32First Process32Next = windll.kernel32.Process32Next GetLastError = windll.kernel32.GetLastError OpenProcess = windll.kernel32.OpenProcess GetPriorityClass = windll.kernel32.GetPriorityClass CloseHandle = windll.kernel32.CloseHandle try: hProcessSnap = c_void_p(0) hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS , 0 ) pe32 = PROCESSENTRY32() pe32.dwSize = sizeof( PROCESSENTRY32 ) ret = Process32First( hProcessSnap , pointer( pe32 ) ) global PROGPid PROGPid=False while ret: print pe32.dwSize,pe32.cntUsage,pe32.th32ProcessID,pe32.th32DefaultHeapID,pe32.th32ModuleID,pe32.cntThreads,pe32.th32ParentProcessID,pe32.pcPriClassBase,pe32.dwFlags,pe32.szExeFile hProcess = OpenProcess( PROCESS_ALL_ACCESS , 0 , pe32.th32ProcessID ) dwPriorityClass = GetPriorityClass( hProcess ) if dwPriorityClass == 0 : CloseHandle( hProcess ) PROGPid=pe32.th32ProcessID ret = Process32Next( hProcessSnap, pointer(pe32) ) print PROGPid CloseHandle ( hProcessSnap ) except: print "Error in ListProcessPid" 

Источник

Читайте также:  Php сбросить кэш файлов
Оцените статью