Quantcast
Channel: VBForums - API
Viewing all articles
Browse latest Browse all 168

Crypto API giving me unexplained problems.

$
0
0
Ok, so I have this program, just to test my ability to get the Crypto API started, and closed properly. And I'm already having troubles.

Here's my test code.
Code:

Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByRef hProv As Long, ByRef dwFlags As Long) As Long
Private Const PROV_RSA_FULL As Long = &H1
Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000

Private Sub Form_Load()
Dim hProv As Long
Dim RetVal As Long

RetVal = CryptAcquireContext(hProv, vbNullString, "Microsoft Base Cryptographic Provider v1.0", PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)
If RetVal = 0 Then Print "CryptAcquireContext Error: " & CStr(GetLastError)

Print "Crypto Provider Handle: " & CStr(hProv)

RetVal = CryptReleaseContext(hProv, 0)
If RetVal = 0 Then Print "CryptReleaseContext Error: " & CStr(GetLastError)
End Sub

Here's what I'm seeing when running the program.




If there is any error, it should be first relating to CryptAcquireContext, which results in an invalid Crypto Provider Handle (the value 0), which then results in another error when using that handle with CryptReleaseContext. What's happening is the program works PERFECTLY up to the part where there it tries to release that handle, and only THEN there is an error. Releasing something should not be the first time an error appears, since it IS a valid crypto handle, and it's being used for NOTHING ELSE. Therefore it shouldn't be a situation where it can't release it due to it using it for something else.

And the error it generates (error 87) corresponds to the error handle ERROR_INVALID_PARAMETER. This means that one of the parameters I gave it is incorrect. How can it be incorrect. The hProv (Provider Handle) is valid, as it was generated by CryptAcquireContext. And the other parameter for CryptReleaseContext (dwFlags) must be set to 0, as the MSDN page for the function says this about that parameter:
Quote:

Reserved for future use and must be zero. If dwFlags is not set to zero, this function returns FALSE but the CSP is released.
So I don't see why my code is failing. And since it's failing in the most basic parts of the Crypto API, I fear I may not be able to use it. Anyone who can help me here, please do so.

Viewing all articles
Browse latest Browse all 168

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>