带有winscard.dll的.NET应用程序中的ERROR_INVALID_HANDLE

时间:2012-05-17 12:27:26

标签: .net windows 64-bit winscard

我正在创建一个.NET Windows服务来与智能卡读卡器进行通信。 maincomponent是一个名为winscard.dll的Windows DLL,我在.NET代码中使用DLLImport属性。在Windows XP 32位上一切正常,但是当我在 Windows 7 x64 上运行时,我会在调用 SCardTransmit 时重新接收 0x6 ERROR_INVALID_HANDLE 结果。应用程序(作为命令行应用程序的服务)可以连接到读卡器,但无法从卡中读取任何内容。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

你有 SCardEstablishContext 的问题,它会返回 0 ,但也会处理 0 。使用 IntPtr 代替整数

Public Declare Function SCardEstablishContext Lib "Winscard.dll" (ByVal dwScope As Integer, _
                                                                      ByVal pvReserved1 As Integer, _
                                                                      ByVal pvReserved2 As Integer, _
                                                                      ByRef phContext As IntPtr) As Integer

phContext将在Windows x64中存储有效值(通过RDP测试,Windows 8 x64和Windows 2008)。当用作句柄时,也将其他“Integer”替换为“IntPtr”。

Public Declare Function SCardConnect Lib "Winscard.dll" Alias "SCardConnectA" (ByVal hContext As IntPtr, _
                                                                                   ByVal szReaderName As String, _
                                                                                   ByVal dwShareMode As Integer, _
                                                                                   ByVal dwPrefProtocol As Integer, _
                                                                                   ByRef hCard As IntPtr, _
                                                                                   ByRef ActiveProtocol As IntPtr) As Integer