大佬教程收集整理的这篇文章主要介绍了访问 VBA 和智能卡 - 诀窍是什么?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 windows 10/Access 2016 和 VBA 来玩智能卡,并且我遇到了几乎所有我能找到的关于如何实现这一点的示例,包括:
我遇到的问题是 @H_673_15@SCardEstablishContext 返回 @H_673_15@0x0 - SCARD_S_succesS,但是当我将 SCARDCOntexT 传递给 @H_673_15@SCardisValIDContext 时,它返回 @H_673_15@0x6 - ERROR_INVALID_HANDLE。
这是我正在使用的代码的相关部分(忽略 @H_673_15@AuthDict 和 @H_673_15@SCardAuthCode,因为它们只是帮助函数来解码其他函数返回的任何状态)
@H_673_15@Public AuthDict As ScripTing.Dictionary Public Const SCARD_ScopE_USER As Long = &H0 Public Const SCARD_ScopE_SYstem As Long = &H2 Public Const SCARD_SHARE_SHARED As Long = &H2 Public Const SCARD_SHARE_EXCLUSIVE As Long = &H1 Public Const SCARD_SHARE_DIRECT As Long = &H3 Public Const SCARD_PROTOCol_T0 As Long = &H1 Public Const SCARD_PROTOCol_T1 As Long = &H2 Public Const SCARD_DEFAulT_READERS As String = "SCard$DefaultReaders\000" Public Const SCARD_all_READERS As String = "SCard$AllReaders\000" Public Type SCARDCOntexT CardContext1 As Long Readername As String End Type Public Declare PtrSafe Function SCardEstablishContext lib "winscard.dll" ( _ ByVal DWScope As Long,_ ByVal pvReserved1 As Long,_ ByVal pvReserved2 As Long,_ ByRef phContext As SCARDCOntexT _ ) As Long Public Declare PtrSafe Function SCardisValIDContext lib "winscard.dll" ( _ ByRef hContext As SCARDCOntexT _ ) As Long Public Sub GetContext() Dim lreturn As Long Dim RSVD1 As Long,RSVD2 As Long Dim myContext As SCARDCOntexT Set AuthDict = New ScripTing.Dictionary DeBUG.Print "-----------------------------------------------------------------------------" lreturn = SCardEstablishContext(SCARD_ScopE_USER,RSVD1,RSVD2,myContext) SCardAuthCode lreturn DeBUG.Print "SCardEstablishContext:" & vbCrLf & _ " Return = " & AuthDict("hr") & vbCrLf & _ " Value = " & AuthDict("hc") & vbCrLf & _ " Description = " & AuthDict("hd") & vbCrLf & _ " myContext.CardContext1 = " & myContext.CardContext1 & vbCrLf & _ " myContext.Readername = " & chr(34) & myContext.Readername & chr(34) & vbCrLf lreturn = SCardisValIDContext(myContext) SCardAuthCode lreturn DeBUG.Print "SCardisValIDContext:" & vbCrLf & _ " Return = " & AuthDict("hr") & vbCrLf & _ " Value = " & AuthDict("hc") & vbCrLf & _ " Description = " & AuthDict("hd") & vbCrLf If lreturn <> 0 Then GoTo GetContextExit GetContextExit: DeBUG.Print "-----------------------------------------------------------------------------" & vbCrLf End Sub
运行 Sub,这是输出:
@H_673_15@----------------------------------------------------------------------------- SCardEstablishContext: Return = 0x00000000 Value = SCARD_S_succesS Description = No error was encountered. myContext.CardContext1 = -855572480 myContext.Readername = "" SCardisValIDContext: Return = 0x00000006 Value = ERROR_INVALID_HANDLE Description = The handle is invalID. -----------------------------------------------------------------------------
似乎我没有正确设置 @H_673_15@myContext,但我不知道它应该是什么样子。
此外,如果您想要漂亮的回报,这里是 @H_673_15@SCardAuthCode 的代码:
@H_673_15@'https://docs.microsoft.com/en-us/windows/win32/secauthn/authentication-return-values?redirectedfrom=MSDN#smart_card_return_values 'https://docs.microsoft.com/en-us/windows/win32/deBUG/system-error-codes--0-499-?redirectedfrom=MSDN Public Function SCardAuthCode(lreturn As Long) AuthDict.RemoveAll Dim hreturn As String,hc As String,hd As String hreturn = "0x" & Right("0000000" & Hex(lreturn),8) SELEct Case hreturn Case "0x00000000": hc = "SCARD_S_succesS": hd = "No error was encountered." Case "0x00000006": hc = "ERROR_INVALID_HANDLE": hd = "The handle is invalID." Case "0x00000109": hc = "ERROR_broKEN_PIPE": hd = "The clIEnt attempted a smart card operation in a remote session,such as a clIEnt session running on a terminal server,and the operaTing system in use does not support smart card redirection." Case "0x80100001": hc = "SCARD_F_INTERNAL_ERROR": hd = "An internal consistency check Failed." Case "0x80100002": hc = "SCARD_E_CANCELLED": hd = "The action was canceled by an SCardCancel request." Case "0x80100003": hc = "SCARD_E_INVALID_HANDLE": hd = "The supplIEd handle was not valID." Case "0x80100004": hc = "SCARD_E_INVALID_ParaMETER": hd = "One or more of the supplIEd parameters Could not be properly interpreted." Case "0x80100005": hc = "SCARD_E_INVALID_TARGET": hd = "Registry startup information is missing or not valID." Case "0x80100006": hc = "SCARD_E_NO_MEMORY": hd = "Not enough memory available to complete this command." Case "0x80100007": hc = "SCARD_F_WAITED_TOO_LONG": hd = "An internal consistency timer has expired." Case "0x80100008": hc = "SCARD_E_INSUFFICIENT_BUFFER": hd = "The data buffer for returned data is too small for the returned data." Case "0x80100009": hc = "SCARD_E_UNKNowN_READER": hd = "The specifIEd reader name is not recognized." Case "0x8010000A": hc = "SCARD_E_TIMEOUT": hd = "The user-specifIEd time-out value has expired." Case "0x8010000B": hc = "SCARD_E_SHARING_VIolATION": hd = "The smart card cAnnot be accessed because of other outstanding connections." Case "0x8010000C": hc = "SCARD_E_NO_smaRTCARD": hd = "The operation requires a smart card,but no smart card is currently in the device." Case "0x8010000D": hc = "SCARD_E_UNKNowN_CARD": hd = "The specifIEd smart card name is not recognized." Case "0x8010000E": hc = "SCARD_E_CANT_disPOSE": hd = "The system Could not dispose of the media in the requested mAnner." Case "0x8010000F": hc = "SCARD_E_PROTO_MIsmaTCH": hd = "The requested protocols are incompatible with the protocol currently in use with the card." Case "0x80100010": hc = "SCARD_E_NOT_READY": hd = "The reader or card is not ready to accept commands." Case "0x80100011": hc = "SCARD_E_INVALID_VALUE": hd = "One or more of the supplIEd parameter values Could not be properly interpreted." Case "0x80100012": hc = "SCARD_E_SYstem_CANCELLED": hd = "The action was canceled by the system,presumably to log off or shut down." Case "0x80100013": hc = "SCARD_F_COMM_ERROR": hd = "An internal communications error has been detected." Case "0x80100014": hc = "SCARD_F_UNKNowN_ERROR": hd = "An internal error has been detected,but the source is unkNown." Case "0x80100015": hc = "SCARD_E_INVALID_ATR": hd = "An ATR String obtained from the registry is not a valID ATR String." Case "0x80100016": hc = "SCARD_E_NOT_transaCTED": hd = "An attempt was made to end a nonexistent transaction." Case "0x80100017": hc = "SCARD_E_READER_UNAVAILABLE": hd = "The specifIEd reader is not currently available for use." Case "0x80100018": hc = "SCARD_P_SHUTDOWN": hd = "The operation has been aborted to allow the server application to exit." Case "0x80100019": hc = "SCARD_E_PCI_TOO_smaLL": hd = "The PCI receive buffer was too small." Case "0x8010001A": hc = "SCARD_E_READER_UNSUPPORTED": hd = "The reader driver does not meet minimal requirements for support." Case "0x8010001B": hc = "SCARD_E_DUPliCATE_READER": hd = "The reader driver dID not produce a unique reader name." Case "0x8010001C": hc = "SCARD_E_CARD_UNSUPPORTED": hd = "The smart card does not meet minimal requirements for support." Case "0x8010001D": hc = "SCARD_E_NO_serviCE": hd = "The smart card resource manager is not running." Case "0x8010001E": hc = "SCARD_E_serviCE_StopPED": hd = "The smart card resource manager has shut down." Case "0x8010001F": hc = "SCARD_E_UNEXPECTED": hd = "An unexpected card error has occurred." Case "0x80100020": hc = "SCARD_E_ICC_INSTALLATION": hd = "No priMary provIDer can be found for the smart card." Case "0x80100021": hc = "SCARD_E_ICC_CREATEORDER": hd = "The requested order of object creation is not supported." Case "0x80100022": hc = "SCARD_E_UNSUPPORTED_FEATURE": hd = "This smart card does not support the requested feature." Case "0x80100023": hc = "SCARD_E_DIR_NOT_FOUND": hd = "The specifIEd directory does not exist in the smart card." Case "0x80100024": hc = "SCARD_E_file_NOT_FOUND": hd = "The specifIEd file does not exist in the smart card." Case "0x80100025": hc = "SCARD_E_NO_DIR": hd = "The supplIEd path does not represent a smart card directory." Case "0x80100026": hc = "SCARD_E_NO_file": hd = "The supplIEd path does not represent a smart card file." Case "0x80100027": hc = "SCARD_E_NO_ACCESS": hd = "Access is denIEd to the file." Case "0x80100028": hc = "SCARD_E_WRITE_TOO_MANY": hd = "An attempt was made to write more data than would fit in the target object." Case "0x80100029": hc = "SCARD_E_BAD_SEEK": hd = "An error occurred in setTing the smart card file object pointer." Case "0x8010002A": hc = "SCARD_E_INVALID_CHV": hd = "The supplIEd PIN is incorrect." Case "0x8010002B": hc = "SCARD_E_UNKNowN_RES_mng": hd = "An unrecognized error code was returned." Case "0x8010002C": hc = "SCARD_E_NO_SUCH_CERTIFICATE": hd = "The requested certificate does not exist." Case "0x8010002D": hc = "SCARD_E_CERTIFICATE_UNAVAILABLE": hd = "The requested certificate Could not be obtained." Case "0x8010002E": hc = "SCARD_E_NO_READERS_AVAILABLE": hd = "No smart card reader is available." Case "0x8010002F": hc = "SCARD_E_COMM_DATA_LOST": hd = "A communications error with the smart card has been detected." Case "0x80100030": hc = "SCARD_E_NO_KEY_CONTAINER": hd = "The requested key container does not exist on the smart card." Case "0x80100031": hc = "SCARD_E_SERVER_TOO_BUSY": hd = "The smart card resource manager is too busy to complete this operation." Case "0x80100032": hc = "SCARD_E_PIN_CACHE_EXPIRED": hd = "The smart card PIN cache has expired." Case "0x80100033": hc = "SCARD_E_NO_PIN_CACHE": hd = "The smart card PIN cAnnot be cached." Case "0x80100034": hc = "SCARD_E_READ_ONLY_CARD": hd = "The smart card is read-only and cAnnot be written to." Case "0x80100065": hc = "SCARD_W_UNSUPPORTED_CARD": hd = "The reader cAnnot communicate with the card,due to ATR String configuration conflicts." Case "0x80100066": hc = "SCARD_W_UNRESPONSIVE_CARD": hd = "The smart card is not responding to a reset." Case "0x80100067": hc = "SCARD_W_UNPOWERED_CARD": hd = "Power has been removed from the smart card,so that further communication is not possible." Case "0x80100068": hc = "SCARD_W_reset_CARD": hd = "The smart card was reset." Case "0x80100069": hc = "SCARD_W_REMOVED_CARD": hd = "The smart card has been removed,so further communication is not possible." Case "0x8010006A": hc = "SCARD_W_Security_VIolATION": hd = "Access was denIEd because of a security violation." Case "0x8010006B": hc = "SCARD_W_WRONG_CHV": hd = "The card cAnnot be accessed because the wrong PIN was presented." Case "0x8010006C": hc = "SCARD_W_CHv_bLOCKED": hd = "The card cAnnot be accessed because the maximum number of PIN entry attempts has been reached." Case "0x8010006D": hc = "SCARD_W_EOF": hd = "The end of the smart card file has been reached." Case "0x8010006E": hc = "SCARD_W_CANCELLED_BY_USER": hd = "The action was canceled by the user." Case "0x8010006F": hc = "SCARD_W_CARD_NOT_AUTHENTICATED": hd = "No PIN was presented to the smart card." Case "0x80100070": hc = "SCARD_W_CACHE_ITEM_NOT_FOUND": hd = "The requested item Could not be found in the cache." Case "0x80100071": hc = "SCARD_W_CACHE_ITEM_STALE": hd = "The requested cache item is too old and was deleted from the cache." Case "0x80100072": hc = "SCARD_W_CACHE_ITEM_TOO_BIG": hd = "The new cache item exceeds the maximum per-item size defined for the cache." Case Else: hc = "UNKNowN VALUE": hd = "UnkNown value." End SELEct AuthDict.Add "hr",hreturn AuthDict.Add "hc",hc AuthDict.Add "hd",hd End Function
@H_673_15@SCARDCOntexT 不是类型,而是句柄。我不知道这种类型来自哪里。
为该句柄使用 @H_673_15@LongPtr。
@H_673_15@SCardEstablishContext 使用指向句柄的指针,所以 @H_673_15@ByRef
@H_673_15@Public Declare PtrSafe Function SCardEstablishContext Lib "winscard.dll" ( _ ByVal dwScope As Long,_ ByVal pvReserved1 As LongPtr,_ ByVal pvReserved2 As LongPtr,_ ByRef phContext As LongPtr_ ) As Long
请注意,两个 @H_673_15@pvReserved 也是指针,所以 @H_673_15@LongPtr,而不是 @H_673_15@Long。在彻底检查哪些 @H_673_15@PtrSafe 实际上应该是 @H_673_15@Long 之后,您才应该添加 @H_673_15@LongPtr。
虽然对于@H_673_15@SCardIsValidContext,句柄是直接传递的,所以@H_673_15@ByVal:
@H_673_15@Public Declare PtrSafe Function SCardIsValidContext Lib "winscard.dll" ( _ ByVal hContext As LongPtr _ ) As Long
然后,相应地调整您的代码,这应该是微不足道的。
当然,完成后不要忘记 @H_673_15@SCardReleaseContext,如果您打开上下文并且从不关闭它们,Windows 可能会变得烦躁。
以上是大佬教程为你收集整理的访问 VBA 和智能卡 - 诀窍是什么?全部内容,希望文章能够帮你解决访问 VBA 和智能卡 - 诀窍是什么?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。