Dear knowledgeable ones.

I started on a project to be able to use a smart card and a smart card reader in order to limit access to a program.

I had some sample code written in Visual Basic, and wanted to translate this to Delphi instead, something I think I have done pretty well.

At the moment, I get my program up and running. It compiles with no error... BUT...

Whenever I try to access external functions I have problems with the parameters.
I wonder if somebody maybe have a good explanation on how to be able to use the functions in the "SCardSyn.DLL"

It seems like Delphi and Visual Basic handles transfer of parameters quite differently.

My code for a few functions is as follows:

{Establish a context to resource manager
                                    Parameters:
                                      dwScope         = Scope (see Scopes)
                                      pvReserved1     = Reserved for further use
                                      pvReserved2     = Reserved for further use
                                      phContext       = Pointer to Context}
  FUNCTION SCardEstablishContext(dwScope, pvReserved1, pvReserved2:LONGWORD; phContext:PCHAR):LONGWORD; stdcall; external 'WINSCARD';

That actually works, where as my direct translation of Visual Basic code
*** Visual Basic code START***
' Establish a context to resource manager
' Parameters:
' dwScope = Scope (see Scopes)
' pvReserved1 = Reserved for further use
' pvReserved2 = Reserved for further use
' phContext = Pointer to Context
'
Public Declare Function SCardEstablishContext Lib "WINSCARD" _
(ByVal dwScope As Long, _
ByVal pvReserved1 As Long, _
ByVal pvReserved2 As Long, _
ByRef phContext As Long) As Long
*** Visual Basic code END ***
*** Delphi equivalent? code START ***

FUNCTION SCardEstablishContext(dwScope, pvReserved1, pvReserved2:LONGWORD; VAR phContext:LONGWORD):LONGWORD; stdcall; external 'WINSCARD';

*** Delphi equivalent? code END ***
Does not work at all

There are many functions in both the WINSCARD and SCARDSYN dynamic link library and many of which I have great problems with in regards to my Visual Basic to Delphi Parameter translation.

I suppose I am on the right track when I try to use pointers to parameters that need to go both ways, but I still experience many problems.

I would greatly appreciate any help and explanation in regards to the use of PCHAR
Also if there are any "simple" way of doing this. I thought this would be simple, but apparently not as simple as I thought. lol.

The functions I have tried to do so far is:

FUNCTION SCardEstablishContext(dwScope, pvReserved1, pvReserved2:LONGWORD; phContext:PCHAR):LONGWORD; stdcall; external 'WINSCARD';
FUNCTION SCardReleaseContext(hContext:LONGWORD):LONGWORD; stdcall; external 'WINSCARD';
FUNCTION SCardListReadersA(hContext:LONGWORD; mszGroups:BYTE; mszReaders, pcchReaders:PCHAR):LONGWORD; stdcall; external 'WINSCARD';
FUNCTION SCardConnectA(hContext:LONGWORD; szReader:STRING; dwShareMode, dwPreferredProtocols:LONGWORD; hCard, dwActiveProtocol:PCHAR):LONGWORD; stdcall; external 'WINSCARD';
FUNCTION SCardDisconnect(hCard,dwDisposition:LONGWORD):LONGWORD; stdcall; external 'WINSCARD';
FUNCTION SCardI2CInit(hCard:LONGWORD; VAR pCardParameters:LONGWORD; lType:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCardI2CReadData(hCard:LONGWORD; VAR pbReadBuffer:BYTE; ulReadBufferSize,ulAddress,ulBytesToRead:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCardI2CWriteData(hCard:LONGWORD; VAR pbWriteBuffer:BYTE; ulWriteBufferSize, ulAddress, ulBytesToWrite:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard2WBPReadData(hCard, ulBytesToRead:LONGWORD; VAR pbData:BYTE; ulAddress:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard2WBPReadProtectionMemory(hCard,ulDataLen:LONGWORD; VAR pbData:BYTE):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard2WBPWriteData(hCard,ulDataLen:LONGWORD; VAR pbData:BYTE; ulAddress:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard2WBPCompareAndProtect(hCard:LONGWORD; VAR bData:BYTE; ulAddress:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard2WBPPresentPIN(hCard,ulPINLen:LONGWORD; VAR pbPIN:BYTE):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard2WBPChangePIN(hCard,ulOldPINLen:LONGWORD; VAR pbOldPIN:BYTE; ulNewPINLen:LONGWORD; VAR pbNewPIN:BYTE):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard2WBPIsPinPresented(hCard:LONGWORD; VAR pfPinPresented:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard3WBPReadData(hCard, ulBytesToRead:LONGWORD; VAR pbData:BYTE; ulAddress:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard3WBPVerifyProtectBit(hCard, ulAddress:LONGWORD; VAR pfProtected:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard3WBPWriteData(hCard, ulDataLen:LONGWORD; VAR pbData:BYTE; ulAddress, fProtect:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard3WBPCompareAndProtect(hCard:LONGWORD; VAR bData:BYTE; ulAddress:LONGWORD):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard3WBPPresentPIN(hCard, ulPINLen:LONGWORD; VAR pbPIN:BYTE):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard3WBPChangePIN(hCard, ulOldPINLen:LONGWORD; VAR pbOldPIN:BYTE; ulNewPINLen:LONGWORD; VAR pbNewPIN:BYTE):LONGWORD; stdcall; external 'scardsyn';
FUNCTION SCard3WBPIsPinPresented(hCard:LONGWORD; VAR pfPinPresented:LONGWORD):LONGWORD; stdcall; external 'scardsyn';

I really need somebody to enlighten me a bit as in how to make the parameters work properly.

Many thanks in advance,
Morten Brendefur.

Recommended Answers

All 3 Replies

It is possible that the dll is not using stdcall functions. Have you tried cdecl for one ? I did not find a lot of info about this dll. Is there a way to get download the dll somewhere ?

If you do not have a header file for the functions, perhaps Borland's TDump.exe can help, although I don't know if it is included in D2009.

The DLL in question is part of the Microsoft Windows OS.
I have managed to find a little bit of information regarding the DLL, but not so much when searching for it. Searching for the functions however provide results.

I have found out that StringParameters does not work directly, but when I make:
TYPE
tbArr : ARRAY OF BYTE
and use this in the parameters ie FUNCTION Test(sString:tbArr):LONGWORD;

and then use the method

PROCEDURE DoSomething;
  VAR
    tekst:ARRAY[1..1024] OF BYTE;
    returnvalue:LONGWORD;
  BEGIN
    returnvalue:=Test(@tekst[low(tekst)]);
  END;

Then I mostly get the results I am after, but with the added disadvantage of having to convert the String into its array representation and back again

Maybe there is a simpler way :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.