-
Notifications
You must be signed in to change notification settings - Fork 94
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
(c#) Why now struct HWND instead of IntPtr ? #61
Comments
We use structs for each unique handle type to give added type safety, similar to that enjoyed by C developers. HWND as a parameter type immediately gives an idea on its content and where you might come by a value to fit, whereas IntPtr tells nearly nothing. Our structs that serve as IntPtr substitutes are implicitly convertable to IntPtr, so you can use them without casts anywhere that an IntPtr is expected. |
I understand, thank you. |
@AArnott is this documented somewhere? You get an It sounds like you can just do: PInvoke.SetForegroundWindow((HWD)process.MainWindowHandle); |
Yes, you can cast them. If Process APIs use IntPtr instead of handle struct types, can you please file a bug at https://github.com/microsoft/win32metadata/issues giving specific APIs as examples? |
I'm sorry for misdirecting you. public static implicit operator nint(HWND value) => value.Value;
public static explicit operator HWND(nint value) => new HWND(value); Note that |
I believe his example was showing the BCL Process API https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.mainwindowhandle. |
Thanks @AArnott, should we re-open this issue for tracking the regression then? |
@michael-hawker What is the regression? Per my last comment, the behavior of requiring an explicit cast to "upgrade" an |
@michael-hawker Did you mean the regression Andrew mentioned as already being tracked at #312? |
Yup, sorry. Didn't connect the dots that that issue was tracking the regression itself. |
Hi,
outside of this project the parameter type for handles is IntPtr, e.g.:
GetWindow(IntPtr hWnd, ...)
SetFocus(IntPtr hWnd)
That fits to the return type of functions which deliver these arguments like:
IntPtr Process.MainWindowHandle { get; }
IntPtr System.Windows.Interop.WindowInteropHelper.Handle { get; }
But win32metadata generates functions with Microsoft.Windows.Sdk.HWND as parameter type. E.g.:
PInvoke.GetWindow( HWND hwnd, ... );
PInvoke.SetFocus( HWND hwnd );
With the consequence, that we first have to convert IntPtr returns to HWND, before we can use win32metadata stuff.
What is the intension behind that type change ?
Whatever disadvantage is removed at HWND site isn't completely gone, its presumably just moved to that convert place.
thank you in advance for any candle light
The text was updated successfully, but these errors were encountered: