1. Jason Schultz
  2. PowerBuilder
  3. Monday, 29 November 2021 11:15 PM UTC

Prior to posting this question, I did already refer to these two excellent articles/tutorials by Bruce Armstrong and John Fauss:

   - https://community.appeon.com/index.php/articles-blogs/tutorials-articles/2-powerbuilder/158-communication-with-a-smart-card-from-powerbuilder

   - https://community.appeon.com/index.php/articles-blogs/tutorials-articles/2-powerbuilder/305-interfacing-pb-applications-with-the-windows-api-part-4

Using Bruce's smart card sample application as a reference point, I took his sample application and wanted to experiment with it. For the most part, it runs out of the box. So far, the only thing I've had to tweak so far were the values passed with specific APDU calls used when verifying the PIN. This was expected since the call values may differ depending on the make/manufacturer of the smart card being used/referenced. Aside from that, the sample application runs fine in 32-bit mode (running from within my PB IDE - 2019R2). After creating a 64-bit .exe, I'm experiencing some issues trying to establish a connection with the card, which leads to my questions. I'm hoping someone can point me in the right direction on this, I feel that it's something obvious, and I'm just not seeing it. As of now, my primary questions refer to some of the API calls being made against the winscard.dll.

Although it would be ideal to reference the sample application code itself, hopefully I can properly frame my specific questions without it.

Within a non-visual object, the following is defined: 

Instance Variables:

protected ulong context
protected string reader
protected ulong	card
protected ulong	protocol

CONSTANT LONG SCARD_S_SUCCESS = 0
CONSTANT ULONG SCARD_SCOPE_USER = 0

CONSTANT INT SUCCESS 		= 1
CONSTANT INT FAILURE 		= -1

CONSTANT LONG SCARD_SHARE_SHARED = 2
CONSTANT LONG SCARD_PROTOCOL_Tx  = 3

 

Within Local External Functions, we have various method calls to the winscard.dll:

Protected Function ulong SCardEstablishContext  ( &
	Long dwScope, &
	long pvReserved1, &
	long pvReserved2, &
	REF ulong phContext &
	) Library "winscard.dll"

Protected Function ulong SCardConnect  ( &
	long hContext, &
	String szReader, &
	Long dwShareMode, &
	Long dwPreferredProtocols, &
	REF ulong phCard, &
	REF ulong pdwActiveProtocol &
	) Library "winscard.dll" Alias For "SCardConnectW"

 

Before attempting to connect to the card, we first need to establish a reference or handle to the card itself. We make the following call through a function called of_establishcontext(). This will populate the 'context' argument. 

ulong	rc

// We should get a return value of 0 for successful call.
rc = scardestablishcontext( SCARD_SCOPE_USER, 0, 0, context )

IF rc = SCARD_S_SUCCESS THEN
	Return SUCCESS
ELSE
	Return FAILURE
END IF

 

After a successful call (and context value is identified), we then attempt to connect to the card via the of_connectcard() function. Assume the passed in as_reader argument value is correct:

ulong	rc
reader = as_reader

rc = SCardConnect ( context, &
		 reader, &
		 SCARD_SHARE_SHARED, &
		 SCARD_PROTOCOL_Tx, &
		 card, &
		 protocol )

IF rc = SCARD_S_SUCCESS THEN
	Return SUCCESS
ELSE
	Return FAILURE
END IF

 

As mentioned before, the above code snippets work fine in 32-bit mode (either 32-bit .exe or run via PB IDE). The of_establishcontext() results in a return value of 0 (success), and the context value is populated with a value of 3439394816. The of_connectcard() is also successful.

When run in 64-bit mode (running a 64-bit .exe), the of_establishcontext() results in a return value of 0 (success), and the context value is populated with a value of 0. The of_connectcard() is unsuccessful with a return value of 6 (invalid handle error).  

This is where I'm at a loss. I'm trying to establish a valid handle to the card (via the context) to successfully connect to the card. As of now, I assume the same winscard.dll calls apply to both 32-bit and 64-bit. Here are the Microsoft definitions of the two API calls in question:

https://docs.microsoft.com/en-us/windows/win32/api/winscard/nf-winscard-scardestablishcontext

https://docs.microsoft.com/en-us/windows/win32/api/winscard/nf-winscard-scardconnectw

Using John's article on API calls, it served as a valuable reference in cross-referencing datatypes between external calls and PowerBuilder. I assume my problem is with the initial context value serving as a reference/pointer.  The Microsoft API refers to it as a LPSCardContext or SCardContext. I'm not sure what that relates to in PowerBuilder. In 32-bit, it works as a ulong. In 64-bit, I've tried referencing context as ulong, LongPTR, and LongLong. Unfortunately, I can't yet get any of them to work, still resulting in an invalid handle error when attempting to connect.

Any ideas on what I'm doing wrong?

I appreciate any input.

Thanks,
Jason  

 


Responses (6)
  1. Likes
  2. Latest
  3. Oldest
Loading...

Find Questions by Tag

.EXE .NET 6.0 .NET Assembly .NET Core 3.1 .NET Core Framework .NET DataStore .NET Std Framework 32-bit 64-bit ADO.NET AEM AI Algorithm Amazon AWS Android Apache API APK App Store App Store (Apple) Appeon Workspace Appeon Xcelerator Plug-in Architecture Array ASE Asynchronous Methods Authentication AutoBuild AutoCompiler Automated Testing Automation AutoScript Azure Barcode Base64 Batch BigData BLOB Branch & Merge Browser Bug Build Button C# C# Class Importer C# Editor C# Model generator Calendar Camera Certificate Chrome Citrix Class Client Client/Server Cloud Cluster Collection COM Command Line Compiler Compression Computed Field Configuration Controls Cookies Cordova Crash Cross-Platform Crosstab CSharpAssembly CSharpObject CSS CSV Cursor Data Database Database Driver Database Painter Database Profile Database Provider DataObject DataSource DataStore DataStore (C#) DataStore (PS) DataType DataWindow DATE DATETIME DB2 Debug Debugger Debugging Deployment Design DLL DO-WHILE Dockable Docker Documentation DOUBLE Download DragDrop Edge Edit Style Editor Elevate Conference Email Embedded SQL Emulator Encoding Encryption Enhancement Request Entity Entity Framework ERP Error Event Event Handler Event Handling Excel Exception Export Expression External Functions F# Field File File Access Filter Firefox Firewall Font FOR-NEXT Foreground Format Function Garbage Collection GeoLocation Git Graph HANA Hash Header HTML/5 HTTP/S HTTPClient Icon IDE Identity IIS IMAPI Import InfoMaker Inheritance Installation Integer IntelliSense Interface Internet Internet Explorer iOS IPA iPad iPhone IWA J# Java JavaScript JBoss JDBC JOIN JSON JSONGenerator JSONParser Kestrel Label Lambda Large File LDAP Library License LINQ Linux OS Load Balancing Localization Localized PBVM Log In Log Out Logging LONG LONGLONG macOS MAPI Maps MDI Memory Memory Leak Menu Merge MessageBox Messagging Method Migration MIME TYPE Mobile Model ModelStore ModelStore (C#) MSOLEDBSQL Multi Threading MVC MySQL n-Tier Namespace NativePDF NVO OAuth ODATA ODBC Office Offline OLE OLEDB Online Open Source OpenAPI OpenSSL Oracle OrcaScript Other Outlook Output Package Parameter Patch PayPal PB Classic PB Native PB.NET PBC PBD PBDOM PBG PBJVM PBL PBNI PBORCA PBVM PBX PDF Performance Permission PFC Picture Pipeline Play Store (Google) Plugin Popup Port POST PostgreSQL PowerBuilder PowerBuilder (Appeon) PowerBuilder (SAP) PowerBuilder Compiler PowerBuilder Runtime PowerClient PowerScript (PS) PowerScript IDE PowerScript Migrator PowerServer PowerServer Mobile PowerServer Toolkit PowerServer Web PowerServerLabel Print Properties Proxy Publish PULL PUSH Query Regression Release Renew Resize Response REST Retrieve RibbonBar RibbonBar Builder Rich Text Roadmap RPC Runtime Packager SaaS Scaffolding Script SDI SDK Security Server Service Session Single Sign-on Size SMTP SMTPClient SnapDevelop SOAP Sort Source Code Speech Recognition SQL SQL Anywhere SQL Server SqlBuilder SqlExecutor SQLite SqlModelMapper Storage Stored Procedure Subscription SVN Swagger Syntax TabbedBar TabbedView Tablet TabPage Target TE Control Testing Text TFS Theme TIME Timer TLS/SSL Tomcat TortoiseGit TortoiseSVN Transaction Transparency Trial Trigger TRY-CATCH TX Control Type UI ULONG UltraLite Uninstall Unit Test Unit Testing UNIX OS Update Upgrade Upload URL User Center User Object UWP Validation VARCHAR Variable Versioning Visual Studio Visual Studio Code VM Voice Warning WCF Web API Web Extensions Web Service WebBrowser WebForms WebLogic WebSphere WildFly WinAPI Window Windows OS WinForms Wizard Workgroup Workspace WPF XCODE XHTML XML Zoom

Helpful?

If a reply or comment is helpful for you, please don’t hesitate to click the Helpful button. This action is further confirmation of their invaluable contribution to the Appeon Community.