You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Foundation.HWND doesn't seem to have an IsNull property, Graphics.Gdi.HGDIOBJ does.
Error CS1061 'HWND' does not contain a definition for 'IsNull' and no accessible extension method 'IsNull' accepting a first argument of type 'HWND' could be found (are you missing a using directive or an assembly reference?
That's where I suspected the bug to be too. We have had a little churn in whether we generate IntPtr or nint lately. I think though, rather than update the condition check to allow for nint, I'd rather fix the generator to produce IntPtr as it is intended to. I realize in C# 11 it's equivalent, but that's not out yet, and IntPtr does a better job of conveying "this is a pointer-sized opaque value" than nint does.
We want the general type settings to prefer nint over IntPtr though. It's only in the handle structs that we want to prefer IntPtr. We have code to do this, but it's failing... somewhere.
I wouldn't bother changing it again for C# 11, since if they are equivalent, there's no value in switching from one to the other.
While I agree with you, Joseph Musser (@jnm2), I suspect time will judge differently. When everyone is using C# 11+ and the two are truly aliases of each other, I suspect people will consider them as equivalent as int and Int32. These days we do still split hairs (at least I do) in preferring int but using Int32 when I want to be explicit about its length -- not because I have to, but it can be useful when implementing network protocols. Time will tell how people feel about IntPtr vs. nint at that point. But it certainly can do no harm to continue to use IntPtr here, and it certainly is useful to do so in the hear and now.
I think because there is no method for releasing the HKL, there is no hit in MetadataIndex.HandleTypeReleaseMethod. So then its up to the special cases section.
I think this works, but might need a type check for intptr?
privateboolIsHandle(TypeDefinitionHandletypeDefHandle,outstring?releaseMethodName){if(this.MetadataIndex.HandleTypeReleaseMethod.TryGetValue(typeDefHandle,outreleaseMethodName)){returntrue;}elseif(this.GetInvalidHandleValues(typeDefHandle).Count>0){returntrue;}// Special case handles that do not carry RAIIFree attributes.releaseMethodName=null;TypeDefinitiontypeDef=this.Reader.GetTypeDefinition(typeDefHandle);returnthis.Reader.StringComparer.Equals(typeDef.Name,"HGDIOBJ")||this.Reader.StringComparer.Equals(typeDef.Name,"HWND");}
But CreateCommonTypeDefMembers would need to change to again check for invalid values and then generate code which checked for the invalid values instead of default?
My above attempts I believe were wrong. I think DeclareTypeDefStruct needs to always have isHandle=true, as its only called when the metadata has the NativeTypedef attribute. Additionally, the section in CreateCommonTypeDefMembers which adds IsNull checks for IntPtr and UIntPtr, so that covers the "mostly" part as far as I can tell.
If there is a counter example we can add it to the tests and see if we need additional information from the metadata.
Andrew Arnott (@AArnott) should the win32metadata repo just explicitly tag each typedef struct which is a handle? Are all the structs with intptr values handles?
I think we can refactor the code where ishandle is called. If you ignore the result, the all nativetypedef structs with intptr are generated with IsNull.
Steve Otteson (@sotteson1) We are looking at how to distinguish between a NativeTypedef which is a handle and one that just has an IntPtr. It seems like not all NativeTypedefs with IntPtr are handles since some are defined in autotypes with "ValueType": "DECLARE_HANDLE" and so a new attribute might be required. Am I way off the mark with this?
Found this comment in Winforms, which might help sway us towards wanting to use nint/nuint over IntPtr/UIntPtr.
// It is particularly dangerous to cast to/from IntPtr on 64 bit platforms as casts are checked.
// Doing so leads to hard-to-find overflow exceptions. We've mitigated this historically by trying
// to first cast to long when casting out of IntPtr and first casting to int when casting into an
// IntPtr. That pattern, unfortunately, is difficult to audit and has negative performance implications,
// particularly on 32bit.
//
// Using the new nint/nuint types allows casting to follow with the default project settings, which
// is unchecked. Casting works just like casting between other intrinsic types (short, int, long, etc.).
Lachlan Ennis (@elachlan), that comment is interesting and I'd like to learn more. But why post it here on an issue about adding IsNull, which may be closed as Won't Fix, but this casting concern should likely have its own discussion lifetime?
Lachlan Ennis (@elachlan), that comment is interesting and I'd like to learn more. But why post it here on an issue about adding IsNull, which may be closed as Won't Fix, but this casting concern should likely have its own discussion lifetime?
Basically this issue is caused by the setting for preferring nint over IntPtr.
Adding this here based on Andrew's question in the PR:
Given how hard it is to get this right, I wonder if it's worth it. Particularly because all this change would do is enable an .IsNull syntax where == default would already do the same. Why is this so important?
I think its less about .IsNull and more about CsWin32 and other Win32Metadata consumers not knowing how to tell if something is a handle or not. An attribute in the metadata would remove that ambiguity. Really .IsNull is just a symptom of the problem and if the ambiguity isn't rectified in the metadata, I imagine there will be other "symptoms".
if the ambiguity isn't rectified in the metadata, I imagine there will be other "symptoms".
I would be content to leave the metadata alone so they can focus on more meaningful changes, until one of these other symptoms shows up with a compelling case for fixing. I don't think IsNull is compelling enough.
Lachlan Ennis (@elachlan), if your concern about IntPtr checked casting remains (I suspect it does), can you please file a new issue to track that? Consider that I would like handle typedef's to use IntPtr instead of nint as their field type. At one point this worked, but I don't know whether that still works. So even if we have nint now, we may have IntPtr later unless a discussion (on a new issue) compels me to give it up and use nint to avoid checked cast exceptions.
I mean, the majority of the typedefstructs work. So the PR isn't a complete failure. Its just if you want complete coverage of all handles then we would need more info from the metadata.
Activity
[-]Generated HKL IsNull helper[/-][+]Generate HKL IsNull helper[/+]elachlan commentedon Jul 21, 2022
Is this where we make the change?
CsWin32/src/Microsoft.Windows.CsWin32/Generator.cs
Lines 3776 to 3788 in 2a93435
elachlan commentedon Jul 21, 2022
Foundation.HWND doesn't seem to have an IsNull property, Graphics.Gdi.HGDIOBJ does.
Error CS1061 'HWND' does not contain a definition for 'IsNull' and no accessible extension method 'IsNull' accepting a first argument of type 'HWND' could be found (are you missing a using directive or an assembly reference?elachlan commentedon Jul 21, 2022
Andrew Arnott (@AArnott) I think the issue is because
Foundation.HWNDusesnintandGraphics.Gdi.HGDIOBJusesIntPtr.This code only checks
IntPtrandUIntPtr.CsWin32/src/Microsoft.Windows.CsWin32/Generator.cs
Lines 3920 to 3928 in 2a93435
I think this:
needs to change to:
I'll create a PR if you are happy with that.
AArnott commentedon Jul 21, 2022
That's where I suspected the bug to be too. We have had a little churn in whether we generate IntPtr or nint lately. I think though, rather than update the condition check to allow for
nint, I'd rather fix the generator to produceIntPtras it is intended to. I realize in C# 11 it's equivalent, but that's not out yet, andIntPtrdoes a better job of conveying "this is a pointer-sized opaque value" thannintdoes.elachlan commentedon Jul 21, 2022
I played around with it and I think its caused by
PreferNativeInton thegeneralTypeSettingsin Generator:CsWin32/src/Microsoft.Windows.CsWin32/Generator.cs
Lines 386 to 393 in 2a93435
This causes the output to change:
CsWin32/src/Microsoft.Windows.CsWin32/PrimitiveTypeHandleInfo.cs
Lines 24 to 47 in 6ea3f44
So I guess we hardcode it for now and update it once
LanguageVersion.CSharp11is included inMicrosoft.CodeAnalysis.CSharp?AArnott commentedon Jul 21, 2022
Thanks for looking.
We want the general type settings to prefer
nintoverIntPtrthough. It's only in the handle structs that we want to prefer IntPtr. We have code to do this, but it's failing... somewhere.I wouldn't bother changing it again for C# 11, since if they are equivalent, there's no value in switching from one to the other.
jnm2 commentedon Jul 21, 2022
And in C# 11 I still feel like showing the type as
IntPtrversusnintcarries useful expectations on the meaning of the contents.AArnott commentedon Jul 21, 2022
While I agree with you, Joseph Musser (@jnm2), I suspect time will judge differently. When everyone is using C# 11+ and the two are truly aliases of each other, I suspect people will consider them as equivalent as
intandInt32. These days we do still split hairs (at least I do) in preferringintbut usingInt32when I want to be explicit about its length -- not because I have to, but it can be useful when implementing network protocols. Time will tell how people feel about IntPtr vs. nint at that point. But it certainly can do no harm to continue to use IntPtr here, and it certainly is useful to do so in the hear and now.elachlan commentedon Jul 22, 2022
This is the code where it decides if a Handle struct is a handle:
CsWin32/src/Microsoft.Windows.CsWin32/Generator.cs
Lines 3776 to 3788 in 2a93435
I think because there is no method for releasing the
HKL, there is no hit inMetadataIndex.HandleTypeReleaseMethod. So then its up to the special cases section.A lot of these types are
DECLARE_HANDLEand have theInvalidHandleValuesvalue. Could we use that?https://github.com/microsoft/win32metadata/blob/5687b3cb4868c3142b98091ee970464f55219406/generation/WinSDK/autoTypes.json#L114-L118
Would
IsNullstill make sense in that context?elachlan commentedon Jul 22, 2022
I think this works, but might need a type check for intptr?
But
CreateCommonTypeDefMemberswould need to change to again check for invalid values and then generate code which checked for the invalid values instead of default?CsWin32/src/Microsoft.Windows.CsWin32/Generator.cs
Lines 3920 to 3928 in 2a93435
elachlan commentedon Jul 22, 2022
Can you clarify mostly?
My above attempts I believe were wrong. I think DeclareTypeDefStruct needs to always have isHandle=true, as its only called when the metadata has the
NativeTypedefattribute. Additionally, the section in CreateCommonTypeDefMembers which addsIsNullchecks forIntPtrandUIntPtr, so that covers the "mostly" part as far as I can tell.If there is a counter example we can add it to the tests and see if we need additional information from the metadata.
elachlan commentedon Jul 25, 2022
Andrew Arnott (@AArnott) should the win32metadata repo just explicitly tag each typedef struct which is a handle? Are all the structs with intptr values handles?
1 remaining item
elachlan commentedon Jul 25, 2022
Can you think of an example of a NativeTypedef struct with an IntPtr which isn't a handle?
AArnott commentedon Jul 25, 2022
No. So far I've coded CsWin32 to consider that all typedef structs with intptr are the same.
elachlan commentedon Jul 26, 2022
I think we can refactor the code where ishandle is called. If you ignore the result, the all nativetypedef structs with intptr are generated with IsNull.
elachlan commentedon Aug 16, 2022
Steve Otteson (@sotteson1) We are looking at how to distinguish between a
NativeTypedefwhich is a handle and one that just has anIntPtr. It seems like not allNativeTypedefswithIntPtrare handles since some are defined in autotypes with"ValueType": "DECLARE_HANDLE"and so a new attribute might be required. Am I way off the mark with this?elachlan commentedon Aug 17, 2022
Jeremy Kuhne (@JeremyKuhne) can I get your thoughts?
elachlan commentedon Aug 19, 2022
Found this comment in Winforms, which might help sway us towards wanting to use
nint/nuintoverIntPtr/UIntPtr.https://github.com/dotnet/winforms/blob/179df82381b5df175d043c2190306354f8b46cae/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs#L39-L50
AArnott commentedon Aug 19, 2022
Lachlan Ennis (@elachlan), that comment is interesting and I'd like to learn more. But why post it here on an issue about adding
IsNull, which may be closed as Won't Fix, but this casting concern should likely have its own discussion lifetime?elachlan commentedon Aug 19, 2022
Basically this issue is caused by the setting for preferring nint over IntPtr.
elachlan commentedon Aug 19, 2022
Adding this here based on Andrew's question in the PR:
I think its less about
.IsNulland more about CsWin32 and other Win32Metadata consumers not knowing how to tell if something is a handle or not. An attribute in the metadata would remove that ambiguity. Really.IsNullis just a symptom of the problem and if the ambiguity isn't rectified in the metadata, I imagine there will be other "symptoms".AArnott commentedon Aug 19, 2022
I would be content to leave the metadata alone so they can focus on more meaningful changes, until one of these other symptoms shows up with a compelling case for fixing. I don't think IsNull is compelling enough.
Lachlan Ennis (@elachlan), if your concern about IntPtr checked casting remains (I suspect it does), can you please file a new issue to track that? Consider that I would like handle typedef's to use
IntPtrinstead ofnintas their field type. At one point this worked, but I don't know whether that still works. So even if we havenintnow, we may have IntPtr later unless a discussion (on a new issue) compels me to give it up and usenintto avoid checked cast exceptions.elachlan commentedon Aug 19, 2022
New issue is at #661
elachlan commentedon Aug 19, 2022
I mean, the majority of the typedefstructs work. So the PR isn't a complete failure. Its just if you want complete coverage of all handles then we would need more info from the metadata.