locked
Help with SCardConnect for Vista 64 Bit RRS feed

  • Question

  •  

    Hi guys help please...I've developed a simple application that can establish context & list reader available but one thing i cant make to work is to connect. Everytime I execute the command SCardConnect it returns a value of 6 which is not the value for SCARD_S_SUCCESS the return should be 0. But when i compile it on XP 32bit it work smothly..Any idea guys on how to solve this? by the way, im using C#2005. Thanks in advance!

     

     

    Code Snippet

    private void btnEstablishContext_Click(object sender, EventArgs e)

    {

    retCode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, ref hContext);

    if (retCode != ModWinsCard.SCARD_S_SUCCESS)

    {

    DisplayOut(ModWinsCard.GetScardErrMsg(retCode));

    return;

    }

    DisplayOut("Establish context success.");

    btnReaderList.Enabled = true;

    }

     

     

    private void btnReaderList_Click(object sender, EventArgs e)

    {

    sReaderList = string.Empty;

    ReaderCount = 255;

    cmboReader.Items.Clear();

    byte[] retData;

    string readerStr = string.Empty;

    //The SCardListReaders function provides the list of readers within a set of named reader groups, eliminating duplicates.

    //ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/secauthn/security/scardlistreaders.htm

    //Get the number of reader found and store it to ReaderCount

    retCode = ModWinsCard.SCardListReaders(hContext, null, null, ref ReaderCount);

    if (retCode != ModWinsCard.SCARD_S_SUCCESS)

    {

    DisplayOut(ModWinsCard.GetScardErrMsg(retCode));

    return;

    }

    retData = new byte[ReaderCount];

    //Get the list of reader present again but this time add sReaderGroup, retData as 2rd & 3rd parameter respectively.

    retCode = ModWinsCard.SCardListReaders(hContext, sReaderGroup, retData, ref ReaderCount);

    if (retCode != ModWinsCard.SCARD_S_SUCCESS)

    {

    DisplayOut(ModWinsCard.GetScardErrMsg(retCode));

    return;

    }

    //Convert retData(Hexadecimal) value to String

    readerStr = System.Text.ASCIIEncoding.ASCII.GetString(retData);

    //Add the readers from readerStr to combobox control

    ModWinsCard.LoadListControl(cmboReader, readerStr);

    //Select 1st item from the combobox if any

    if (cmboReader.Items.Count > 0)

    cmboReader.SelectedIndex = 0;

    //Enable Connect button

    if (cmboReader.Items.Count > 0)

    btnConnect.Enabled = true;

    }

    private void btnConnect_Click(object sender, EventArgs e)

    {

    //establishes a connection (using a specific resource manager context)

    //between the calling application and a smart card contained by a specific reader.

    //If no card exists in the specified reader, an error is returned.

    //For more info:

    //ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/secauthn/security/scardconnect.htm if you have MSDN 2005 installed

    retCode = ModWinsCard.SCardConnect(hContext, cmboReader.SelectedItem.ToString(), ModWinsCard.SCARD_SHARE_EXCLUSIVE,

    ModWinsCard.SCARD_PROTOCOL_T0 | ModWinsCard.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);

    if (retCode != ModWinsCard.SCARD_S_SUCCESS)

    DisplayOut("Not Connected: " + ModWinsCard.GetScardErrMsg(retCode));

    else

    {

    DisplayOut("SCardConnect...OK");

    btnReset.Enabled = true;

    btnSetRetryTime.Enabled = true;

    }

    }

     

     

    Thursday, March 27, 2008 4:41 AM

All replies

  • Having exactly the same problem. Seem to be a Vista problem, strange that no one at microsoft responds to this...
    Thursday, May 8, 2008 2:06 PM
  • You need to build your project for Win32 platform. than it will work.
    Wednesday, September 16, 2009 6:58 AM
  • Hi,

    I think the problem is related to the PC/SC reader and its 64-bit drivers. Personally, I have been using smart card enabled 64-bit applications on Vista 64-bit for more that an year now and everything is working properly. The readers I'm using are Omnikey 3021, Omnikey 5321 and GemPC Twin.

    On what type of readers did you noticed the problem?

    Cheers,
    --
    Mounir IDRASSI
    IDRIX
    http://www.idrix.fr
    Sunday, September 20, 2009 3:40 PM
  • Hi,

    I think the problem is related to the PC/SC reader and its 64-bit drivers. Personally, I have been using smart card enabled 64-bit applications on Vista 64-bit for more that an year now and everything is working properly. The readers I'm using are Omnikey 3021, Omnikey 5321 and GemPC Twin.

    On what type of readers did you noticed the problem?

    Cheers,
    --
    Mounir IDRASSI
    IDRIX
    http://www.idrix.fr

    I thought that up until about an hour ago. My VB2008 smart card application has been running perfectly under Windows 7 32-bit, and any other 32-bit OS before it. But with Windows 7 64-bit, I was receiving exactly the same lResult=6 problem as above. I had come to the conclusion that it was the drivers, and had contacted the vendor. They told me they didn't know, and to ask Microsoft.

    The reader? An Omnikey 5321 - which you know works OK - and which, courtesy of a free EMV Explorer (feed it a credit or debit card or something) application from http://www.soliatis.com/index.php?page=emv_tools&path=_, I am also now aware functions correctly. It's worth downloading, even just to eliminate potential causes of the problem. I can vouch that it does work under Win 7 x64, and probably under Vista 64-bit also.

    So in my case, it has to be a code issue. I'm almost glad to see people having the same issue with this under C#, as I was becoming convinced that it was something to do with my handling of the ReaderName string. But I am sure that is OK.
    Cheers, John
    Tuesday, October 6, 2009 1:22 PM
  • Well, I've discovered a little bit more now, about what the problem could be. I haven't fixed it totally, but I have discovered that the problem is likely to be before ScardConnect.

    On my machine, I have a PCMCIA card reader, permanently installed, and I also have the Omnikey 5321 reader, installed, but not always connected.

    Using my original code, I discovered that even when the Omnikey was disconnected, my ScardListReaders call was still showing it as present. This was NOT the case with the Soliatis tool I mention above. So I began to look at all of my call parameters. I have changed my hCard and hContext declarations from Integer to Int64. And suddenly the ScardListReader command DOES now list only those readers that are actually connected. So that's progress. I've yet to follow this downstream to the ScardConnect call, but it might give others a pointer as to what is wrong.

    I'll post back when I finally get through to ScardConnect - and probably ask for help as to how to get it running using 32-bit Windows again! ;-)

    Cheers

    JW
    Cheers, John
    Tuesday, October 13, 2009 9:46 AM
  • This has managed wrappers for smart card functionality - fyi - http://msdn.microsoft.com/en-us/magazine/cc163521.aspx. I reference it only because I found a different interop approach to be more convenient: provide a native wrapper around SCardUIDlgSelectCard and expose the wrapper to managed code.
    Tuesday, October 13, 2009 9:24 PM
  • I'll post back when I finally get through to ScardConnect - and probably ask for help as to how to get it running using 32-bit Windows again! ;-)

    As I predicted, getting the app to work in 64-bit then caused it to fail in 32-bit. Anyway, thanks to some help on the MS Dotnet newsgroup, I now have a solution, in that my lResult is now defined as LONG, but hCard and hContext are now defined as INTPTR, which selects the correct definition depending on the machine it is being run on!

    JW
    Cheers, John
    • Proposed as answer by j_dublevay Friday, October 16, 2009 4:28 PM
    Friday, October 16, 2009 4:26 PM
  • This has managed wrappers for smart card functionality - fyi - http://msdn.microsoft.com/en-us/magazine/cc163521.aspx. I reference it only because I found a different interop approach to be more convenient: provide a native wrapper around SCardUIDlgSelectCard and expose the wrapper to managed code.

    I see just a header on the page - no content! :-(
    Cheers, John
    Friday, October 16, 2009 4:27 PM
  • It still loads for me. Here's the direct link to the code, if that helps: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/SmartCards2006_11.exe.

    Anyway, glad you found a solution.

    Friday, October 16, 2009 4:46 PM
  • Thanks Dan - I got it with the direct link.

    It's going to take me a while to get my head around it though, as I'm a VB person. But then again, I've been looking for an excuse/interesting route to play with C#! ;-)

    Cheers

    JW
    Cheers, John
    Sunday, October 18, 2009 9:40 PM
  • Hi daimous:

    How is the situation on your side?
    Is this thread solved or NOT?

    Please tell me!

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Friday, November 13, 2009 7:52 PM
  • Hi daimous:

    How is the situation on your side?
    Is this thread solved or NOT?

    Please tell me!

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Thursday, November 26, 2009 12:36 PM
  • Hi again:

    How is the situation on your side?
    Is this thread solved?

    Please tell me!

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Sunday, December 27, 2009 9:40 AM
  • Hi again:

    How is the situation on your side?
    Is this thread solved?

    Please tell me!

    Have a nice day...

    Best regards,
    Fisnik

    Coder24.com
    Saturday, January 2, 2010 3:01 PM
  • Have you tried to change  the project CPU setting to x86?   I had a issue with smartcards in a project being developed in vista x64.  The reader was not detected anyhow.  The setting "Any CPU" didn't work, but x86 did!!!
    • Proposed as answer by JakobB Monday, April 16, 2012 2:32 PM
    Wednesday, February 3, 2010 3:57 PM
  • Hello,

     

    I am encountering the same problem with the same code from ACS, and I am on Windows 7 system. I tried the options suggested by you but its not working for me. Do you have any other solution ? can you please send me the modification you made in the existing code ?

    Although I email to the vendor , and I am waiting reply from him.

    Meanwhile if you have time then pls. help.

     

    A+

    Michel

    • Proposed as answer by yvenumadhav Wednesday, April 4, 2012 5:31 AM
    • Unproposed as answer by yvenumadhav Wednesday, April 4, 2012 5:31 AM
    Monday, December 19, 2011 10:56 PM
  • The return code 6 mean INVALID HANDLE. Please check whether you are using 64-bit Windows. If yes, the problem is might be because you are still using 32 bits in DLL Marshalling. The solution is to change all the handle and pointer from int32 to int64 in the DLL Marshalling code, and make sure you are using int64 for all pointer and handle in all your code.

    I have the same problem, and solved by changing all pointer type to long (64 bits) in C#.

    Sunday, April 15, 2012 10:40 AM
  • Thanks a lot!! This solved it for me. (VS2010 Win7x64)

    Monday, April 16, 2012 2:33 PM
  • Hello there,

    I've experienced the same problem...

    SCardConnect returned the value of 6 !!

    I tried changing the platform from all to x86 : this time the result changed from 6 to some strange minus value..

    I tried changing all Uint32 declarations into Uint64 : but got error messages all over telling "cann't conver Long to int"

    I tried changing the same into uint : now gives me the same original error number : 6


    • Edited by YSF1122 Saturday, June 9, 2012 4:22 AM
    Friday, June 8, 2012 7:09 PM
  • Hello,

    I'm also having the same problem using Windows 7 with Omnikey5321. The Omnikey Workbench (diagnostic tool) for 64bit machine can read the card no problem, so I'm sure something needs to be changed in my code.

    I have the following code.  As suggested above, I changed the hContext and hCard to IntPtr, and also tried "UInteger", "Int32", "Int64", "Long" for the variables indicated as Int64 below. But it always returns 6.

    Public Declare Function SCardConnect Lib "WINSCARD" Alias "SCardConnectA" ( _
                ByVal hContext As IntPtr, _
                ByVal szReader As String, _
                ByVal dwShareMode As Int64, _
                ByVal dwPreferredProtocols As Int64, _
                ByRef hCard As IntPtr, _
                ByRef dwActiveProtocol As Int64) As Int64

    Public Function ReadCard() As Long
           Dim intDWShareMode As Int64
            Dim intDWPreferredProtocols As Int64
            Dim intDWActiveProtocol As Int64       
            Dim bSuccess As Boolean  
            Dim  c_strRFIDReader As String
            Dim c_intRC As Int64       

          
           intDWShareMode = SCARD_SHARE_SHARED
           intDWPreferredProtocols = SCARD_PROTOCOL_T1
           c_strRFIDReader = "OMNIKEY CardMan 5x21-CL 0"

          Try
                c_intRC = SCardConnect(c_intHContext, c_strRFIDReader, intDWShareMode, intDWPreferredProtocols, c_intHCard, intDWActiveProtocol)
                If c_intRC = 0 Then
                  'Do whatever needs to be done after getting connected
                Else                  
                    Throw New Exception(HandleOmnikeyError(c_intRC))
                End If
            Catch excX As Exception
               'Do whatever needs to be done
            End Try       
        End Function

    I also tried changing the data type of dwActiveProtocol to IntPtr as suggested by http://www.pinvoke.net/default.aspx/winscard.scardestablishcontext. But again, no luck.

    It was using "Integer" type for all input parameters for SCardConnect() except for the reader name, and it was working just fine on the 32 bit machine even with Windows 7. But now, we want to support 64 bit machines with Windows 7. If anyone who got it to work somehow other than what's suggested above can share what you did with us, it will be much appreciated.

    Thank you in advance.

    Tuesday, October 16, 2012 8:03 PM
  • worked  tks.
    Tuesday, November 4, 2014 12:16 PM
  • Hello!

    Please Share the solution

    Monday, November 24, 2014 3:36 AM
  • Hi Anna,

    I experienced similar issue.

    Check out the solution which I have found - the source code is there.

    http://www.it-sideways.com/2017/04/64-bit-mykad-reader-application-source.html

    or

    http://mykad.codeplex.com/

    The idea is to change all Handle type parameters to IntPtr instead of Int32.  

    Change Handle type parameters from Int32 to IntPtr

    It works !!




    Cheers

    Sunday, April 9, 2017 2:31 AM