CryptGetDefaultProvider

This function is used to determine the default cryptographic service provider (CSP) either for the current user or the machine for a given provider type. The name of the default CSP for the type specified in the dwProvType parameter is returned.

At a Glance

Header file: Wincrypt.h
Windows CE versions: 2.10 and later

Syntax

BOOL WINAPI CryptGetDefaultProvider( DWORD dwProvType,
DWORD *pdwReserved, DWORD dwFlags, LPTSTR pszProvName,
DWORD *pcbProvName );

Parameters

dwProvType

[in] Specifies the provider type of the CSP specified by the pszProvName parameter.

pdwReserved

[in] Reserved for future use; set to NULL.

dwFlags

[in] Specifies a bitmask of flags. It is one of the following values:

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

[out] Pointer to a buffer that receives the null-terminated string that contains the name of the default CSP.

This parameter can be NULL to set the size of the name for memory allocation purposes.

pcbProvName

[in/out] 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.

Note   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 ensure 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

TRUE indicates success. FALSE indicates failure. To get extended error information, call GetLastError. Common values for GetLastError are described in the following table. The error values prefaced by "NTE" are generated by the particular CSP you are using.

Value 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.

Windows CE does not support the ANSI version of this function.

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
...

See Also

CryptSetProviderEx, CryptSetProvider