1

下面的代码曾经工作。我认为它在一些 .NET Framework 或 Windows 10 升级后坏了,但不确定。

它会打开一个临时证书存储以生成自签名证书。

using System;
using System.Runtime.InteropServices;

namespace MyTestApp
{
    internal static class Program
    {
        public static void Main(string[] arguments)
        {
            var certStore = CertOpenStore(
                "Memory", // sz_CERT_STORE_PROV_MEMORY
                0,
                IntPtr.Zero,
                CERT_STORE_CREATE_NEW_FLAG,
                IntPtr.Zero
            );

            if (certStore != IntPtr.Zero)
            {
                Console.WriteLine("Success!");
            }
            else
            {
                var error = Marshal.GetHRForLastWin32Error();
                Console.WriteLine("Error: " + error.ToString("X"));
            }

            Console.ReadLine();
        }

        private const uint CERT_STORE_CREATE_NEW_FLAG = 0x00002000;

        [DllImport("Crypt32.dll", SetLastError = true, CharSet=CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr CertOpenStore(
            [MarshalAs(UnmanagedType.LPWStr)] string storeProvider,
            uint messageAndCertificateEncodingType,
            IntPtr cryptProvHandle,
            uint flags,
            IntPtr parameters
        );

    }
}

输出是:

Error: 80070002

这是ERROR_FILE_NOT_FOUND。在 .NET4.54.5.2.

我可以做些什么来进一步诊断或修复?

4

0 回答 0