OSError: [WinError 193] %1 is not a valid Win32 application when using ctypes

OSError: [WinError 193] %1 is not a valid Win32 application when using ctypes

我正在尝试使用 kernal32 dll 并使用其中的函数。尝试这样做时,出现错误。

Traceback (most recent call last):
  File "C:\Users\Tawfiq\DesktopD render\win32.py", line 2, in <module>
    mydll = cdll.LoadLibrary(r"C:\Windows\SysWOW64\kernel32.dll")
  File "C:\Program Files\Python39\lib\ctypes\__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "C:\Program Files\Python39\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

这是我正在执行的代码:

from ctypes import*
mydll = cdll.LoadLibrary(r"C:\Windows\SysWOW64\kernel32.dll")
print(mydll.timeGetTime())

c:\windows\syswow64 包含 32 位 DLL。您的 Python 路径是 c:\Program Files,这是 64 位 Python 安装位置。不能混。

不要硬编码路径。只需使用 mydll = WinDLL('kernel32') 和 Windows 将搜索 Python 运行(32 位或 64 位)的正确标准位置。