本文介绍了如何使用.NET的P / Invoke CryptUIWizExport功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能翻译这两个cryptui.dll功能/结构成C#.NET [dllimport的]包装?我想的P / Invoke的CryptUIWizExport函数来显示Windows证书导出向导。特别是,我需要通过一个.NET x509证书作为一个参数到CryptUIWizExport功能。你的帮助是多少AP preciated!

CryptUIWizExport功能

  BOOL WINAPI CryptUIWizExport(
  一切变得DWORD dwFlags中,
  一切变得HWND hwndParent,
  一切变得LPCWSTR pwszWizardTitle,
  一切变得PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo,
  一切变得无效* PVOID
);

typedef结构_CRYPTUI_WIZ_EXPORT_INFO {
  DWORD的dwSize;
  LPCWSTR pwszExportFileName;
  DWORD dwSubjectChoice;
  联盟 {
    PCCERT_CONTEXT pCertContext;
    PCCTL_CONTEXT pCTLContext;
    PCCRL_CONTEXT pCRLContext;
    HCERTSTORE hCertStore;
  };
  DWORD cStores;
  HCERTSTORE * rghStores;
} CRYPTUI_WIZ_EXPORT_INFO,* PCRYPTUI_WIZ_EXPORT_INFO;
 

解决方案

您可以在不同的情况下使用 CryptUIWizExport 功能。这里有一个例子来导出证书。这个例子可以很容易修改为使用它在你需要它的情况。

 使用系统;
使用了System.Runtime.InteropServices;
使用System.Security.Cryptography.X509Certificates;

命名空间CryptUIWizExportTest {
    使用HCERTSTORE = IntPtr的;
    使用HWND = IntPtr的;

    静态内部类NativeMethods {
        内部枚举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)
        内部结构CRYPTUI_WIZ_EXPORT_INFO {
            内部UINT的dwSize;
            //要求:应设置为sizeof(CRYPTUI_WIZ_EXPORT_INFO)

            内部字符串pwszExportFileName;
            //必选如果CRYPTUI_WIZ_NO_UI标志设置,可选,否则。
            //完全限定的文件名导出到,如果这是
            //非NULL和CRYPTUI_WIZ_NO_UI标志没有设置,那么它是
            //显示给用户作为默认文件名

            内部CryptuiExportChoice dwSubjectChoice;
            //要求:指明对象的类型:
            //如果能执行下列操作之一:
            // 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

            内部IntPtr的pCertContext;
            //联盟
            // {
            // PCCERT_CONTEXT pCertContext;
            // PCCTL_CONTEXT pCTLContext;
            // PCCRL_CONTEXT pCRLContext;
            // HCERTSTORE hCertStore;
            //};

            内部UINT cStores;
            //可选:额外的门店数要搜索的的证书
            //信任链如果链正在导出一个证书。
            //这个被忽略,如果dwSubjectChoice是什么,其他
            //比CRYPTUI_WIZ_EXPORT_CERT_CONTEXT

            内部HCERTSTORE rghStores;
            // HCERTSTORE * !!!!
            //可选:阵列额外的存储搜索中的证书
            //信任链如果链正在导出一个证书。
            //这个被忽略,如果dwSubjectChoice是什么,其他
            //比CRYPTUI_WIZ_EXPORT_CERT_CONTEXT

        };

        [的DllImport(Cryptui.dll,字符集= CharSet.Uni code,
                    ExactSpelling = TRUE,SetLastError =真)
        [返回:的MarshalAs(UnmanagedType.Bool)
        内部静态外部布尔CryptUIWizExport(UINT dwFlags中,
            HWND hwndParent,串pwszWizardTitle,
            IntPtr的pExportInfo,IntPtr的PVOID);
    }
    类节目{
        静态无效的主要(字串[] args){
            X509Certificate2 X509 =新X509Certificate2(@C:\ Test.pfx​​,测试);
            如果(X509 == NULL)
                返回;

            NativeMethods.CRYPTUI_WIZ_EXPORT_INFO exportInfo =
                新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,假);
            NativeMethods.CryptUIWizExport(0,IntPtr.Zero,
                pExportInfo,IntPtr.Zero导出证书);
        }
    }
}
 

Can anyone translate these two cryptui.dll functions/structures into C#.NET [dllimport] wrappers? I would like to P/Invoke the CryptUIWizExport function to display the Windows Certificate Export Wizard. In particular, I need to pass a .NET X509Certificate as a parameter into the CryptUIWizExport function. You help is much appreciated!!!

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;
解决方案

You can use CryptUIWizExport function in different situations. Here is an example to export a certificate. This example can be easy modified for using it to the situations in which you need it.

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);
        }
    }
}

这篇关于如何使用.NET的P / Invoke CryptUIWizExport功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 06:28