See the question and my original answer on StackOverflow

It should work, but it depends on your configuration. Also note CsWin32 has been greatly improved lately and you want to use the CsWin32RunAsBuildTask directive, so here's an example that works:

csproj: `

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <CsWin32RunAsBuildTask>true</CsWin32RunAsBuildTask>
    <DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
    <InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.242">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
</ItemGroup>
`

NativeMethods.txt:

ICertAdmin
CCertRequest
ICertRequest
ICertRequest2
ICertRequest3

Program.cs:

using System;
using System.Runtime.Versioning;
using Windows.Win32.Security.Cryptography.Certificates;

[assembly: SupportedOSPlatform("windows5.1.2600")]

namespace Test;

internal class Program
{
    static void Main()
    {
        var instance = CCertRequest.CreateInstance<ICertRequest>();
        Console.WriteLine(instance);
    }
}