Prior to posting this question, I did already refer to these two excellent articles/tutorials by Bruce Armstrong and John Fauss:
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
I was just replying to Mark's input when I saw your feedback appear as well.
Using ProcessMonitor, I created a dump file and it appears my 64-bit .exe is referencing the correct file (c:\windows\system32\WinSCard.dll).
I'll take another look at Part II as you suggested, and I do think you are correct about using LongPTR. I've tried different combos throughout the local external function calls, but I might have missed something along the way (forest/trees syndrome). Thanks again for the suggestions, I'll be sure to provide any updates as necessary.
The type definition for SCARDHANDLE is also ULONG_PTR, so the same discussion applies to it. The protocol is a Windows DWORD or PB ULong, just as you have it coded.
HTH