-
Notifications
You must be signed in to change notification settings - Fork 96
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
Provide a static interface for the Guid on COM structs #752
Comments
cc: @lonitra |
Here is what I plan to add. It's not quite the same as what you were doing. Please advise if it deviates in an unacceptable way. internal interface IComIID
{
internal static abstract ref readonly Guid Guid { get; }
} |
We're passing Had a chat with @tannergooding to vet design. To be as safe as possible we need the public static struct IUnknown : IComIID
{
public static ref readonly Guid Guid
{
// Aggressive inlining needed to get this to inline as a single address move
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
// Little Endian- all Windows platforms
ReadOnlySpan<byte> data = new byte[] {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0xC0,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x46
};
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
} This ends up getting you this for asm:
And we can then write something like this: public unsafe static class IID
{
public static Guid* Get<T>() where T : unmanaged, IComIID
{
return (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in T.Guid));
}
} See Sharplab for an example. Note, that we would like the |
We do offer a public
Is a static field movable? I didn't expect that. |
Static fields do not have any guarantee of being immovable. As an implementation detail the JIT may decide to allocate the space for them on the FOH (Frozen Object Heap) or POH (Pinned Object Heap), but it is not required to do so. -- There are runtimes/GCs that may not do this optimization and scenarios like collectible assemblies where RyuJIT might not either. C# currently emits an RVA static (backing data is embedded in the |
This way, the reference can be converted by others into a pointer and passed around without fear that the data will move. See #752
This way, the reference can be converted by others into a pointer and passed around without fear that the data will move. See #752
We don't want to use reflection to get
Guid
information and also want to constrain generic helpers so in WinForms we implementINativeGuid
on every generated COM struct:Here is an example of how we use it: #751 (comment)
Here is another:
The text was updated successfully, but these errors were encountered: