Skip to content

gh-152433: Windows: fix ctypes error in UWP build#154700

Open
thexai wants to merge 1 commit into
python:mainfrom
thexai:uwp-fix-ctypes
Open

gh-152433: Windows: fix ctypes error in UWP build#154700
thexai wants to merge 1 commit into
python:mainfrom
thexai:uwp-fix-ctypes

Conversation

@thexai

@thexai thexai commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Fix ctypes error in UWP build:

In UWP is possible call GetLastError() Win32 API but not like import from ctypes with LibraryLoader:

GetLastError = windll.kernel32.GetLastError

The error produced is like this (in this case using Cryptodome):

EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
 - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <class 'AttributeError'>
Error Contents: kernel32
Traceback (most recent call last):
  File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\site-packages\Cryptodome\Util\_raw_api.py", line 83, in <module>
    raise ImportError("CFFI is not compatible with Python 3.12 on Windows")
ImportError: CFFI is not compatible with Python 3.12 on Windows

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 461, in __getattr__
    dll = self._dlltype(name)
  File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 390, in __init__
    self._handle = _dlopen(self._name, mode)
                   ~~~~~~~^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'kernel32' (or one of its dependencies). Try using the full path with constructor syntax.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Joel\AppData\Local\Packages\XBMCFoundation.Kodi_4n2hpmxwrvr6p\LocalState\addons\script.kodi.loguploader\default.py", line 1, in <module>
    from Cryptodome.Cipher import AES
  File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\site-packages\Cryptodome\Cipher\AES.py", line 26, in <module>
    from Cryptodome.Util._raw_api import (load_pycryptodome_raw_lib,
                                      VoidPointer, SmartPointer,
                                      c_size_t, c_uint8_ptr)
  File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\site-packages\Cryptodome\Util\_raw_api.py", line 170, in <module>
    import ctypes
  File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 492, in <module>
    GetLastError = windll.kernel32.GetLastError
                   ^^^^^^^^^^^^^^^
  File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 463, in __getattr__
    raise AttributeError(name)
AttributeError: kernel32
-->End of Python script error report<--

Since GetLastError is available from _winapi, the workaround is import from _winapi and call directly.

@thexai

thexai commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

If this is not acceptable also is possible implement GetLastError() function inside ctypes (C code).

and change to:

from _ctypes import GetLastError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant