nfc-reader icon indicating copy to clipboard operation
nfc-reader copied to clipboard

Error SCardListReaders

Open quindo opened this issue 6 years ago • 20 comments

Card.SCardListReaders(this.hContext, null, null, ref pcchReaders); is returning a 6 for me and I am not sure how to move forward with throubleshooting.

I am in a .NET 4.x environment and I am not getting any compile errors. I have an ACR122 reader that is working with other software.

The code that is generating the error is below: ` public NFCReader NFC;

void Start()
{
    NFCReader NFC = new NFCReader();

    NFC.CardInserted += new NFCReader.CardEventHandler(CardIn);

    NFC.CardEjected += new NFCReader.CardEventHandler(CardOut);

    NFC.Watch(); //public void Watch()
}

public void CardIn() {
    try
    {
        if (NFC.Connect())
        {
            Debug.Log( NFC.ReadBlock("0"));

        }
        else
        {
            //Give error message about connection...
        }
    }
    catch (Exception ex)
    {
        Debug.Log("Exception Should Not Have Ran: " + ex.Message);
    }
}

public void CardOut()
{
    NFC.Disconnect();
}`

quindo avatar May 14 '19 23:05 quindo

Hi, you may use GetScardErrMsg method in Card.cs in order to translate the error message. Keep me posted. I am sorry I could not write any proper documentation or testing because of my being busy

h4kbas avatar May 16 '19 16:05 h4kbas

Managed to get back to this.

Unfortunately GetScardErrMsg is returning ?.

One interesting quirk is that the first time I run the code I get "0 No error was encountered." as the response, however string readerName = GetReadersList()[0]; is resulting in a null value. I can confirm my RFID reader is plugged in and working when I get this result.

I am not sure if this is an issue with windows 10 or the specific environment I am attempting to port the SDK into.

quindo avatar May 28 '19 16:05 quindo

Hello. Hüseyin, Quindo, i have created 2 project in visual studio. In first project ( that i using for TESTING) all it is going right. I have no problem. I can read TAGS very well. After i decide to copy this TESTING CODE in another project for have NFC functionality in my application. But here i have same issue of QUINDO.

First time that i run i have result with name of may CardReader (runtime i read value of property readername) But i have two issue:

  1. event attacched it is not fired !!!!!
  2. Card.SCardListReaders(this.hContext, null, null, ref pcchReaders); is returning a 6.

How it'is possible ? I have copied same code from TEST to the other project without modify.

May be is a issue for use of hContext wy i have encapsulated in a other application ?

dbste71 avatar Jun 06 '19 14:06 dbste71

For your first problem, you need to activate the Watch() method if you don't, it won't listen. For the second one I don't really know why but did you try to run the app in administrator mode. I don't use win for a long time but I believe it can change the response of the applications.

I can't understand why one app works and the other doesn't work. Maybe your device is locked because of the first app and the other app can't connect because of OS lock. Can you post your related code snippet here so that we can see more clearly.

I am away from my Windows computer. When I get back, I can look into the library in a more detailed way and release a proper version for our own good 😃

h4kbas avatar Jun 07 '19 09:06 h4kbas

@quindo you may have driver issues? I have never seen such a thing if my device is connected. We should also add a proper check code for the device. This library was from one of my old projects so I just put it here without caring a lot.

h4kbas avatar Jun 07 '19 09:06 h4kbas

@dbste71 , What .net API versions are your 2 projects targeting? Could that be the issue?

@h4kbas it may very well be a driver issue... If the library is expecting a different driver that has been 'retired' that might explain why it is getting a different response. Other compiled applications interface with the tag reader perfectly.

quindo avatar Jun 07 '19 15:06 quindo

Good evening to all, after 1 day of tests i tink to have found where is fired the problem. I tink is for how compiled 32bit or 64bit. ### In visual studio if set 32 bit, all its working fine. image

Instead if i dont set "Prefer 32-bit" i have same issue of quindo.

My Pc is a 64bit window 10.

I need to work on 64 bit on my application. I can't set "prefer 32-bit"

I did some research and I found that in 64bit environment I should use IntPtr instead of int in the declaration of some "winscard" dll variables.

h4kbas, quindo any suggestion ?

dbste71 avatar Jun 10 '19 17:06 dbste71

I tried swapping my project to 32 bit, but unfortunately that did not change the behavior I am seeing.

quindo avatar Jun 10 '19 18:06 quindo

@quindo but how many reader results ? One only or more ?

dbste71 avatar Jun 10 '19 18:06 dbste71

take a look int this link https://stackoverflow.com/questions/10635738/error-invalid-handle-in-net-app-with-winscard-dll

dbste71 avatar Jun 10 '19 18:06 dbste71

I never had an issue like 32 bit or 64 bit. It was working on both platforms on win7 and win 10. I don't think it is related to that.

h4kbas avatar Jun 12 '19 11:06 h4kbas

I was using .net 4 i believe when I started to that project it was the stable platform

h4kbas avatar Jun 12 '19 11:06 h4kbas

Same problem here with "ACS ACR122U PICC Interface 0" // string from GetReadersList() Card.SCardConnect returns 6 // ERROR_INVALID_HANDLE

athaks avatar Oct 28 '19 22:10 athaks

I also get the same error as athaks. If I compile at 32bit I'm at least able to do NFC.GetReadersList(), but still nothing works, no event is fired, but in debug console as I put some tag on the reader I get 'System.InvalidOperationException' in System.Windows.Forms.dll.

RaulMerelli avatar Dec 22 '19 18:12 RaulMerelli

I had the same issue that was being reported above. I came across this post (https://social.msdn.microsoft.com/Forums/ie/en-US/84faa51e-98ee-4289-8e0f-c1408580f30f/winscard-smart-card-dll-32-bit-64-bit-compatibility?forum=windowscompatibility) that mentioned they had to change the hContext from an Int to a Long. That fixed the issue.

jonhack avatar Mar 19 '20 14:03 jonhack

That's cool if it works you can send a pull request and I can approve? I don't have windows to test

h4kbas avatar Mar 19 '20 18:03 h4kbas

Well, it works in that I no longer receive that particular error.

But I still am not able to read the cards Uid. The watch() function cycles, but there's never a change in card status. Once I get that working I can make a PR for it.

jonhack avatar Mar 19 '20 18:03 jonhack

@jonhack were you ever able to get the cards status change to work?

Lachlancg avatar Feb 06 '21 14:02 Lachlancg

I'm having the same issue, but I changed target platform to x86 and error is gone.

seikosantana avatar Jan 07 '22 16:01 seikosantana

Thanks . My project worked with 32bit build.

ezdiatech avatar Feb 03 '22 06:02 ezdiatech