ERROR_INVALID_HANDLE in .NET app with winscard.dll

4k views Asked by At

I'm creating a .NET Windows service to communicate with a smart card reader. The maincomponent is a Windows DLL called winscard.dll which I'm using with DLLImport attribute in the .NET code. Everything works fine on Windows XP 32 bit, but when i running it on Windows 7 x64 i recive 0x6 ERROR_INVALID_HANDLE result when calling SCardTransmit. The application (the service as command line app) can connect to the card reader but cannot read anything from the card.

Any idea?

1

There are 1 answers

2
Artem On BEST ANSWER

You have problems with SCardEstablishContext it will return 0 but handle also 0. Use IntPtr instead of Integer:

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 will be store valid value in Windows x64 (Windows 8 x64 and Windows 2008 via RDP tested). Also replace other "Integer" to "IntPtr" when it used as handle.

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