コード例 #1
0
ファイル: x509ui.cs プロジェクト: dox0/DotNet471RS3
        private static void DisplayX509Certificate(SafeCertContextHandle safeCertContext, IntPtr hwndParent)
        {
            if (safeCertContext.IsInvalid)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_InvalidHandle"), "safeCertContext");
            }

            int dwErrorCode = CAPI.ERROR_SUCCESS;

            // Initialize view structure.
            CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW ViewInfo = new CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW();
            ViewInfo.dwSize             = (uint)Marshal.SizeOf(ViewInfo);
            ViewInfo.hwndParent         = hwndParent;
            ViewInfo.dwFlags            = 0;
            ViewInfo.szTitle            = null;
            ViewInfo.pCertContext       = safeCertContext.DangerousGetHandle();
            ViewInfo.rgszPurposes       = IntPtr.Zero;
            ViewInfo.cPurposes          = 0;
            ViewInfo.pCryptProviderData = IntPtr.Zero;
            ViewInfo.fpCryptProviderDataTrustedUsage = false;
            ViewInfo.idxSigner        = 0;
            ViewInfo.idxCert          = 0;
            ViewInfo.fCounterSigner   = false;
            ViewInfo.idxCounterSigner = 0;
            ViewInfo.cStores          = 0;
            ViewInfo.rghStores        = IntPtr.Zero;
            ViewInfo.cPropSheetPages  = 0;
            ViewInfo.rgPropSheetPages = IntPtr.Zero;
            ViewInfo.nStartPage       = 0;

            // View the certificate
            if (!CAPI.CryptUIDlgViewCertificateW(ViewInfo, IntPtr.Zero))
            {
                dwErrorCode = Marshal.GetLastWin32Error();
            }

            // CryptUIDlgViewCertificateW returns ERROR_CANCELLED if the user closes
            // the window through the x button or by pressing CANCEL, so ignore this error code
            if (dwErrorCode != CAPI.ERROR_SUCCESS && dwErrorCode != CAPI.ERROR_CANCELLED)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
        }