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
);
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. |
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.
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.
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. |
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.
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
...
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.
CryptSetProviderEx, CryptSetProvider