dotnet / pinvoke

A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS.
MIT License
2.12k stars 222 forks source link

User32: UnhookWinEvent is missing #457

Closed x1unix closed 4 years ago

x1unix commented 4 years ago

Hello, I noticed that UnhookWinEvent from User32 is missing.

Can you add please it?

[DllImport("user32.dll")]
public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
AArnott commented 4 years ago

We actually do have it, but it's private because we expect people to use SafeEventHookHandle, which is what the public SetWinEventHook method returns. Are you getting the handle from some other API such that you need direct access to the IntPtr method? Would it make sense for you to initialize a SafeEventHookHandle object when you get the IntPtr another way, and use that, and then close the handle using that object?

x1unix commented 4 years ago

@AArnott I had my own package with user32 bindings. Recently discovered this repo and decided to migrate to it (basically replaced all my bindings in favor of pinvoke) and discovered that this function was missing.

AArnott commented 4 years ago

@x1unix can you open a separate issue to discuss GetMenuItemInfo? Let's try to keep each issue's discussion focused. Your comment above would be great to directly copy over to that issue.

Still waiting for your response as to how/whether you can use SafeEventHookHandle for this issue.

x1unix commented 4 years ago

@AArnott can safeEventHookHandle.Dispose() be used instead of UnhookWinEvent()?

If yes - this is not so critical but maybe someone will need UnhookWinEvent in some exotic situations when messing with some low-level stuff.

Maybe it's worth to mention that hook can be removed in easy way with .Dispose() in SafeEventHookHandle documentation since this wrapper exists only in this particular library.

AArnott commented 4 years ago

Yes, the SafeEventHookHandle.Dispose method calls UnhookWinEvent internally. It's pretty obvious in the API, IMO, because the APIs that produce handles that need to be unhooked produce SafeEventHookHandle instead of IntPtr. Since SafeEventHookHandle derives from SafeHandle, it follows the typical .NET rules for such classes. So I'm not sure where else this would need to be documented.

x1unix commented 4 years ago

@AArnott thank you for response :)