Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) PC/SC Async Wait for Smart Card Status Change (Inserted, Removed from Reader, etc.)See more SCard ExamplesDemonstrates how to start an asynchronous Chilkat task to wait for a status change, such as for a smart card to be inserted into a reader, or removed from a reader. After starting the background task, the code loops to check on the status of your task. Note: Instead of writing a loop to wait for the status change, your application might periodically check the task status via a timer event or something similar. The purpose of this example is to show (1) how to start the async task, and (2) how to periodically check the status of the task. Note: This functionality was introduced in Chilkat v9.5.0.87.
#include <CkSCard.h> #include <CkStringTable.h> #include <CkJsonObject.h> #include <CkTask.h> void ChilkatSample(void) { CkString strOut; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkSCard scard; // First establish a context to the PC/SC Resource Manager bool success = scard.EstablishContext("user"); if (success == false) { strOut.append(scard.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Get the list of all readers. CkStringTable stReaders; success = scard.ListReaders(stReaders); if (success == false) { strOut.append(scard.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Create a Chilkat task to wait for a max of 1 hour (3600 seconds, or 3600000 milliseconds) for any smart card reader status change. CkJsonObject json; CkTask *task = scard.GetStatusChangeAsync(3600000,stReaders,json); if (scard.get_LastMethodSuccess() == false) { strOut.append(scard.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Start the task in a background thread. success = task->Run(); if (!success) { strOut.append(task->lastErrorText()); strOut.append("\r\n"); } // Loop until the task is finished, which happens when any reader's status changes. // Instead of looping here, your application could periodically check on the task status in some other way, // such as in a periodic timer event.. while (task->get_Finished() != true) { // Sleep 100 ms. task->SleepMs(100); } // When we call GetStatusChangeAsync, what's really happening is that GetStatusChange is being called in a background thread. // It returns a boolean (success/failure). Therefore, we call task.GetResultBool to get the boolean returned by GetStatusChange // in the background thread. success = task->GetResultBool(); if (success == false) { // The call to GetStatusChange in the background thread failed. Let's find out why by getting the LastErrorText // for the background synchronous call. strOut.append(task->resultErrorText()); strOut.append("\r\n"); } delete task; // If the background call to GetStatusChange succeeded, then the result was placed in the last arg, // which was our variable named "json". if (success == false) { SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Let's see what happened... json.put_EmitCompact(false); strOut.append(json.emit()); strOut.append("\r\n"); strOut.append(" "); strOut.append("\r\n"); // See the Wait for Smart Card Insertion/Removal Example for details about parsing the returned JSON. // Applications should always release the context when finished. success = scard.ReleaseContext(); if (success == false) { strOut.append(scard.lastErrorText()); strOut.append("\r\n"); } SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.