Opc.Ua.Win32.CertStrToNameW C# (CSharp) Method

CertStrToNameW() private method

private CertStrToNameW ( int dwCertEncodingType, [ pszX500, int dwStrType, IntPtr pvReserved, IntPtr pbEncoded, int &pcbEncoded, IntPtr ppszError ) : int
dwCertEncodingType int
pszX500 [
dwStrType int
pvReserved System.IntPtr
pbEncoded System.IntPtr
pcbEncoded int
ppszError System.IntPtr
return int
        public static extern int CertStrToNameW(
            int dwCertEncodingType,
            [MarshalAs(UnmanagedType.LPWStr)] string pszX500,
            int dwStrType,
            IntPtr pvReserved,
            IntPtr pbEncoded,
            ref int pcbEncoded,
            IntPtr ppszError);

Usage Example

Beispiel #1
0
        /// <summary>
        /// Encodes a CERT_NAME_BLOB
        /// </summary>
        public static void Encode_CERT_NAME_BLOB(string name, ref CERT_NAME_BLOB pName)
        {
            int    dwSize  = 0;
            IntPtr pBuffer = IntPtr.Zero;

            try
            {
                // reconstruct name using comma as delimeter.
                name = ChangeSubjectNameDelimiter(name, ',');

                int bResult = Win32.CertStrToNameW(
                    X509_ASN_ENCODING,
                    name,
                    CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    ref dwSize,
                    IntPtr.Zero);

                if (bResult == 0)
                {
                    throw GetLastError(StatusCodes.BadEncodingError, "Could not get size for CERT_X500_NAME_STR.");
                }

                pBuffer = Marshal.AllocHGlobal(dwSize);

                bResult = Win32.CertStrToNameW(
                    X509_ASN_ENCODING,
                    name,
                    CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
                    IntPtr.Zero,
                    pBuffer,
                    ref dwSize,
                    IntPtr.Zero);

                if (bResult == 0)
                {
                    throw GetLastError(StatusCodes.BadEncodingError, "Could not encode CERT_X500_NAME_STR.");
                }

                pName.pbData = pBuffer;
                pName.cbData = dwSize;
                pBuffer      = IntPtr.Zero;
            }
            finally
            {
                if (pBuffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pBuffer);
                }
            }
        }