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

CryptEnumProviders (advapi32)
 
.
Summary
TODO - a short description

C# Signature:

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool CryptEnumProviders(
    [MarshalAs(UnmanagedType.U4)] int dwIndex,
    [MarshalAs(UnmanagedType.U4)] ref int pdwReserved,
    [MarshalAs(UnmanagedType.U4)] int dwFlags,
    [MarshalAs(UnmanagedType.U4)] ref int pdwProvType,
    StringBuilder pszProvName,
    [MarshalAs(UnmanagedType.U4)] ref int pcbProvName);

Another kind of declaration:

[DllImport("advapi32.dll", EntryPoint = "CryptEnumProviders", SetLastError = true)]

[DllImport("advapi32.dll", EntryPoint = "CryptEnumProviders", SetLastError = true)]

static extern bool CryptEnumProviders(
    uint dwIndex,
    IntPtr pdwReserved,
    uint dwFlags,
    ref uint pdwProvType,
    StringBuilder pszProvName,
    ref uint pcbProvName);

VB Signature:

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

[DllImport("Advapi32.dll")]
internal static extern bool CryptEnumProviders(
    int dwIndex,
    IntPtr pdwReserved,
    int dwFlags,
    ref int pdwProvType,
    StringBuilder pszProvName,
    ref int pcbProvName);

public static Dictionary<string, int> ReadAllProviders()
{
    Dictionary<string, int> installedCSPs = new Dictionary<string, int>();
    int cbName;
    int dwType;
    int dwIndex;
    StringBuilder pszName;
    dwIndex = 0;
    dwType = 1;
    cbName = 0;
    while (CryptEnumProviders(dwIndex, IntPtr.Zero, 0, ref dwType, null, ref cbName))
    {
        pszName = new StringBuilder(cbName);

        if (CryptEnumProviders(dwIndex++, IntPtr.Zero, 0, ref dwType, pszName, ref cbName))
        {
            installedCSPs.Add(pszName.ToString(),dwType);
        }
    }
    return installedCSPs;
}

Sample PowerShell Code:

function Get-CryptoServiceProvider {
$TypeDefinition=@"
using System.Runtime.InteropServices;
using System.Security;

namespace PKI {

     public static class CSP {
     [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]
         public static extern bool CryptEnumProviders(
         uint dwIndex,
         uint pdwReserved,
         uint dwFlags,
         ref uint pdwProvType,
         System.Text.StringBuilder pszProvName,
         ref uint pcbProvName);

     [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]
         public static extern bool CryptEnumProviderTypes(
         uint dwIndex,
         uint pdwReserved,
         uint dwFlags,
         ref uint pdwProvType,
         System.Text.StringBuilder pszTypeName,
         ref uint pcbTypeName);
     }
}
"@

     Add-Type -TypeDefinition $TypeDefinition -PassThru
     $CSP=@()
     $ProvTypes = @{}
#region default value definition for provider types
     [UInt32]$dwIndex = 0
     [UInt32]$pdwProvType = 0
     $pszTypeName = New-Object Text.StringBuilder
     [UInt32]$pcbTypeName = 0
#endregion

     # retrieve provider type enumeration to a hashtable
     while ([PKI.CSP]::CryptEnumProviderTypes($dwIndex,0,0,[ref]$pdwProvType,$null,[ref]$pcbTypeName)) {
     $pszTypeName.Length = $pcbTypeName
     if ([PKI.CSP]::CryptEnumProviderTypes($dwIndex++,0,0,[ref]$pdwProvType,$pszTypeName,[ref]$pcbTypeName)) {
         $Type = $pszTypeName.TosTring()
         $ProvTypes.Add($pdwProvType,$Type)
     }
     }

#region default value definition for providers
     [UInt32]$dwIndex = 0
     [UInt32]$pdwProvType = 0
     $pszProvName = New-Object Text.Stringbuilder
     [UInt32]$pcbProvName = 0
#endregion

     # retrieve providers
     while ([PKI.CSP]::CryptEnumProviders($dwIndex,0,0,[ref]$pdwProvType,$null,[ref]$pcbProvName)) {
     $pszProvName.Length = $pcbProvName
     if ([PKI.CSP]::CryptEnumProviders($dwIndex++,0,0,[ref]$pdwProvType,$pszProvName,[ref]$pcbProvName)) {
         $objCSP=New-Object System.Object
         $objCSP | Add-Member -NotePropertyName "Name" -NotePropertyValue $pszProvName.ToString()
         $objCSP | Add-Member -NotePropertyName "TypeNum" -NotePropertyValue $pdwProvType
         $objCSP | Add-Member -NotePropertyName "TypeName" -NotePropertyValue $ProvTypes[$pdwProvType]
         $CSP+=$objCSP
     }
     }
     $CSP | Format-Table -AutoSize
}

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