I have written a program to communicate with a smart card (Gemalto Company MPCOS applet). I could successfully connect to card and transmit commands and fetch data.
I have found the problem:
The problem was due to invoking the SCardTransmit function for two times. Indeed, one time to get response length and the second time for execute the command and getting response.
This dual invoke lead to error 6981:
function SCardTransmitFunc(aCallbackName, myCommand){
var _SCARD_IO_REQUEST = new CONST.SCARD_IO_REQUEST;
_SCARD_IO_REQUEST.dwProtocol = AProtocol;
_SCARD_IO_REQUEST.cbPciLength = CONST.SCARD_IO_REQUEST.size;
var myArrayCommand = hex2Dec(myCommand);
var command = TYPES.LPBYTE.targetType.array(myArrayCommand.length)(myArrayCommand);
var commandLength = command.length;
var responseLength = TYPES.DWORD();
var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, null, responseLength.address());
var response = TYPES.LPBYTE.targetType.array(parseInt(responseLength.value))();
var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, response, responseLength.address());
var myResponse = "";//new Array();
for(i = response.length - 2; i < response.length ; i++)
{
myResponse += dec2Hex(response[i]);
}
}
and the corrected code is this:
function SCardTransmitFunc(aCallbackName, myCommand){
var _SCARD_IO_REQUEST = new CONST.SCARD_IO_REQUEST;
_SCARD_IO_REQUEST.dwProtocol = AProtocol;
_SCARD_IO_REQUEST.cbPciLength = CONST.SCARD_IO_REQUEST.size;
var myArrayCommand = hex2Dec(myCommand);
var command = TYPES.LPBYTE.targetType.array(myArrayCommand.length)(myArrayCommand);
var commandLength = command.length;
var responseLength = TYPES.DWORD(1024);
var response = TYPES.BYTE.array(parseInt(1024))();
var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, response, responseLength.address());
var myResponse = "";//new Array();
var myLength = parseInt(responseLength.value);
for(i = myLength - 2; i < myLength ; i++)
{
myResponse += dec2Hex(response[i]);
}
}
I really thanks @guidot for his good hint and dear @vlp for his helps