WinApi icon indicating copy to clipboard operation
WinApi copied to clipboard

NativeWindow doesn't provide OnHandleChange

Open jaydcarlson opened this issue 6 years ago • 1 comments

For some lightweight applications, such as sniffing WM_DEVICECHANGE (and subsequent DBT_DEVICEARRIVAL events), we must call RegisterDeviceNotification() when we have a valid handle; but we must also re-register if our handle ever changes. In the Windows.Forms NativeWindow implementation, this is done in the OnHandleChanged() method. This PR adds such a method to WinApi.

jaydcarlson avatar Mar 16 '18 06:03 jaydcarlson

Hi @jaydcarlson, thanks for the PR :) But lets think about it a moment. WinForms does a lot more than just a think wrapper, though I don't exactly remember how and when a handle changes.

The problem I see with merging this is that - it creates an API with caveats. The class provides an OnHandleChange - so from the outside perspective it must cleanly abstract all Handle changes, but it is not so. It is only handled when the change is limited to the NativeWindow itself. NativeWindow is a class that's essentially the core and lightweight bridge. So, it doesn't guarantee that handle changes are within it's scope in entirety.

The second problem is that it causes OnHandleChange during zeroing out the pointers. This may be desired in some cases, and not desired in others.

I think it's simpler and caveat free, when change of handles is just handled externally when you need it. Please let me know if you think there's any other way to do this without these caveats above.

prasannavl avatar May 21 '18 11:05 prasannavl