GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-85470: Delay load ole32.dll in the _ctypes module by serhiy-storchaka · Pull Request #155435 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 9, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Measured on Windows 11 with a debug x64 build -- import _ctypes before the change loads user32.dll, ole32.dll, gdi32.dll, win32u.dll and combase.dll; after it, only combase.dll remains (pulled in by oleaut32, which does not depend on user32). A plain python.exe already loaded none of them, so ctypes was the last thing making the interpreter a GUI process -- and it is the module a console script needs in order to call SetConsoleCtrlHandler() in the first place.

ProgIDFromCLSID() in SetComError() is the only ole32 use in _ctypes, and it is reached only when formatting an error from a COM interface method. PCbuild/_wmi.vcxproj already delay loads the same DLL.

test_ctypes passes (679 tests). This covers the PCbuild build only; resolving ProgIDFromCLSID() through GetProcAddress() would also cover other toolchains, at the cost of some C code.

  • Issue: [ctypes] Enable handling logoff and shutdown Windows console events #85470

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
pythongh-85470: Delay load ole32.dll in the _ctypes module
ab4d315
ole32.dll pulls in user32.dll, which makes the system treat the process as
a GUI process and stop sending logoff and shutdown console control events
to it.  It is only needed for ProgIDFromCLSID() when formatting a COM error.
@serhiy-storchaka
serhiy-storchaka requested a review from a team as a code owner August 9, 2026 13:52
@serhiy-storchaka
serhiy-storchaka requested review from eryksun and zooba August 9, 2026 13:53
@bedevere-app bedevere-app Bot mentioned this pull request Aug 9, 2026
[ctypes] Enable handling logoff and shutdown Windows console events #85470
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Aug 9, 2026
@serhiy-storchaka

serhiy-storchaka commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

Verified the actual effect, not just the module list: two processes started from the same build in the same interactive session, one of them force-loading user32.dll to emulate the previous state, each with a SetConsoleCtrlHandler() installed through ctypes. On logoff the process without user32.dll received CTRL_LOGOFF_EVENT and ran its handler; the one with user32.dll loaded was terminated without any event.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka merged commit 0e8100a into python:main Aug 29, 2026
52 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Aug 29, 2026
@serhiy-storchaka
serhiy-storchaka deleted the gh-85470-ctypes-delayload-ole32 branch August 29, 2026 18:04
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Sep 12, 2026
@serhiy-storchaka @clin1234
pythongh-85470: Delay load ole32.dll in the _ctypes module (pythonGH-…
e2eb95f
…155435)

ole32.dll pulls in user32.dll, which makes the system treat the process as
a GUI process and stop sending logoff and shutdown console control events
to it.  It is only needed for ProgIDFromCLSID() when formatting a COM error.
This was referenced Oct 8, 2026
Windows: import hashlib loads user32.dll and the GDI DLLs through libcrypto; delay-load user32 #159008
Open
gh-159008: Delay load user32.dll in libcrypto on Windows #159009
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eryksun eryksun Awaiting requested review from eryksun
@zooba zooba Awaiting requested review from zooba

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.