I just bought a NFC ACR122U. It comes with samples to delphi 7.
I am using delphi XE8 and compiling the sample to 32 bits/win 8.1.
I did the correct changes(I believe) to adapt the api and sample project functions to delphi Xe8, replacing Pchar to PAnsiChar and Char to AnsiChar where needed.
I am using native win 8 drivers, no manufacturer drive.
I can initialize the device and get the device name correctly with:
procedure TfrmDevProg.btnInitClick(Sender: TObject);
var index: integer;
begin
//Establish context
retCode := SCardEstablishContext(SCARD_SCOPE_USER,
nil,
nil,
@hContext);
if retCode <> SCARD_S_SUCCESS then begin
displayout(GetScardErrMsg(retcode),2);
Exit;
end ;
//List PC/SC readers installed in the system
BufferLen := MAX_BUFFER_LEN;
retCode := SCardListReadersA(hContext,
nil,
@Buffer,
@BufferLen);
if retCode <> SCARD_S_SUCCESS then begin
DisplayOut(getscarderrmsg(retCode),2);
Exit;
end;
btnInit.Enabled := false;
btnConnect.Enabled := true;
LoadListToControl(cbReader,@buffer,bufferLen);
// Look for ACR128 PICC and make it the default reader in the combobox
for index := 0 to cbReader.Items.Count-1 do begin
cbReader.ItemIndex := index;
if AnsiPos('ACR122U PICC', cbReader.Text) > 0 then
Exit;
end;
cbReader.ItemIndex := 0;
end;
The procedure above works very well. Next, I use the next code to connect to device:
procedure TfrmDevProg.btnConnectClick(Sender: TObject);
begin
//Connect to reader using a shared connection
retCode := SCardConnectA(hContext,
PAnsiChar(cbReader.Text),
SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1,
@hCard,
@dwActProtocol);
if retcode <> SCARD_S_SUCCESS then begin
displayout(GetScardErrMsg(retcode),2)
end
else begin
displayout('Successful connection to ' + cbReader.Text, 1)
end;
end;
Here, I am getting an error from SCardConnectA: "The specified reader name is not recognized." and the retcode var is: -2146435063.
Here is a snippet code of the api copied from DVD sent with the device, when I bought it:
///////////////////////////////////////////////////////////////////////////////
// Imported functions from Winscard.dll (WIN32 API)
///////////////////////////////////////////////////////////////////////////////
Function SCardEstablishContext(dwscope :DWORD;
pvReserved1: LPCVOID;
pvReserved2: LPCVOID;
phContext :LPSCARDCONTEXT):LONG; stdcall; external 'Winscard.dll';
Function SCardReleaseContext(hContext:SCARDCONTEXT):LONG; stdcall; external 'Winscard.dll';
Function SCardListReadersA(hContext : SCARDCONTEXT;
mszGroups:LPCSTR;
szReaders:LPSTR;
pcchReaders:LPDWORD):LONG; stdcall; external 'Winscard.dll';
//Note : ScardConnectA is for non-UNICODE characters which is only one byte.
// For UNICODE characters it is SCardConnectW. Special processing is
// required for UNICODE. Be careful!
Function SCardConnectA(hContext : SCARDCONTEXT;
szReaders:LPSTR;
dwShareMode : DWORD;
dwPreferredProtocols : DWORD;
phCard : LPSCARDHANDLE;
pdwActiveProtocols:LPDWORD):LONG; stdcall; external 'Winscard.dll';
I downloaded an binary app from mannufacturer site to test the device and all works well. But I need do it work with my Delphi app.
Any help, please.
the problem is the cast from cbReader.Text do PAnsiChar. fix it to
retCode := SCardConnectA(hContext, PAnsiChar(AnsiString(cbReader.Text)), SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1, @hCard, @dwActProtocol);