Node smartcard throw SCardConnect error while scanning the card and SCardListReaders error after disconnect

2

I am also using angular and electron, but everything worked fine previously. This is the error:

Error: SCardConnect error: 
SCardConnect error: The smart card cannot be accessed because of other connections outstanding.
(0x8010000b)

After the first time scanning a card, it no longer throws error and works fine reading cards until relaunch. Here are some snippets of my code relevant to smartcard:

const smartcard = require('smartcard');
const Devices = smartcard.Devices;
const devices = new Devices();
let currentDevices = [];

//something else

app.run(function($rootScope) {
let registerDevices = function (event) {
    currentDevices = event.devices;
    currentDevices.forEach(function (device) {
        device.on('card-inserted', event => {
            let card = event.card;
            console.log(`Card '${card.getAtr()}' inserted into '${card.device}'`);
            $rootScope.$broadcast('card-attach',card.getAtr());
        });
        device.on('card-removed', event => {
        });
        device.on('error', event => {
            console.error("Card Reader Error: " + event);
        });
    });
};

devices.on('device-activated', event => {
    console.log("Reader added :" + event.device);
    registerDevices(event);
});

devices.on('device-deactivated', event => {
    console.log("Reader removed :" + event.device);
    registerDevices(event);
});
});

In addition, when I disconnect the scanner, it says

events.js:160 Uncaught Error: SCardListReaders error: The Smart Card Resource Manager is not running.
(0x8010001d)

events.js:163 Uncaught Error: Uncaught, unspecified "error" event. ([object Object])

And the scanner is not working after reconnect.

javascript
angularjs
node.js
electron
smartcard
asked on Stack Overflow Nov 19, 2016 by DEDZTBH • edited Nov 21, 2016 by DEDZTBH

1 Answer

1

This error code is E_SHARING_VIOLATION -- some process has already connected to the card in exclusive mode (using SCARD_SHARE_EXCLUSIVE for SCardConnect).


[Assuming you are under Windows]:

There is a Plug&Play mechanism in Windows which by default automatically accesses every card immediately after insertion and tries to determine the correct driver for it -- which creates a short time window when the card is being accessed (this is IMHO the most probable cause).

You have two choices:

  1. Deal with it -- retry the card connection attempt after some time (tens of ms, YMMV) for this particular error code (possibly could be done in loop with some max retry count).

  2. Disable this behavior -- there are two ways (I have never used the Group Policy one, but it should work):

    a/ using Local Group Policy Settings (disable Computer Configuration -> Administrative Templates -> Windows Components -> Smart Card -> Turn On Smart Card Plug and Play Service) (see e.g. here)

    b/ by setting the registry key EnableScPnP under HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\ScPnP to dword:0x00000000 (for a 64-bit system set it under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\ScPnP as well) -- see e.g. here. Be sure to reboot your system for the change to take effect


Regarding your edit with the 0x8010001D (E_NO_SERVICE) error code and reconnecting a reader -- I have no idea.

Good luck!

answered on Stack Overflow Nov 20, 2016 by vlp • edited Nov 22, 2016 by vlp

User contributions licensed under CC BY-SA 3.0