gpt4 book ai didi

c# - 如何使用 .NET P/调用 CryptUIWizExport 函数

转载 作者:行者123 更新时间:2023-11-30 19:49:52 24 4
gpt4 key购买 nike

任何人都可以将这两个 cryptui.dll 函数/结构翻译成 C#.NET [dllimport] 包装器吗?我想 P/调用 CryptUIWizExport 函数来显示 Windows 证书导出向导。特别是,我需要将 .NET X509Certificate 作为参数传递给 CryptUIWizExport 函数。非常感谢您的帮助!!!

CryptUIWizExport function

BOOL WINAPI CryptUIWizExport(
__in DWORD dwFlags,
__in HWND hwndParent,
__in LPCWSTR pwszWizardTitle,
__in PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo,
__in void *pvoid
);

typedef struct _CRYPTUI_WIZ_EXPORT_INFO {
DWORD dwSize;
LPCWSTR pwszExportFileName;
DWORD dwSubjectChoice;
union {
PCCERT_CONTEXT pCertContext;
PCCTL_CONTEXT pCTLContext;
PCCRL_CONTEXT pCRLContext;
HCERTSTORE hCertStore;
} ;
DWORD cStores;
HCERTSTORE *rghStores;
} CRYPTUI_WIZ_EXPORT_INFO, *PCRYPTUI_WIZ_EXPORT_INFO;

最佳答案

您可以在不同的情况下使用CryptUIWizExport 函数。这是导出证书的示例。这个例子可以很容易地修改,以便在您需要它的情况下使用它。

using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;

namespace CryptUIWizExportTest {
using HCERTSTORE = IntPtr;
using HWND = IntPtr;

static internal class NativeMethods {
internal enum CryptuiExportChoice : uint {
CRYPTUI_WIZ_EXPORT_CERT_CONTEXT = 1,
CRYPTUI_WIZ_EXPORT_CTL_CONTEXT = 2,
CRYPTUI_WIZ_EXPORT_CRL_CONTEXT = 3,
CRYPTUI_WIZ_EXPORT_CERT_STORE = 4,
CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY = 5,
CRYPTUI_WIZ_EXPORT_FORMAT_CRL = 6,
CRYPTUI_WIZ_EXPORT_FORMAT_CTL = 7
}

[StructLayout (LayoutKind.Sequential)]
internal struct CRYPTUI_WIZ_EXPORT_INFO {
internal uint dwSize;
//Required: should be set to sizeof(CRYPTUI_WIZ_EXPORT_INFO)

internal string pwszExportFileName;
//Required if the CRYPTUI_WIZ_NO_UI flag is set, Optional otherwise.
//The fully qualified file name to export to, if this is
//non-NULL and the CRYPTUI_WIZ_NO_UI flag is NOT set, then it is
//displayed to the user as the default file name

internal CryptuiExportChoice dwSubjectChoice;
//Required: indicate the type of the subject:
// If can one of the following:
// CRYPTUI_WIZ_EXPORT_CERT_CONTEXT
// CRYPTUI_WIZ_EXPORT_CTL_CONTEXT
// CRYPTUI_WIZ_EXPORT_CRL_CONTEXT
// CRYPTUI_WIZ_EXPORT_CERT_STORE
// CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY

internal IntPtr pCertContext;
//union
//{
// PCCERT_CONTEXT pCertContext;
// PCCTL_CONTEXT pCTLContext;
// PCCRL_CONTEXT pCRLContext;
// HCERTSTORE hCertStore;
//};

internal uint cStores;
// Optional: count of extra stores to search for the certs in the
// trust chain if the chain is being exported with a cert.
// this is ignored if dwSubjectChoice is anything other
// than CRYPTUI_WIZ_EXPORT_CERT_CONTEXT

internal HCERTSTORE rghStores;
// HCERTSTORE* !!!!
// Optional: array of extra stores to search for the certs in the
// trust chain if the chain is being exported with a cert.
// this is ignored if dwSubjectChoice is anything other
// than CRYPTUI_WIZ_EXPORT_CERT_CONTEXT

};

[DllImport ("Cryptui.dll", CharSet = CharSet.Unicode,
ExactSpelling = true, SetLastError = true)]
[return: MarshalAs (UnmanagedType.Bool)]
internal static extern bool CryptUIWizExport (uint dwFlags,
HWND hwndParent, string pwszWizardTitle,
IntPtr pExportInfo, IntPtr pvoid);
}
class Program {
static void Main (string[] args) {
X509Certificate2 x509 = new X509Certificate2(@"c:\Test.pfx", "test");
if (x509 == null)
return;

NativeMethods.CRYPTUI_WIZ_EXPORT_INFO exportInfo =
new NativeMethods.CRYPTUI_WIZ_EXPORT_INFO ();
exportInfo.dwSize = (uint)Marshal.SizeOf (
typeof (NativeMethods.CRYPTUI_WIZ_EXPORT_INFO));
//exportInfo.pwszExportFileName = @"C:\TEMP\tt.cer";
exportInfo.dwSubjectChoice =
NativeMethods.CryptuiExportChoice.CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;
exportInfo.pCertContext = x509.Handle;
exportInfo.cStores = 0;

IntPtr pExportInfo = Marshal.AllocHGlobal ((int)exportInfo.dwSize);
Marshal.StructureToPtr (exportInfo, pExportInfo, false);
NativeMethods.CryptUIWizExport (0, IntPtr.Zero,
"Export of Certificate", pExportInfo, IntPtr.Zero);
}
}
}

关于c# - 如何使用 .NET P/调用 CryptUIWizExport 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3224117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com