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 31, 2026

Copy link
Copy Markdown
Contributor Author

Ping @zooba for review

@thexai
thexai marked this pull request as draft August 4, 2026 11:15
@thexai

thexai commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

I've finally resolved all the issues related to ctype. The first error was due to loading kernel32 with LoadPackagedLibrary. Being able to use LoadLibrary resolved it automatically. This has been changed in #152792.

The second error was a MemoryError exception. This is because VirtualAlloc doesn't have code execution permissions in UWP. Since don't think execution permission is needed, changed also for Windows Desktop.

@thexai
thexai marked this pull request as ready for review August 4, 2026 16:38
UWP does not allow allocating memory with code execution permissions.
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