The CPDestroyKey function releases the handle referenced by the hKey parameter. After a key handle has been released, it becomes invalid and cannot be used again.
BOOL CPDestroyKey(
HCRYPTPROV hProv, // in
HCRYPTKEY hKey // in
):
If the function succeeds, TRUE should be returned; otherwise, return FALSE. When FALSE is returned, the appropriate error code (see the following table) must be set via SetLastError.
Error | Description |
---|---|
NTE_BAD_KEY | The hKey parameter does not contain a valid handle to a key. |
NTE_BAD_UID | The hProv parameter does not contain a valid context handle. |
If the handle refers to a session key, or to a public key that has been imported into the CSP via CryptImportKey, this function destroys the key and frees the memory that the key occupied. The CSP will typically scrub the memory where the key was held before freeing it.
On the other hand, if the handle refers to a public/private key pair (obtained from CryptGetUserKey), the underlying key pair is not destroyed by this function. Only the handle is destroyed.
CPDeriveKey, CPGenKey, CPGetUserKey, CryptDestroyKey