Skip to main content

Access selected win32 APIs through ctypes

Project description

logo ctwin32

winonly PyPI - Version PyPI - Python Version License - MIT PyPI - Stats


ctwin32 is a pure Python module, that wraps some Windows APIs (win32) by using ctypes. Since it is my personal playground, the selection of supported APIs and the way those are wrapped are solely dictated by my needs and preferences.

ctwin32 is not a replacement for pywin32, although it serves that purpose for me (in conjunction with comtypes). Compared with pywin32 the coverage of ctwin32 is downright tiny.

Here is an example of a classic ‘Hello world’ program:

from ctwin32 import user, gdi, wndcls, wtypes
from ctwin32 import WM_CREATE, WM_PAINT, WM_DESTROY
from ctwin32 import DT_CENTER, DT_SINGLELINE, DT_VCENTER

class HelloWnd(wndcls.SimpleWnd):

    DT_FLAGS = DT_CENTER | DT_SINGLELINE | DT_VCENTER
    MSG = "Hello from ctwin32!"

    def on_message(self, msg, wp, lp):

        if msg == WM_CREATE:
            lf = wtypes.LOGFONT(
                lfHeight=-72,
                lfFaceName = "MS Shell Dlg"
                )
            self.font = gdi.CreateFontIndirect(lf)
            return 0

        if msg == WM_PAINT:
            hdc, ps = self.begin_paint()
            oldfont = gdi.SelectObject(hdc, self.font)
            user.DrawText(hdc, self.MSG, self.client_rect(), self.DT_FLAGS)
            gdi.SelectObject(hdc, oldfont)
            self.end_paint(ps)
            return 0

        if msg == WM_DESTROY:
            gdi.DeleteObject(self.font)
            user.PostQuitMessage(0)
            return 0

        return self.def_win_proc(msg, wp, lp)

if __name__ == "__main__":

    icon = wndcls.load_ctwin32_ico()
    wnd = HelloWnd(wndcls.WndCreateParams("Hello Window", icon))
    wnd.show()

    while msg := user.GetMessage():
        user.TranslateMessage(msg)
        user.DispatchMessage(msg)

See the samples directory for several instances of how ctwin32 can be used.

While there is already another Python module that does something very similar to ctwin32 (pywin32-ctypes), it has a different purpose (namely to serve the internal needs of its creator). However, ctwin32 wraps significantly more API functions than pywin32-ctypes (as of September 2025: 417 / 16).


Note: Even though ctwin32 is a pure Python module, it can of course only be used on Windows. For the first three years or so, packages were tagged with the Windows platform tags to prevent pip from using them on other operating systems. However, it became futile to try to prevent this when after only one year the sdist package was also released. In the absence of suitable wheel packages for other operating systems, this would mean that pip install ctwin32 would now use the sdist package to install ctwin32 anyway. Therefore, a universal wheel is now distributed and it is up to the user to be smart enough to use it on Windows only.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ctwin32-4.0.0.tar.gz (203.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ctwin32-4.0.0-py3-none-any.whl (162.3 kB view details)

Uploaded Python 3

File details

Details for the file ctwin32-4.0.0.tar.gz.

File metadata

  • Download URL: ctwin32-4.0.0.tar.gz
  • Upload date:
  • Size: 203.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ctwin32-4.0.0.tar.gz
Algorithm Hash digest
SHA256 5a13813fd4ff9bae1f35f205b7394dc745c94013bb1f587a3280f5707ad3bddf
MD5 e8a2cdc5563292bac5322c8eec811f9c
BLAKE2b-256 2c5cdedf0b7f886662bc58cb251452bbf576bb987588f696eaabfba81d536b28

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctwin32-4.0.0.tar.gz:

Publisher: release.yml on RoccoMatano/ctwin32

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ctwin32-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: ctwin32-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 162.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ctwin32-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b938a42c2fbb49d26dd8d7fbbdfde62574d4f8217413bd941e2bcd421e65fcf
MD5 d065fcfceac9f5b835d45f7593a46875
BLAKE2b-256 3c3ea1b0d1acf1500e922eebb9f8c0093bb58e2119c43a55584f26e44fab2f3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctwin32-4.0.0-py3-none-any.whl:

Publisher: release.yml on RoccoMatano/ctwin32

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page