Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) 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.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @scard int -- Use "Chilkat_9_5_0.SCard" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.SCard', @scard OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- First establish a context to the PC/SC Resource Manager DECLARE @success int EXEC sp_OAMethod @scard, 'EstablishContext', @success OUT, 'user' IF @success = 0 BEGIN EXEC sp_OAGetProperty @scard, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @scard RETURN END -- Get the list of all readers. DECLARE @stReaders int -- Use "Chilkat_9_5_0.StringTable" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringTable', @stReaders OUT EXEC sp_OAMethod @scard, 'ListReaders', @success OUT, @stReaders IF @success = 0 BEGIN EXEC sp_OAGetProperty @scard, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @scard EXEC @hr = sp_OADestroy @stReaders RETURN END -- Create a Chilkat task to wait for a max of 1 hour (3600 seconds, or 3600000 milliseconds) for any smart card reader status change. DECLARE @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT DECLARE @task int EXEC sp_OAMethod @scard, 'GetStatusChangeAsync', @task OUT, 3600000, @stReaders, @json EXEC sp_OAGetProperty @scard, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @scard, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @scard EXEC @hr = sp_OADestroy @stReaders EXEC @hr = sp_OADestroy @json RETURN END -- Start the task in a background thread. EXEC sp_OAMethod @task, 'Run', @success OUT IF Not @success BEGIN EXEC sp_OAGetProperty @task, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END -- 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.. EXEC sp_OAGetProperty @task, 'Finished', @iTmp0 OUT WHILE @iTmp0 <> 1 BEGIN -- Sleep 100 ms. EXEC sp_OAMethod @task, 'SleepMs', NULL, 100 END -- 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. EXEC sp_OAMethod @task, 'GetResultBool', @success OUT IF @success = 0 BEGIN -- The call to GetStatusChange in the background thread failed. Let's find out why by getting the LastErrorText -- for the background synchronous call. EXEC sp_OAGetProperty @task, 'ResultErrorText', @sTmp0 OUT PRINT @sTmp0 END EXEC @hr = sp_OADestroy @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 = 0 BEGIN EXEC @hr = sp_OADestroy @scard EXEC @hr = sp_OADestroy @stReaders EXEC @hr = sp_OADestroy @json RETURN END -- Let's see what happened... EXEC sp_OASetProperty @json, 'EmitCompact', 0 EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT PRINT @sTmp0 PRINT ' ' -- See the Wait for Smart Card Insertion/Removal Example for details about parsing the returned JSON. -- Applications should always release the context when finished. EXEC sp_OAMethod @scard, 'ReleaseContext', @success OUT IF @success = 0 BEGIN EXEC sp_OAGetProperty @scard, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END -- 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 EXEC @hr = sp_OADestroy @scard EXEC @hr = sp_OADestroy @stReaders EXEC @hr = sp_OADestroy @json END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.