Dongle.GuidRVAGen
1.0.2
See the version list below for details.
dotnet add package Dongle.GuidRVAGen --version 1.0.2
NuGet\Install-Package Dongle.GuidRVAGen -Version 1.0.2
<PackageReference Include="Dongle.GuidRVAGen" Version="1.0.2" />
paket add Dongle.GuidRVAGen --version 1.0.2
#r "nuget: Dongle.GuidRVAGen, 1.0.2"
// Install Dongle.GuidRVAGen as a Cake Addin #addin nuget:?package=Dongle.GuidRVAGen&version=1.0.2 // Install Dongle.GuidRVAGen as a Cake Tool #tool nuget:?package=Dongle.GuidRVAGen&version=1.0.2
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!
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 1.1
- System.Memory (>= 4.5.5)
-
.NETStandard 2.0
- 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.