I have the same problem

  • Thread starter jarod tupak via .NET 247
  • Start date
J

jarod tupak via .NET 247

(Type your message here)

I have the same problem when executing the code below. It returns me a "126" error from kernel32.

Dim sContainer as String = "sample_example_key"
Dim sProvider as String = "myprovider"
If Not (CryptAcquireContext(lHCryptprov, sContainer, sProvider, PROV_RSA_FULL, 0)) Then
MsgBox("Error " & GetLastError.ToString & " during CryptAcquireContext!")
End If

<DllImport("advapi32.dll")> _
Public Shared Function CryptAcquireContext( _
ByRef hProv As Integer, _
ByVal pszContainer As String, _
ByVal pszProvider As String, _
ByVal dwProvType As Integer, _
ByVal dwFlags As Integer) As Boolean
End Function

Private Declare Function GetLastError Lib "kernel32" () As Long
 
C

Chris Rolon

One note: you should declare the Win32 method to get the last win32 error as
follows

<DllImport("advapi32.dll", SetLasetError := true)> _
Public Shared Function CryptAcquireContext( _
ByRef hProv As Integer, _
ByVal pszContainer As String, _
ByVal pszProvider As String, _
ByVal dwProvType As Integer, _
ByVal dwFlags As Integer) As Boolean
End Function

If false is returned, you then call
System.Runtime.InteropServices.GetLastWin32Error() to get the error code.
This will ensure that you get the correct error.

One more thing--why don't you use the managed classes for crypto? They are
much easier to use.

Chris Rolon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top