js-ctypes invalid arguments

1

Iam trying to run winscard functions using js-ctypes in Firefox. I have working C code and I started to copy code to javascipt. Unfortunetly my first function- SCardEstablishContext returns the following error:

SCARD_E_INVALID_PARAMETER 0x80100004

Whats wrong with the arguments?

Components.utils.import("resource://gre/modules/ctypes.jsm");
const NULL = ctypes.voidptr_t(0);

var cardLib = ctypes.open("C:\\WINDOWS\\system32\\WinSCard.dll");
var SCardEstablishContext = cardLib.declare("SCardEstablishContext", ctypes.winapi_abi, ctypes.uint32_t, ctypes.uint32_t, ctypes.voidptr_t, ctypes.voidptr_t, ctypes.voidptr_t);
var ContextHandle = new ctypes.voidptr_t();
var ret = SCardEstablishContext(2, NULL, NULL, ContextHandle);
cardLib.close();
javascript
windows
smartcard
jsctypes
asked on Stack Overflow Jul 30, 2015 by Karol Sarnecki • edited Jul 30, 2015 by Paradox

2 Answers

1

The last parameter of SCardEstablishContext (phContext) should be a pointer to a 32-bit integer. Upon success, SCardEstablishContext populates this integer value with the handle to the SCARD context.

You define ContextHandle as new uninitialized instance of ctypes.voidptr_t, which is essentially the same as ctypes.voidptr_t(0) (hence, a null-pointer). You then pass this null-pointer to SCardEstablishContext, which, conequently, cannot assign a value (as the reference/pointer is not backed by actual data memory).

Thus, you should define ContextHandle as a voidptr_t (32-bit integer on 32-bit platforms / 64-bit integer on 64-bit platforms)

var ContextHandle = ctypes.voidptr_t(0);

and then pass the pointer to this ContextHandle to the SCardEstablishContext function:

var ret = SCardEstablishContext(2, NULL, NULL, ContextHandle.address());
answered on Stack Overflow Jul 30, 2015 by Michael Roland • edited Aug 23, 2015 by Michael Roland
1

Your issue is you need to pass ContextHandle.address() you only pass ContextHandle.

Here's my style of code:

Cu.import('resource://gre/modules/ctypes.jsm');
//const NULL = ctypes.voidptr_t(0); // no need for this, just use js null

var is64bit = ctypes.voidptr_t.size == 4 ? false : true;
var TYPES = {
  ABI: is64bit ? ctypes.default_abi : ctypes.winapi_abi,
  CALLBACK_ABI: is64bit ? ctypes.default_abi : ctypes.stdcall_cabi,
  DWORD: ctypes.unsigned_long,
  LPCVOID: ctypes.voidptr_t,
  LPSCARDCONTEXT: ctypes.voidptr_t
}

var CONST = {
  SCARD_SCOPE_USER: 0,
  SCARD_SCOPE_SYSTEM: 2,
  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 ContextHandle = TYPES.LPSCARDCONTEXT();
var ret = SCardEstablishContext(CONST.SCARD_SCOPE_SYSTEM, null, null, ContextHandle.address());

console.info('ret:', ret, ret.toString());

if (ret.toString() != CONST.SCARD_S_SUCCESS.toString()) {
  console.error('failed to establish context! error code was: ' + ret + ' in other terms it is: 0x' + ret.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');
}

cardLib.close();

It wont work for me has i dont have a card reader it keeps giving me error 0x8010001d which is SCARD_E_NO_SERVICE per https://msdn.microsoft.com/en-us/library/windows/desktop/aa374738%28v=vs.85%29.aspx#smart_card_return_values and this totally makes sense as my computer has no card reader.

But this code works and shows you how to see the error you get as well :) Keep up the awesome work and keep posting here if you need help!

There's also jsctypes channel on moz irc servers. Can copy paste this url into your firefox: irc://moznet/jsctypes

answered on Stack Overflow Aug 1, 2015 by Noitidart • edited Jun 20, 2020 by Community

User contributions licensed under CC BY-SA 3.0