CryptReleaseContext 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 CryptReleaseContext function releases the handle of a cryptographic service provider (CSP) and a key container. At each call to this function, the reference count on the CSP is reduced by one. When the reference count reaches zero, the context is fully released and it can no longer be used by any function in the application.

An application calls this function after finishing the use of the CSP. After this function is called, the released CSP handle is no longer valid. This function does not destroy key containers or key pairs.


BOOL CryptReleaseContext(
  [in] HCRYPTPROV hProv,
  [in] DWORD      dwFlags


[in] hProv

Handle of a cryptographic service provider (CSP) created by a call to CryptAcquireContext.

[in] dwFlags

Reserved for future use and must be zero. If dwFlags is not set to zero, this function returns FALSE but the CSP is released.

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. Some possible error codes are listed in the following table.

Return code Description
The CSP context specified by hProv is currently being used by another process.
One of the parameters specifies a handle that is not valid.
One of the parameters contains a value that is not valid. This is most often a pointer that is not valid.
The dwFlags parameter is nonzero.
The hProv parameter does not contain a valid context handle.


After this function has been called, the CSP session is finished and all existing session keys and hash objects created by using the hProv handle are no longer valid. In practice, all of these objects should be destroyed with calls to CryptDestroyKey and CryptDestroyHash before CryptReleaseContext is called.


For an example that uses this function, see Example C Program: Creating and Hashing a Session Key.


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

See also




Service Provider Functions