可以在 32 位但不能在 64 位中使用 ctypes 访问某些 kernel32 函数

Can access some kernel32 functions with ctypes in 32 bit but not 64 bit

我正在尝试在 64 位 python 应用程序中使用来自 kernel32 的 InterlockedExchange

这是我理想中希望运行的代码:

import ctypes
from ctypes import *

interlockedValue = ctypes.c_long(5)

print(interlockedValue.value)

locked = ctypes.c_long(68)
print(windll.kernel32.InterlockedExchange(byref(interlockedValue),locked))

print(interlockedValue.value)

然而这是我的 64 位输出 python 3.5.2:

C:\Users\Douglas Sexton\Source\Repos\SharedMemory\SharedMemory\Python>python interlocked3.py
5
Traceback (most recent call last):
File "interlocked3.py", line 10, in <module>
print(windll.kernel32.InterlockedExchange(byref(interlockedValue), locked))
File "C:\Program Files (x86)\Python35\lib\ctypes\__init__.py", line 360, in __getattr__
func = self.__getitem__(name)
File "C:\Program Files (x86)\Python35\lib\ctypes\__init__.py", line 365, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'InterlockedExchange' not found

32 位工作如我所料:

C:\Users\Douglas Sexton\Source\Repos\SharedMemory\SharedMemory\Python>"C:\Program Files (x86)\Python36\python.exe" interlocked3.py
5
5
68

我也尝试从序号访问:

import ctypes
from ctypes import *

interlockedValue = ctypes.c_long(5)

print(interlockedValue.value)

locked = ctypes.c_long(68)

print(windll.kernel32[868](byref(interlockedValue), locked))

print(interlockedValue.value)

这对于 32 位具有相同的输出,但对于 64 位,这是输出:

C:\Users\Douglas Sexton\Source\Repos\SharedMemory\SharedMemory\Python>python interlocked.py
5
0
0

我已经尝试了几种不同的方法来从 python 64 访问 InterlockedExchange,现在似乎都 运行 进入了同一个问题。我已经能够使用 python 64 的其他 kernel32 函数。这让我发疯。

我现在暂时不需要这个,但我能够为 InterlockedExchange 创建一个包装器 dll 并从 64 位进程成功使用它。