Log in

View Full Version : How do use winscard.dll within VBA?



mikwilmot
10-01-2013, 05:55 AM
I am using Access 2010 with a SQL Server 2005 backend. I am using VBA to manipulate the code in SQL Server. Last week I was given a requirement to read a CAC( card inserted in an ActivIdentity card reader ), find the user who is logged in and then log into the SQL Server as that user. I am using winscard.dll to connect to the card reader.

The following is the declaration I am using:
'SCardEstablishContext
'LONG STDCALL SCardEstablishContext(DWORD(in), LPCVOID(in), LPCVOID(in),
' LPSCARDCONTEXT(out))
Public Declare Function SCardEstablishContext Lib "winscard.dll" ( _
ByVal dwScope As Long, _
ByVal pvReserved1 As Long, _
ByVal pvReserved2 As Long, _
ByRef phContext As Long _
) As Long
This is how I am calling the function:
...
Dim lParam1 As Long
Dim lParam4 As Long
Dim lReturn As Long

lParam1 = 1
lReturn = SCardEstablishContext(lParam1, 0, 1, lParam4)
...
The function does not error out, however, the lReturn is = "-2146435055"

QUESTIONS:
How do I set up SCardEstablishContext to run properly?
How do I set up lParam4 to hold all of the information after SCardEstablishContext runs correctly?
Thanks in advance,

mikwilmot
10-01-2013, 06:37 AM
I have changed the code slightly. However, the call to SCardEstablishContext fails with the same return "-2146435055".

Have a look.

Public Type SCARDCONTEXT
CardContext1 As Long
ReaderName As Byte
End Type

'SCardEstablishContext
'LONG STDCALL SCardEstablishContext(DWORD(in), LPCVOID(in), LPCVOID(in),
' LPSCARDCONTEXT(out))
Public Declare Function SCardEstablishContext Lib "winscard.dll" ( _
ByVal dwScope As Long, _
ByVal pvReserved1 As Long, _
ByVal pvReserved2 As Long, _
ByRef phContext As SCARDCONTEXT _
) As Long
....

Dim lParam1 As Long
Dim lParam4 As SCARDCONTEXT
Dim lReturn As Long

lParam1 = 1

lReturn = SCardEstablishContext(lParam1, 0, 0, lParam4)

....

Sven
02-25-2014, 11:45 AM
Did you ever make any progress with this project? Im in the same boat and just starting to go down this path. Looking to pull information from the CAC to validate user credentials.

Any help would be appreciated.

Thanks,
Sven