Home > Net >  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

Time:09-24

i am trying to use kernal32 dll and use functions from it. when trying to do that, i got the error.

Traceback (most recent call last):
  File "C:\Users\Tawfiq\Desktop\2D 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

this is the code i am executing:

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

CodePudding user response:

c:\windows\syswow64 contains 32-bit DLLs. Your Python path is c:\Program Files which is the 64-bit Python installation location. You can't mix.

Don't hard-code the path. Just use mydll = WinDLL('kernel32') and Windows will search the correct standard location for the Python running (32- or 64-bit).

  • Related