Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn .net users of SeWindowsHookExW about garbage-collection of callbacks #1792

Open
wants to merge 1 commit into
base: docs
Choose a base branch
from

Conversation

asklar
Copy link
Contributor

@asklar asklar commented Mar 17, 2024

No description provided.

Copy link

@asklar : Thanks for your contribution! The author(s) have been notified to review your proposed change.

@asklar asklar changed the title Update nf-winuser-setwindowshookexw.md Warn .net users of SeWindowsHookExW about garbage-collection of callbacks Mar 17, 2024
@@ -300,6 +300,8 @@ An error may occur if the <i>hMod</i> parameter is <b>NULL</b> and the <i>dwThre

Calling the [CallNextHookEx function](nf-winuser-callnexthookex.md) function to chain to the next hook procedure is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call <b>CallNextHookEx</b> unless you absolutely need to prevent the notification from being seen by other applications.

In .net, you must ensure the callback is not moved around by the garbage collector (otherwise your app will crash with an ExecutionEngineException). One way to do this is by making the callback a static method of your class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do the other "set this method as a callback" docs say about this? We should probably be consistent. Also there's other GC'd runtimes beyond .NET...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnumWindows() (as an example) doesn't say anything, and I feel that is sufficient. Repeating ground rules for interfacing with C APIs shouldn't proliferate the API documentation.

If you feel that a warning is due, at the very least, make sure a developer who's not in the target audience can quickly determine, at a glance, that this information isn't for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants