javascriptwinapifirefox-addon-sdkjsctypes

SCardListReaders in js-ctypes giving 0x6 error


continuing the previous question: Error while calling SCardListReaders in js-ctypes

the code was amended as follows:

Cu.import('resource://gre/modules/ctypes.jsm');

var is64bit = ctypes.voidptr_t.size == 4 ? false : true;
var ifdef_UNICODE = true;

var TYPES = {
ABI: is64bit ? ctypes.default_abi : ctypes.winapi_abi,
CALLBACK_ABI: is64bit ? ctypes.default_abi : ctypes.stdcall_cabi,

CHAR: ctypes.char,
DWORD: ctypes.uint32_t,
LONG: ctypes.long,
LPCVOID: ctypes.voidptr_t,
ULONG_PTR: is64bit ? ctypes.uint64_t : ctypes.unsigned_long,
WCHAR: ctypes.jschar
};

// advanced types - based on simple types
TYPES.LPSTR = TYPES.CHAR.ptr;
TYPES.LPCSTR = TYPES.CHAR.ptr;
TYPES.LPDWORD = TYPES.DWORD.ptr;
TYPES.LPCWSTR = TYPES.WCHAR.ptr;
TYPES.LPWSTR = TYPES.WCHAR.ptr;
TYPES.SCARDCONTEXT = TYPES.ULONG_PTR;

// advanced advanced types - based on advanced types
TYPES.LPCTSTR = ifdef_UNICODE ? TYPES.LPCWSTR : TYPES.LPCSTR;
TYPES.LPTSTR = ifdef_UNICODE ? TYPES.LPWSTR : TYPES.LPSTR;
TYPES.PSCARDCONTEXT = TYPES.SCARDCONTEXT.ptr;
TYPES.LPSCARDCONTEXT = TYPES.SCARDCONTEXT.ptr;

var CONST = {
    SCARD_AUTOALLOCATE: TYPES.DWORD('0xffffffff'),
    SCARD_SCOPE_SYSTEM: 2,
    SCARD_SCOPE_USER: 0,
    SCARD_S_SUCCESS: 0
};

var cardLib = ctypes.open('Winscard');
var SCardEstablishContext = cardLib.declare('SCardEstablishContext', TYPES.ABI, TYPES.DWORD, TYPES.DWORD, TYPES.LPCVOID, TYPES.LPCVOID, TYPES.LPSCARDCONTEXT);
var SCardListReaders = cardLib.declare(ifdef_UNICODE ? 'SCardListReadersW' : 'SCardListReadersA', TYPES.ABI, TYPES.LONG, TYPES.LPSCARDCONTEXT, TYPES.LPCTSTR, TYPES.LPTSTR, TYPES.LPDWORD);
var SCardFreeMemory = cardLib.declare('SCardFreeMemory', TYPES.ABI, TYPES.LONG, TYPES.SCARDCONTEXT, TYPES.LPCVOID);
var SCardReleaseContext = cardLib.declare('SCardReleaseContext', TYPES.ABI, TYPES.LONG, TYPES.SCARDCONTEXT);

// types, consts, and functions declarations complete, now lets use it
try {
    var hSC = TYPES.SCARDCONTEXT();
    var rez_SCEC = SCardEstablishContext(CONST.SCARD_SCOPE_USER, null, null, hSC.address());
    if (rez_SCEC.toString() != CONST.SCARD_S_SUCCESS.toString()) {
        console.error('failed to establish context! error code was: ' + rez_SCEC + ' in other terms it is: 0x' + rez_SCEC.toString(16) + ' you can look up this error value here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374738%28v=vs.85%29.aspx#smart_card_return_values');
        throw new Error('failed to establish context!');
    }

    try {
        var reader_count = TYPES.DWORD();
        var reader_names = TYPES.LPTSTR.targetType(0);
        var rez_SCLR = SCardListReaders(hSC.address(), null, null, reader_count.address());
        console.log('rez_SCLR:', rez_SCLR, rez_SCLR.toString());
        if (rez_SCLR.toString() != CONST.SCARD_S_SUCCESS.toString()) {
            console.error('failed to get list of readers! error code was: ' + rez_SCLR + ' in other terms it is: 0x' + rez_SCLR.toString(16) + ' you can look up this error value here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374738%28v=vs.85%29.aspx#smart_card_return_values');
            throw new Error('failed to get list of readers!');
        }
throw ('done');
        var rez_free = SCardFreeMemory(hSC, reader_names);
        console.log('rez_free:', rez_free, rez_free.toString());
        if (rez_release.toString() != CONST.SCARD_S_SUCCESS.toString()) {
            console.error('failed to free the multi-string that lists the card readers! error code was: ' + rez_release + ' in other terms it is: 0x' + rez_release.toString(16) + ' you can look up this error value here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374738%28v=vs.85%29.aspx#smart_card_return_values');
            throw new Error('failed to free the multi-string that lists the card readers!');
        }
    } finally {
        var rez_release = SCardReleaseContext(hSC);
        console.log('rez_release:', rez_release, rez_release.toString());
        if (rez_release.toString() != CONST.SCARD_S_SUCCESS.toString()) {
            console.error('failed to release context! error code was: ' + rez_release + ' in other terms it is: 0x' + rez_release.toString(16) + ' you can look up this error value here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa374738%28v=vs.85%29.aspx#smart_card_return_values');
            throw new Error('failed to release context!');
        }
    }

} finally {
    cardLib.close();
    console.log('cardLib closed');
}

However it also has the following error:

"failed to get list of readers! error code was: 6 in other terms it is: 0x6"

Exception: Error: failed to get list of readers! @Scratchpad/2:61:1 WCA_evalWithDebugger@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webconsole.js:1185:16 WCA_onEvaluateJS@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webconsole.js:797:20 WCA_onEvaluateJSAsync@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webconsole.js:768:20 DSC_onPacket@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/main.js:1456:15 LocalDebuggerTransport.prototype.send/<@resource://gre/modules/devtools/dbg-client.jsm -> resource://gre/modules/devtools/transport/transport.js:561:11 makeInfallible/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/DevToolsUtils.js:82:14 makeInfallible/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/DevToolsUtils.js:82:14


Solution

  • It turns out the hSC passed to SCardListReaders was wrong.

    SCardListReaders(hSC.address(),
    

    Should be:

    SCardListReaders(hSC,