Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

CertGetNameString (crypt32)
 
.
Summary
The CertGetNameString function obtains the subject or issuer name from a certificate CERT_CONTEXT structure and converts it to a null-terminated character string.

C# Signature:

[DllImport("crypt32.dll", EntryPoint = "CertGetNameString", CharSet = CharSet.Auto, SetLastError = true)]
static extern UInt32 CertGetNameString(IntPtr CertContext, UInt32 lType, UInt32 lFlags, IntPtr pTypeParameter, System.Text.StringBuilder str, UInt32 cch);
static extern System.UInt32 CertGetNameString(IntPtr CertContext, System.UInt32 lType, System.UInt32 lFlags, IntPtr pTypeParameter, System.Text.StringBuilder str, System.UInt32 cch);

VB Signature:

Declare Function CertGetNameString Lib "Crypt32.dll" Alias "CertGetNameStringW" ( _
    ByVal pCertContext As Long, _
    ByVal dwType As Long, _
    ByVal dwFlags As Long, _
    pvTypePara As Any, _
    ByVal pszNameString As Long, _
    ByVal cchNameString As Long _
    ) As Long
Declare Function CertGetNameString Lib "crypt32.dll" (TODO) As TODO

User-Defined Types:

    const uint CERT_NAME_EMAIL_TYPE         = 1;
    const uint CERT_NAME_RDN_TYPE           = 2;
    const uint CERT_NAME_ATTR_TYPE          = 3;
    const uint CERT_NAME_SIMPLE_DISPLAY_TYPE    = 4;
    const uint CERT_NAME_FRIENDLY_DISPLAY_TYPE  = 5;
    const uint CERT_NAME_DNS_TYPE           = 6;
    const uint CERT_NAME_URL_TYPE           = 7;
    const uint CERT_NAME_UPN_TYPE           = 8;

    const uint CERT_NAME_ISSUER_FLAG        = 0x1;
    const uint CERT_NAME_DISABLE_IE4_UTF8_FLAG  = 0x00010000;

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

For VB usage:

Ensure you use StrPtr and that the function signature is "CertGetNameStringW", or you will regularly crash your program!

None.

Tips & Tricks:

You can call this first with a null buffer and null cchNameString to return just the required buffer length, then size the buffer appropriately and call again as above!

Please add some!

Sample Code:

C#:

        while ((certContext = CertEnumCertificatesInStore(hCertStore, certContext)) != IntPtr.Zero)
        {
        StringBuilder Buffer = new StringBuilder(255);
        UInt32 cchString = 255 ;
        System.UInt32 nChars = CertGetNameString(certContext,
                        CERT_NAME_FRIENDLY_DISPLAY_TYPE,
                        CERT_NAME_ISSUER_FLAG,
                        IntPtr.Zero,
                        Buffer,
                        cchString);
        // use Buffer.ToString() to use the cert's name string
        }

VB:

     Dim bfr As String, bPtr As Long
     bfr = String(szNameString, vbNullChar)
     bPtr = StrPtr(bfr)
     szNameString = CertGetNameString(hCert_Context&, _
                      CERT_NAME_SIMPLE_DISPLAY_TYPE, _
                      0&, _
                      0&, _
                      bPtr&, _
128& _                      )

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions