0

When I run the code I get this error:

[WinError 193]% 1 is not a valid Win32 application

    from ctypes import *
    mydll = windll.LoadLibrary(r"C:\Windows\SysWOW64\kernel32.dll")
    mydll.Beep(2000,500)
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21
  • [\[SO\]: Python Ctypes - loading dll throws OSError: \[WinError 193\] %1 is not a valid Win32 application (@CristiFati's answer)](https://stackoverflow.com/questions/57187566/python-ctypes-loading-dll-throws-oserror-winerror-193-1-is-not-a-valid-win/57297745#57297745). You have *064bit* *Python* trying to open a *032bit* *.dll*. Use *r"C:\Windows\System32\kernel32.dll"* or simply *r"kernel32.dll"*. – CristiFati Jan 16 '22 at 22:49

1 Answers1

1

TL;DR Use dll = WinDLL('kernel32'). That path is in the default DLL search path.

You have to use 32-bit DLLs with 32-bit Python and 64-bit DLLs with 64-bit Python.

Non-intuitively, C:\Windows\SysWOW64 on 64-bit Windows contains DLLs compatible with 32-bit applications and C:\Windows\System32 contains 64-bit DLLs.

However, whether you are using 32-bit Python or 64-bit Python, always use C:\Windows\System32 if hard-coding the path because for backward compatibility, 32-bit applications originally written for 32-bit Windows will try to open C:\Windows\System32 but if run on 64-bit Windows will automatically redirect the access to C:\Windows\SysWOW64.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251