Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C) PC/SC Async Wait for Smart Card Status Change (Inserted, Removed from Reader, etc.)

See more SCard Examples

Demonstrates 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.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkSCardW.h>
#include <C_CkStringTableW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkTaskW.h>

void ChilkatSample(void)
    {
    HCkSCardW scard;
    BOOL success;
    HCkStringTableW stReaders;
    HCkJsonObjectW json;
    HCkTaskW task;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    scard = CkSCardW_Create();

    // First establish a context to the PC/SC Resource Manager
    success = CkSCardW_EstablishContext(scard,L"user");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
        CkSCardW_Dispose(scard);
        return;
    }

    // Get the list of all readers.
    stReaders = CkStringTableW_Create();
    success = CkSCardW_ListReaders(scard,stReaders);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
        CkSCardW_Dispose(scard);
        CkStringTableW_Dispose(stReaders);
        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.
    json = CkJsonObjectW_Create();
    task = CkSCardW_GetStatusChangeAsync(scard,3600000,stReaders,json);
    if (CkSCardW_getLastMethodSuccess(scard) == FALSE) {
        wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
        CkSCardW_Dispose(scard);
        CkStringTableW_Dispose(stReaders);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // Start the task in a background thread.
    success = CkTaskW_Run(task);
    if (!success) {
        wprintf(L"%s\n",CkTaskW_lastErrorText(task));
    }

    // 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 (CkTaskW_getFinished(task) != TRUE) {

        // Sleep 100 ms.  
        CkTaskW_SleepMs(task,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 = CkTaskW_GetResultBool(task);
    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.
        wprintf(L"%s\n",CkTaskW_resultErrorText(task));
    }

    CkTaskW_Dispose(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) {
        CkSCardW_Dispose(scard);
        CkStringTableW_Dispose(stReaders);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // Let's see what happened...
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));
    wprintf(L" \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 = CkSCardW_ReleaseContext(scard);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
    }

    // Note: It may be necessary to call FinalizeThreadPool in some programming environments just before your program exits.
    // (Not after every async function call, but only before program exit.)
    // See Call FinalizeThreadPool before program exit


    CkSCardW_Dispose(scard);
    CkStringTableW_Dispose(stReaders);
    CkJsonObjectW_Dispose(json);

    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.