h4kbas / nfc-reader

A simple library that provides to use rfid card readers.
MIT License
127 stars 44 forks source link

Error SCardListReaders #2

Open quindo opened 5 years ago

quindo commented 5 years ago

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();
}`
h4kbas commented 5 years ago

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

quindo commented 5 years ago

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.

dbste71 commented 5 years ago

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 ?

h4kbas commented 5 years ago

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 commented 5 years ago

@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.

quindo commented 5 years ago

@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.

dbste71 commented 5 years ago

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 ?

quindo commented 5 years ago

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

dbste71 commented 5 years ago

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

dbste71 commented 5 years ago

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

h4kbas commented 5 years ago

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 commented 5 years ago

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

athaks commented 4 years ago

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

RaulMerelli commented 4 years ago

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.

jonhack commented 4 years ago

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.

h4kbas commented 4 years ago

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

jonhack commented 4 years ago

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.

Lachlancg commented 3 years ago

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

seikosantana commented 2 years ago

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

ezdiatech commented 2 years ago

Thanks . My project worked with 32bit build.