winscard-nfc
WinSCard & NFC API Documentation#
Overview#
The WinSCard API provides applications with the ability to interact with both contact-based smart cards and NFC (Near Field Communication) cards and readers through the PC/SC Resource Manager. This API allows applications to establish connections with smart cards, send commands, manage transactions, and work with NFC-enabled devices.
Important Note on SCardStatus Timeout Behavior#
⚠️ SCardStatus Synchronous Operation with 15-Second Timeout#
The SCardStatus function operates as a synchronous blocking call with an internal 15-second timeout. Applications should be aware of this behavior when implementing card detection logic.
Critical Behavior:
- 15-second timeout: If no card is detected within 15 seconds, the function will return with an error
- Blocking operation: The call will block the executing thread until either:
- A card is detected
- The 15-second timeout expires
- An error occurs
NFC-Specific Considerations#
NFC Reader Detection#
NFC readers appear as standard PC/SC readers in the system. Applications can detect NFC-enabled readers using the same SCardListReaders function:
NFC Card Protocols#
When working with NFC cards, the following protocols are commonly used:
- ISO 14443 Type A/B (MIFARE, DESFire, etc.)
- ISO 15693 (Vicinity cards)
- FeliCa (Sony proprietary)
Establishing Context#
SCardEstablishContext#
Creates an Application Context to the PC/SC Resource Manager. This must be the first WinSCard function called for both contact and NFC card operations.
NFC-Specific Notes:
- Works identically for both contact-based and NFC smart cards
- No special initialization required for NFC readers
Reader Management#
SCardListReaders#
Returns a list of currently available readers, including NFC-enabled readers.
NFC-Specific Usage:
NFC Card Connection Management#
SCardConnect#
Establishes a connection to NFC readers and cards. The connection process for NFC cards is similar to contact cards but may involve different protocols.
NFC-Specific Parameters:
szReader- Name of the NFC readerdwPreferredProtocols- For NFC cards, typically use:SCARD_PROTOCOL_T1- Commonly used for NFC communicationsSCARD_PROTOCOL_RAW- For direct access to NFC card data
NFC Connection Example:
NFC Card Status Monitoring with Timeout Considerations#
SCardStatus#
Retrieves the current status of NFC readers and cards. Important: This is a synchronous call with a 15-second internal timeout.
Timeout Behavior:
- Blocks for up to 15 seconds waiting for card detection
- Returns error if no card is detected within the timeout period
- CARD DETECTED ERROR may occur if timeout expires
Best Practices for NFC Status Monitoring:
Alternative Approach Using Threading:
Handling SCardStatus Timeout in Applications:
NFC Card Communication#
SCardTransmit#
Sends APDU commands to NFC cards. The APDU structure for NFC cards follows the same standard as contact cards but may use different command sets.
NFC-Specific APDU Examples:
Common NFC Card Operations#
Reading NFC Card Data:
Writing to NFC Card:
Transaction Management for NFC#
SCardBeginTransaction / SCardEndTransaction#
Provides exclusive access to NFC cards during multiple operations, ensuring data consistency.
Complete NFC Workflow Example with Timeout Handling#
NFC-Specific Error Handling with Timeout Considerations#
When working with NFC cards, pay special attention to timeout-related errors:
This extended documentation provides comprehensive guidance for using the WinSCard API with NFC-enabled readers and cards, including specific handling of the 15-second synchronous timeout in SCardStatus and best practices for NFC technology integration with proper timeout management.