CryptGetDefaultProviderA function (wincrypt.h)

Important  This API is deprecated. New and existing software should start using Cryptography Next Generation APIs. Microsoft may remove this API in future releases.
 
The CryptGetDefaultProvider function finds the default cryptographic service provider (CSP) of a specified provider type for the local computer or current user. The name of the default CSP for the provider type specified in the dwProvType parameter is returned in the pszProvName buffer.

Syntax

BOOL CryptGetDefaultProviderA(
  [in]      DWORD dwProvType,
  [in]      DWORD *pdwReserved,
  [in]      DWORD dwFlags,
  [out]     LPSTR pszProvName,
  [in, out] DWORD *pcbProvName
);

Parameters

[in] dwProvType

The provider type for which the default CSP name is to be found.

Defined provider types are as follows:

[in] pdwReserved

This parameter is reserved for future use and must be NULL.

[in] dwFlags

The following flag values are defined.

Value Meaning
CRYPT_USER_DEFAULT
0x00000002
Returns the user-context default CSP of the specified type.
CRYPT_MACHINE_DEFAULT
0x00000001
Returns the computer default CSP of the specified type.

[out] pszProvName

A pointer to a null-terminated character string buffer to receive the name of the default CSP.

To find the size of the buffer for memory allocation purposes, this parameter can be NULL. For more information, see Retrieving Data of Unknown Length.

[in, out] pcbProvName

A pointer to a DWORD value that specifies the size, in bytes, of the buffer pointed to by the pszProvName parameter. When the function returns, the DWORD value contains the number of bytes stored or to be stored in the buffer.

Note  When processing the data returned in the buffer, applications must use the actual size of the data returned. The actual size can be slightly smaller than the size of the buffer specified on input. (On input, buffer sizes are usually specified large enough to ensure that the largest possible output data fits in the buffer.) On output, the variable pointed to by this parameter is updated to reflect the actual size of the data copied to the buffer.
 

Return value

If the function succeeds, the return value is nonzero (TRUE).

If the function fails, the return value is zero (FALSE). For extended error information, call GetLastError.

The error code prefaced by NTE is generated by the particular CSP being used. Possible error codes include the following.

Return code Description
ERROR_INVALID_PARAMETER
One of the parameters contains a value that is not valid. This is most often a pointer that is not valid.
ERROR_MORE_DATA
The buffer for the name is not large enough.
ERROR_NOT_ENOUGH_MEMORY
The operating system ran out of memory.
NTE_BAD_FLAGS
The dwFlags parameter has an unrecognized value.

Remarks

This function determines which installed CSP is currently set as the default for the local computer or current user. This information is often displayed to the user.

Examples

The following example retrieves the name of the default CSP for the PROV_RSA_FULL provider type. For another example that uses this function, see Example C Program: Enumerating CSP Providers and Provider Types.

#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#pragma comment(lib, "crypt32.lib")

void main()
{

    DWORD       cbProvName=0;
    LPTSTR      pbProvName=NULL;
    // Copyright (C) Microsoft.  All rights reserved.
    // Get the length of the RSA_FULL default provider name.
    if (!(CryptGetDefaultProvider(
         PROV_RSA_FULL, 
         NULL, 
         CRYPT_MACHINE_DEFAULT,
         NULL, 
         &cbProvName))) 
    { 
      printf("Error getting the length of the default "
          "provider name.\n");
      exit(1);
    }

    // Allocate local memory for the name of the default provider.
    if (!(pbProvName = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, 
        cbProvName)))
    {
        printf("Error during memory allocation for "
            "provider name.\n");
        exit(1);
    }

    // Get the default provider name.
    if (CryptGetDefaultProvider(
        PROV_RSA_FULL, 
        NULL, 
        CRYPT_MACHINE_DEFAULT,
        pbProvName,
        &cbProvName)) 
    {
        printf("The default provider name is %s\n",pbProvName);
    }
    else
    {
        printf("Getting the name of the provider failed.\n");
        exit(1);
    }

    // Free resources when done.
    LocalFree(pbProvName);

}

Note

The wincrypt.h header defines CryptGetDefaultProvider as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.

Requirements

Requirement Value
Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header wincrypt.h
Library Advapi32.lib
DLL Advapi32.dll

See also

CryptSetProvider

CryptSetProviderEx

Service Provider Functions