<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yiv7468746207"><div id="yui_3_16_0_1_1433190274387_15096"><div id="yui_3_16_0_1_1433190274387_15095" style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;"><div id="yiv7468746207yui_3_16_0_1_1433190274387_9100"><span>Hi Ludovic,</span></div><div id="yiv7468746207yui_3_16_0_1_1433190274387_9529"><br clear="none"><span></span></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_9768"><span id="yiv7468746207yui_3_16_0_1_1433190274387_10219">I have tested a similar sample.c (attached) in a windows vista (sorry, it's the only windows I have) with Windows SDK 6.1 and it is clear that the SCardRconnect clears the connections.  Compilation and output is presented below:<br></span></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_10456"><br clear="none"></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_12385">C:\Users\ricky\Desktop>cl sample.c Winscard.lib<br class="yiv7468746207" style="" clear="none">Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86<br class="yiv7468746207" style="" clear="none">Copyright (C) Microsoft Corporation.  All rights reserved.<br class="yiv7468746207" style="" clear="none"><br class="yiv7468746207" style="" clear="none">sample.c<br class="yiv7468746207" style="" clear="none">Microsoft (R) Incremental Linker Version 9.00.21022.08<br class="yiv7468746207" style="" clear="none">Copyright (C) Microsoft Corporation.  All rights reserved.<br class="yiv7468746207" style="" clear="none"><br class="yiv7468746207" style="" clear="none">/out:sample.exe<br class="yiv7468746207" style="" clear="none">sample.obj<br class="yiv7468746207" style="" clear="none">Winscard.lib</div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_12625"><br clear="none"><span id="yiv7468746207yui_3_16_0_1_1433190274387_10219"></span></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_10693"><span id="yiv7468746207yui_3_16_0_1_1433190274387_10219">C:\Users\ricky\Desktop>sample.exe<br class="yiv7468746207" style="" clear="none">reader name: Gemplus USB SmartCard Reader 0<br class="yiv7468746207" style="" clear="none">response: 86 CC 40 80 7A 68 C9 90 00<br class="yiv7468746207" style="" clear="none">response: 86 CC 40 80 7A 68 C9 90 00<br class="yiv7468746207" style="" clear="none">SCardEndTransaction: 0x80100016 (0x80100016)</span></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_10702"><br clear="none"><span id="yiv7468746207yui_3_16_0_1_1433190274387_10219"></span></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_10939"><span id="yiv7468746207yui_3_16_0_1_1433190274387_10219">So the SCardEndTransaction returns SCARD_E_NOT_TRANSACTED after the reconnect. Besides the documentation says the following:</span></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_11637"><br clear="none"></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_11659">"The application must immediately call the SCardDisconnect, SCardReconnect, or SCardReleaseContext function to avoid an existing transaction blocking other threads and processes from communicating with the smart card."<br clear="none"></div><div id="yiv7468746207yui_3_16_0_1_1433190274387_12152"><br clear="none"></div><div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_12151">See here: <a rel="nofollow" shape="rect" id="yiv7468746207yui_3_16_0_1_1433190274387_12155" target="_blank" href="https://msdn.microsoft.com/es-es/library/windows/desktop/aa379477%28v=vs.85%29.aspx">https://msdn.microsoft.com/es-es/library/windows/desktop/aa379477%28v=vs.85%29.aspx</a><br clear="none"></div><div class="yiv7468746207qtdSeparateBR" id="yiv7468746207yui_3_16_0_1_1433190274387_9356"><div id="yiv7468746207yui_3_16_0_1_1433190274387_12855"><br clear="none"></div><div id="yiv7468746207yui_3_16_0_1_1433190274387_12851">Regards!</div><div id="yiv7468746207yui_3_16_0_1_1433190274387_12852"><br clear="none"></div></div><div class="yiv7468746207yqt9464439938" id="yiv7468746207yqt40566"></div></div></div></div><div class=".yiv7468746207yahoo_quoted"> <div id="yiv7468746207yui_3_16_0_1_1433190274387_9352" style="font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;"> <div id="yiv7468746207yui_3_16_0_1_1433190274387_9351" style="font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;"> <div dir="ltr" id="yiv7468746207yui_3_16_0_1_1433190274387_9355"> <font id="yiv7468746207yui_3_16_0_1_1433190274387_9354" face="Arial" size="2"> On Monday, June 1, 2015 2:52 PM, Ludovic Rousseau <ludovic.rousseau@gmail.com> wrote:<br clear="none"> </font> </div>  <br clear="none"><br clear="none"> <div class="yiv7468746207y_msg_container" id="yiv7468746207yui_3_16_0_1_1433190274387_9350">2015-06-01 13:57 GMT+02:00 Ricardo Martin <<a rel="nofollow" shape="rect" ymailto="mailto:rickyepoderi@yahoo.es" target="_blank" href="mailto:rickyepoderi@yahoo.es">rickyepoderi@yahoo.es</a>>:<br clear="none">> Hi everybody,<br clear="none"><br clear="none">Hello,<br clear="none"><br clear="none">> Playing with opensc and the dnie driver I found a weird behavior. After some<br clear="none">> tests I opened the following issue in opensc github:<br clear="none">><br clear="none">> <a rel="nofollow" shape="rect" target="_blank" href="https://github.com/OpenSC/OpenSC/issues/475">https://github.com/OpenSC/OpenSC/issues/475</a><br clear="none">><br clear="none">> It seems that opensc in the method sc_reset finally sends a SCardReconnect<br clear="none">> with SCARD_UNPOWER_CARD or SCARD_RESET_CARD (depending the do_cold_reset<br clear="none">> parameter). But at the end of the sc_reset function opensc performs the<br clear="none">> following:<br clear="none">><br clear="none">>         /* pcsc_reconnect unlocks card... try to lock it again if it was<br clear="none">> locked */<br clear="none">>         if(old_locked)<br clear="none">>                 r = pcsc_lock(reader);<br clear="none">><br clear="none">> If the connection was previously locked, after the reconnect, it adds a lock<br clear="none">> again (SCardBeginTransaction is called again inside pcsc_lock). My<br clear="none">> impression is that this extra call adds one more lock in the pcsc<br clear="none">> (rContext->LockCount) which is never released, and this ends in the complete<br clear="none">> lock of the card until the current process finishes (no other process can<br clear="none">> access the card in shared mode). Every call to pcsc_reset in opensc seems to<br clear="none">> add a lost lock in pcsc (and the dnie driver calls to this reset).<br clear="none">><br clear="none">> I have done some little test with pcsc (with the little sample.c copied<br clear="none">> below) and it seems that SCardReconnect does not lose the locks (in any<br clear="none">> case: SCARD_SHARE_SHARED or SCARD_SHARE_EXCLUSIVE, SCARD_UNPOWER_CARD or<br clear="none">> SCARD_RESET_CARD). But, just to be sure, can anybody confirm that<br clear="none">> SCardReconnect maintains the locks (transactions)? Is the extra call done in<br clear="none">> opensc to SCardBeginTransaction needed?<br clear="none"><br clear="none">It looks like you are right.<br clear="none"><br clear="none">I checked the source code of pcsc-lite and SCardReconnect() does not<br clear="none">release or take a lock. It only checks if a lock (from another PC/SC<br clear="none">context) is not yet present.<br clear="none"><br clear="none">To know if this is a bug or not in pcsc-lite we should know how it<br clear="none">works on Windows.<br clear="none">I could not find any reference to transactions or locks in MSDN<br clear="none">documentation [1].<br clear="none"><br clear="none">Can you do some tests on Windows and tell us how SCardReconnect()<br clear="none">behaves on Windows?<div class="yiv7468746207yqt0953246664" id="yiv7468746207yqtfd42405"><br clear="none"><br clear="none">Thanks</div><br clear="none"><br clear="none">[1] <a rel="nofollow" shape="rect" id="yiv7468746207yui_3_16_0_1_1433190274387_13195" target="_blank" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa379797%28v=vs.85%29.aspx">https://msdn.microsoft.com/en-us/library/windows/desktop/aa379797%28v=vs.85%29.aspx</a><br clear="none"><br clear="none">-- <br clear="none"> Dr. Ludovic Rousseau<div class="yiv7468746207yqt0953246664" id="yiv7468746207yqtfd94037"><br clear="none"></div><br clear="none"><br clear="none"></div>  </div> </div>  </div></div></body></html>