Dongle.GuidRVAGen
1.0.4
See the version list below for details.
dotnet add package Dongle.GuidRVAGen --version 1.0.4
NuGet\Install-Package Dongle.GuidRVAGen -Version 1.0.4
<PackageReference Include="Dongle.GuidRVAGen" Version="1.0.4" />
paket add Dongle.GuidRVAGen --version 1.0.4
#r "nuget: Dongle.GuidRVAGen, 1.0.4"
// Install Dongle.GuidRVAGen as a Cake Addin #addin nuget:?package=Dongle.GuidRVAGen&version=1.0.4 // Install Dongle.GuidRVAGen as a Cake Tool #tool nuget:?package=Dongle.GuidRVAGen&version=1.0.4
GuidRVAGen
A source generator that generates RVAs for GUIDs using a GuidAttribute
equivalent.
Purpose
Typically, static GUIDs are defined using static readonly
fields, like below:
static readonly Guid IID_IUnknown = new Guid(0x00000000, 0x0000, 0x0000, 0xc000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
While this is a fine solution, it can be a bit more cumbersome to use in case of doing lower-level COM interop (which is required if you want to use CsWin32 with Native AOT).
Under CsWin32's "disable-marshalling" mode (which must be used for AOT compilation), it generates a QueryInterface
method which takes a Guid*
:
HRESULT QueryInterface(Guid* riid, void** ppv);
This means to use it with static readonly Guid
, you would have to copy it to another variable:
IUnknown* comObject;
IUnknown* queriedObject;
Guid guid = IID_IUnknown;
Marshal.ThrowExceptionForHR(comObject->QueryInterface(&guid, (void**)(&queriedObject)));
Or pin it yourself:
IUnknown* comObject;
IUnknown* queriedObject;
fixed (Guid* pGuid = IID_IUnknown)
{
Marshal.ThrowExceptionForHR(comObject->QueryInterface(pGuid, (void**)(&queriedObject)));
}
Or use Marshal.QueryInterface
, which even though gives an idiomatic way to pass in the field using ref
, requires a lot of casting:
IUnknown* comObject;
IUnknown* queriedObject;
Marshal.ThrowExceptionForHR(Marshal.QueryInterface((nint)comObject, ref IUnknown, out nint pQueriedObject);
queriedObject = (IUnknown*)pQueriedObject;
This is not to mention that having lots of static readonly
fields can impact performance, due to the fact that they have to be initialized all at once.
GuidRVAGen helps resolve those problems for you!
Usage
To start with, you will need the following:
- .NET 9 SDK Preview 6 or newer
[!NOTE]
You don't need to use the .NET 9 target framework itself. You only need to have the SDK installed.
- Visual Studio 2022, version 17.12 or newer (or another IDE with .NET 9 Preview 6 support or newer)
- The C# version to be set to 13.0 or newer
- You don't need to change it in .NET 9 or newer unless you have explicitly changed the
<LangVersion>
MSBuild property. For older targets, add the following line to one of your<PropertyGroup>
:<LangVersion>13.0</LangVersion>
- You don't need to change it in .NET 9 or newer unless you have explicitly changed the
Then, install the NuGet package and add a class, like the following:
namespace YourNamespace;
public static unsafe partial class IID
{
[GuidRVAGen.Guid("00000000-0000-0000-c000-000000000046")]
public static partial Guid* IID_IUnknown { get; }
}
[!WARNING]
The
partial
keyword is critical. Make sure you set it to the property, as long with any types that the property resides in (in this case, theIID
class).
[!CAUTION]
Do NOT change the value of the
Guid*
that the property returns! Doing so can cause crashes, memory corruption, etc.
As you can see, we now have a Guid*
property that we can now use:
IUnknown* comObject;
IUnknown* queriedObject;
Marshal.ThrowExceptionForHR(comObject->QueryInterface(IID.IID_IUnknown, (void**)(&queriedObject)));
If you need a ref
or ref readonly
return value instead, you can also do that!
namespace YourNamespace;
public static unsafe partial class IID
{
[GuidRVAGen.Guid("00000000-0000-0000-c000-000000000046")]
public static partial ref readonly Guid IID_IUnknown { get; }
}
namespace YourNamespace;
public static unsafe partial class IID
{
[GuidRVAGen.Guid("00000000-0000-0000-c000-000000000046")]
public static partial ref Guid IID_IUnknown { get; }
}
[!CAUTION]
Similar to the
Guid*
return type, do NOT change the value of theref
returned!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.1 is compatible. netstandard1.2 was computed. netstandard1.3 was computed. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 was computed. netstandard2.1 was computed. |
.NET Framework | net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Windows Phone | wpa81 was computed. |
Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 1.1
- System.Memory (>= 4.5.5)
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.