CryptGetDefaultProvider

The CryptGetDefaultProvider function is used to determine the default CSP either for the current user or the machine for a given provider type. The name of the default CSP for the type given in the dwProvType parameter is returned.

#include <wincrypt.h>
BOOL WINAPI CryptGetDefaultProvider(
  DWORD dwProvType,   // in
  DWORD *pdwReserved, // in
  DWORD dwFlags,      // in
  LPTSTR pszProvName, // out
  DWORD *pcbProvName  // in/out
);
 

Parameters

dwProvType
The provider type of the CSP specified by the pszProvName parameter.
pdwReserved
This parameter is reserved for future use and must be NULL.
dwFlags
The flag values.
Flag value Description
CRYPT_MACHINE_DEFAULT Causes the machine default CSP of the given type to be returned.
CRYPT_USER_DEFAULT Causes the user default CSP of the given type to be returned.

pszProvName
Pointer to a buffer that receives the name of the default CSP. This is a string including the terminating NULL character.

This parameter can be NULL to set the size of the name for memory allocation purposes. For more information, see the "Common In/Out Parameter Conventions" section at the beginning of this Reference.

pcbProvName
Pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the pszProvName parameter. When the function returns, the variable pointed to by the pcbProvName parameter contains the number of bytes stored in the buffer. This parameter can be NULL, only if pszProvName is NULL.

Note that when processing the data returned in the buffer, applications need to use the actual size of the data returned. The actual size may be slightly smaller than the size of the buffer specified on input. (On input, buffer sizes are usually specified large enough to insure that the largest possible output data will fit 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 Values

If the function succeeds, the return value is TRUE. If it fails, the return value is FALSE. To retrieve extended error information, use the GetLastError function.

The following table lists the error codes most commonly returned by the GetLastError function. The error codes prefaced by "NTE" are generated by the particular CSP you are using.

Error code Description
ERROR_INVALID_PARAMETER One of the parameters contains an invalid value. This is most often an illegal pointer.
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 dwFlags has an unrecognized value.

Remarks

CryptGetDefaultProvider is used to determine which installed CSP is currently set as the default for the current user or the machine. This information will most often be used for display to the user.

Example

LPTSTR   pszName;
DWORD    cbName;

// Get the name of the default CSP specified for the PROV_RSA_SIG 
// type for the machine.
cbName = 0;
if (!CryptGetDefaultProvider(PROV_RSA_SIG, NULL, CRYPT_MACHINE_DEFAULT,
        NULL, &cbName)) 
   {printf("Error %x during CryptGetDefaultProvider!\n", GetLastError);
   return;
   }

if (NULL == (pszProvName = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, cbName))) 
   {printf("Error during memory allocation\n");
    return;
   }

if (!CryptGetDefaultProvider(PROV_RSA_SIG, NULL, CRYPT_MACHINE_DEFAULT,
        NULL, &cbName)) 
   {printf("Error %x during CryptGetDefaultProvider!\n", GetLastError);
    return;
   }

// Display this information to the user
...
 

QuickInfo

  Windows NT: Requires version 5.0 or later.
  Windows: Unsupported.
  Windows CE: Unsupported.
  Header: Declared in wincrypt.h.
  Import Library: Use advapi32.lib.
  Unicode: Defined as Unicode and ANSI prototypes.

See Also

CryptSetProviderEx, CryptSetProvider