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
blockingA projection cannot use the latest versionnamespacesFeedback on namespace names or organizationrustCritical for Rust adoptionusabilityTouch-up to improve the user experience for a language projection
These attributes refer to other types without including their namespace, making it difficult to use in any meaningful way. It should ideally be something like this:
For now, I'm just assuming the type/function is in the same namespace as the type that includes the attribute but I'm guessing that may not always be the case.
No, the Apis class is an implementation detail of the metadata that would ideally not exist (e.g. ECMA 335 supports free functions). Logically, the HeapDestroy function is in the Windows.Win32.SystemServices namespace. So it would just be Windows.Win32.SystemServices.HeapDestroy.
The attributes are only in the metadata, so IMO they should refer to other members by their metadata names. The Apis class is not an implementation detail of the metadata, since all direct users of the metadata have to know about it and write projections based on it.
What I mean is that the very existence of the Apis class regrettable. It is a concession to the implementation choices of using C# to generate metadata. Had it not been for this implementation detail, the Apis class would not need to exist. Logically, at least for languages that support namespace-level functions (like C++ and Rust and perhaps others), the Apis class is just this odd placeholder type where we find the "free functions" for that namespace. There is after all no "Apis" class in the original Win32 API headers that this project parses.
The C++ and Rust parsers perform type lookup via a two-string key consisting of namespace and name. In the example presented here, that would be "Windows.Win32.SystemServices" and "HeapDestroy". The Apis is not considered a namespace nor is it part of the name. I guess in C# you would consider these static methods of the Apis class, but that just seems like a very C# specific view of the world.
It is a concession to the implementation choices of using C# to generate metadata. Had it not been for this implementation detail, the Apis class would not need to exist.
I don't see any other way to represent methods in metadata without putting them on a type, or maybe you know how to do it. I would be happy to get rid of the Apis class if I can. I'm using a metadata writer to output to metadata, even though I start by walking a C# syntax tree generated by ClangSharp. I have flexibility to move away from C# when we need to (which I'm already doing a lot).
I think you're right. Software is full of compromise and this just seemed like one of those concessions we had to make to get the job done given the constraints we were given, metadata formats and such. I also didn’t fully grasp that you had moved away from a purely C# driven approach to metadata generation, so I was still thinking that’s all we had to work with.
I have to say that I’m thrilled with what you've come up with for win32 metadata. For years I’ve dreamed of having metadata for Win32 APIs and you’ve single-handedly made that a reality. The only reason I made that comment about methods and classes is that IL technically supports free functions. I happen to know that because I wrote about it many years ago, but its been so long I have no idea whether that would work for us in practice nor does it really matter. The Apis class is perfectly fine – it doesn’t bother me in the least. My only point really was that the Apis class is just the construct we use for grouping functions within namespaces. It’s not part of the fully-qualified name of the function, at least in the way that I think about it – every construct has a namespace and a name – and in that worldview there’s no room for the Apis class name. But I can see how that is somewhat subjective. 😊
I'm sorry if I gave the impression of being critical of the implementation. Far from it - I'm really quite thrilled.
This is probably a silly idea, but you could get rid of Apis class by defining the last namespace as a static class rather than a namespace. So instead of:
This would remove the need for the Apis class but it would also mean that the sub-types within the top-level class would typically be printed as ActiveDirectory+AccessControlEntry and it's just weird. Seems like a a choice between the lesser of 2 evils.
IMHO no, it doesn't, but it really depends on how you see it. The convention is to put "free" functions on the hidden module class. I don't know how well established this pattern is with tooling (maybe its even standardized somewhere), but at the end its just convention.
Considering that the hidden module class is global you would lose the ability to classify methods by namespace/component if you pick up this convention.
I finally tried implementing RAIIFree in Rust (to generate the equivalent of a destructor) but because HANDLE is in the Windows.Win32.SystemServices namespace but CloseHandle is in a different namespace there's no way for me to reliably find it. If HANDLE were defined as follows, it would be a piece of cake:
Note that storing the namespace as a type doesn't actually make things better. As I mentioned this is just encoded as a string anyway, but the real problem is that the function name is still stored as a string rather than a MethodDef record. Without that, a tool still needs to walk the ECMA-335 MethodDef table looking for the function since this table is not sorted. Now if the attribute were only used for functions with a single parameter, then this wouldn't be necessary to weed out functions that are un-callable - I assume that's still an issue.
I'm still tinkering with the code for this, but perhaps we can augment the RAIIFreeAttribute with an optional integer containing the Row ID (RID) of the freeing method in the MethodDef table. This would be generated at winmd generation time. This will eliminate the MethodDef table scan, simplifying code generation.
a tool still needs to walk the ECMA-335 MethodDef table looking for the function since this table is not sorted
The methods of a class have to be consecutive, and the methods of a COM interface have to be in the vtable order; but would it be feasible to sort the methods of Apis classes by name, and perhaps add an attribute to indicate this was done? Projections could then do a binary search.
Is it possible that a metadata assembly of an extension SDK will need to use RAIIFreeAttribute on a type of its own (or a parameter or a return value, if supported) but reference a freeing method in the Windows SDK metadata? In that case, the MethodDef row ID would not be reliable, as the version might not match exactly.
Yes, a simpler approach may be to guarantee that the functions in the Apis class are sorted by name to allow a binary search. Then the following simpler attribute would be sufficient:
Don't think that'll be possible as metadata relies on .NET for the build up and emission of the winmd, and I don't think there are any ordering guarantees or knobs to tweak there. Will take a deeper look though!
At this point, all AlsoUsableFor and RAIIFree definitions are in the same namespace as the struct they're on except for:
FreeLibrary in Windows.Win32.System.LibraryLoader while HMODULE/HINSTANCE in Windows.Win32.Foundation
Local/GlobalFree in Windows.Win32.System.Memory while HLOCAL/HGLOBAL in Windows.Win32.Foundation
Should we just move these Free functions to Windows.Win32.Foundation and call it a day?
Looking at the metadata now compared to 2 years ago when this was filed, I think it's fair to enforce that all AlsoUsableFor and RAIIFree relationships must exist in the same namespace. A violation of that would be a bug in the metadata.
blockingA projection cannot use the latest versionnamespacesFeedback on namespace names or organizationrustCritical for Rust adoptionusabilityTouch-up to improve the user experience for a language projection
Activity
kennykerr commentedon Mar 25, 2021
For now, I'm just assuming the type/function is in the same namespace as the type that includes the attribute but I'm guessing that may not always be the case.
sotteson1 commentedon Mar 26, 2021
I can fix it fairly easily. And HeapDestroy, it would include the class, right? Windows.Win32.SystemServices.Apis.HeapDestroy
kennykerr commentedon Mar 29, 2021
No, the
Apisclass is an implementation detail of the metadata that would ideally not exist (e.g. ECMA 335 supports free functions). Logically, theHeapDestroyfunction is in theWindows.Win32.SystemServicesnamespace. So it would just beWindows.Win32.SystemServices.HeapDestroy.AArnott commentedon Mar 29, 2021
The attributes are only in the metadata, so IMO they should refer to other members by their metadata names. The
Apisclass is not an implementation detail of the metadata, since all direct users of the metadata have to know about it and write projections based on it.kennykerr commentedon Mar 29, 2021
What I mean is that the very existence of the
Apisclass regrettable. It is a concession to the implementation choices of using C# to generate metadata. Had it not been for this implementation detail, theApisclass would not need to exist. Logically, at least for languages that support namespace-level functions (like C++ and Rust and perhaps others), theApisclass is just this odd placeholder type where we find the "free functions" for that namespace. There is after all no "Apis" class in the original Win32 API headers that this project parses.The C++ and Rust parsers perform type lookup via a two-string key consisting of namespace and name. In the example presented here, that would be "Windows.Win32.SystemServices" and "HeapDestroy". The
Apisis not considered a namespace nor is it part of the name. I guess in C# you would consider these static methods of theApisclass, but that just seems like a very C# specific view of the world.AArnott commentedon Mar 29, 2021
Ok, fair enough.
sotteson1 commentedon Mar 30, 2021
I don't see any other way to represent methods in metadata without putting them on a type, or maybe you know how to do it. I would be happy to get rid of the Apis class if I can. I'm using a metadata writer to output to metadata, even though I start by walking a C# syntax tree generated by ClangSharp. I have flexibility to move away from C# when we need to (which I'm already doing a lot).
kennykerr commentedon Apr 2, 2021
I think you're right. Software is full of compromise and this just seemed like one of those concessions we had to make to get the job done given the constraints we were given, metadata formats and such. I also didn’t fully grasp that you had moved away from a purely C# driven approach to metadata generation, so I was still thinking that’s all we had to work with.
I have to say that I’m thrilled with what you've come up with for win32 metadata. For years I’ve dreamed of having metadata for Win32 APIs and you’ve single-handedly made that a reality. The only reason I made that comment about methods and classes is that IL technically supports free functions. I happen to know that because I wrote about it many years ago, but its been so long I have no idea whether that would work for us in practice nor does it really matter. The Apis class is perfectly fine – it doesn’t bother me in the least. My only point really was that the Apis class is just the construct we use for grouping functions within namespaces. It’s not part of the fully-qualified name of the function, at least in the way that I think about it – every construct has a namespace and a name – and in that worldview there’s no room for the Apis class name. But I can see how that is somewhat subjective. 😊
I'm sorry if I gave the impression of being critical of the implementation. Far from it - I'm really quite thrilled.
marler8997 commentedon Apr 3, 2021
This is probably a silly idea, but you could get rid of
Apisclass by defining the last namespace as a static class rather than a namespace. So instead of:You could do this:
This would remove the need for the
Apisclass but it would also mean that the sub-types within the top-level class would typically be printed asActiveDirectory+AccessControlEntryand it's just weird. Seems like a a choice between the lesser of 2 evils.weltkante commentedon Apr 6, 2021
IMHO no, it doesn't, but it really depends on how you see it. The convention is to put "free" functions on the hidden module class. I don't know how well established this pattern is with tooling (maybe its even standardized somewhere), but at the end its just convention.
Considering that the hidden module class is global you would lose the ability to classify methods by namespace/component if you pick up this convention.
kennykerr commentedon Apr 14, 2021
I finally tried implementing
RAIIFreein Rust (to generate the equivalent of a destructor) but becauseHANDLEis in theWindows.Win32.SystemServicesnamespace butCloseHandleis in a different namespace there's no way for me to reliably find it. IfHANDLEwere defined as follows, it would be a piece of cake:But perhaps
HANDLEandCloseHandlebelong in the same namespace? How are others handling this today?kennykerr commentedon Apr 15, 2021
Same for
Windows.Win32.MenusAndResources.HMENUwhose free function isWindows.Win32.WindowsAndMessaging.DestroyMenu.Same for HICON/HMENU/HCURSOR and possibly others.
17 remaining items
kennykerr commentedon Dec 9, 2022
Note that storing the namespace as a type doesn't actually make things better. As I mentioned this is just encoded as a string anyway, but the real problem is that the function name is still stored as a string rather than a
MethodDefrecord. Without that, a tool still needs to walk the ECMA-335MethodDeftable looking for the function since this table is not sorted. Now if the attribute were only used for functions with a single parameter, then this wouldn't be necessary to weed out functions that are un-callable - I assume that's still an issue.riverar commentedon Dec 10, 2022
I'm still tinkering with the code for this, but perhaps we can augment the
RAIIFreeAttributewith an optional integer containing the Row ID (RID) of the freeing method in the MethodDef table. This would be generated at winmd generation time. This will eliminate the MethodDef table scan, simplifying code generation.Example from my experimental winmd:
Screenshot from ILSpy showing relevant MethodDef table record:

Q: Do we still need the method name in the final output?
kennykerr commentedon Dec 10, 2022
If the
MethodDefrow ID is available, the name isn't needed since that can be looked up directly. Then it can simply be:KalleOlaviNiemitalo commentedon Dec 11, 2022
The methods of a class have to be consecutive, and the methods of a COM interface have to be in the vtable order; but would it be feasible to sort the methods of
Apisclasses by name, and perhaps add an attribute to indicate this was done? Projections could then do a binary search.Is it possible that a metadata assembly of an extension SDK will need to use RAIIFreeAttribute on a type of its own (or a parameter or a return value, if supported) but reference a freeing method in the Windows SDK metadata? In that case, the MethodDef row ID would not be reliable, as the version might not match exactly.
kennykerr commentedon Dec 12, 2022
Yes, a simpler approach may be to guarantee that the functions in the
Apisclass are sorted by name to allow a binary search. Then the following simpler attribute would be sufficient:riverar commentedon Dec 12, 2022
Don't think that'll be possible as metadata relies on .NET for the build up and emission of the winmd, and I don't think there are any ordering guarantees or knobs to tweak there. Will take a deeper look though!
mikebattista commentedon Mar 8, 2023
Assigning to you Rafael Rivera (@riverar) since it sounds like you've gotten pretty far with this already.
mikebattista commentedon May 10, 2023
At this point, all
AlsoUsableForandRAIIFreedefinitions are in the same namespace as the struct they're on except for:FreeLibraryinWindows.Win32.System.LibraryLoaderwhileHMODULE/HINSTANCEinWindows.Win32.FoundationLocal/GlobalFreeinWindows.Win32.System.MemorywhileHLOCAL/HGLOBALinWindows.Win32.FoundationShould we just move these
Freefunctions toWindows.Win32.Foundationand call it a day?Looking at the metadata now compared to 2 years ago when this was filed, I think it's fair to enforce that all
AlsoUsableForandRAIIFreerelationships must exist in the same namespace. A violation of that would be a bug in the metadata.kennykerr commentedon May 12, 2023
That's fine with me.
[AlsoUsableFor] and [RAIIFree]. Fixed microsoft#389.