代码之家  ›  专栏  ›  技术社区  ›  Douglas Sexton

可以使用32位但不是64位的ctypes访问某些kernel32函数

  •  2
  • Douglas Sexton  · 技术社区  · 7 年前

    我正在尝试使用 InterlockedExchange 来自64位python应用程序中的内核32。

    这是我最希望的代码:

    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函数。这让我快疯了。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Douglas Sexton    7 年前

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