Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added FirstTest.dcu
Binary file not shown.
36 changes: 25 additions & 11 deletions FirstTest.dfm
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
object Form1: TForm1
Left = 380
Top = 178
Width = 356
Height = 336
Caption = 'Form1'
ClientHeight = 553
ClientWidth = 811
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Expand Down Expand Up @@ -140,7 +139,7 @@ object Form1: TForm1
end
object bt_Close: TButton
Left = 5
Top = 65
Top = 105
Width = 62
Height = 19
Caption = 'Close'
Expand All @@ -158,20 +157,35 @@ object Form1: TForm1
end
object bt_Send: TButton
Left = 5
Top = 105
Top = 65
Width = 62
Height = 19
Caption = 'Send'
TabOrder = 5
OnClick = bt_SendClick
end
object Memo1: TMemo
Left = 75
Top = 130
Width = 271
Height = 176
Left = 8
Top = 192
Width = 793
Height = 353
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 6
end
object Button1: TButton
Left = 5
Top = 155
Width = 75
Height = 25
Caption = 'List'
TabOrder = 7
OnClick = Button1Click
end
object pcsc: TPCSCConnector
OnCardInserted = pcscCardInserted
OnCardActive = pcscCardActive
Expand All @@ -180,7 +194,7 @@ object Form1: TForm1
OnReaderWaiting = pcscReaderWaiting
OnReaderListChange = pcscReaderListChange
OnError = pcscError
Left = 315
Top = 5
Left = 227
Top = 13
end
end
102 changes: 98 additions & 4 deletions FirstTest.pas
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ TForm1 = class(TForm)
Memo1: TMemo;
Label13: TLabel;
Label14: TLabel;
Button1: TButton;
procedure pcscCardRemoved(Sender: TObject);
procedure pcscError(Sender: TObject; ErrSource: TErrSource; ErrCode: Cardinal);
procedure ShowData;
Expand All @@ -46,6 +47,7 @@ TForm1 = class(TForm)
procedure pcscReaderDisconnect(Sender: TObject);
procedure pcscReaderListChange(Sender: TObject);
procedure pcscReaderWaiting(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
Expand All @@ -63,6 +65,73 @@ implementation

HexChars = '0123456789abcdefABCDEF';

procedure ListSmartCardReaders(Memo: TMemo);
var
hContext: cardinal;
Readers: PChar;
ReaderList: TStringList;
ReaderListSize: integer;
Res: LongInt;
PtrReader: PChar;
qt:LongInt;
begin
Memo.Clear; // Limpa o memo antes de adicionar os leitores
Readers := nil;
ReaderListSize := 0;
ReaderList := TStringList.Create;

try
// Estabelece o contexto para comunica��o com o gerenciador de smartcard
Res := SCardEstablishContext(SCARD_SCOPE_USER, nil, nil, @hContext);
if Res <> SCARD_S_SUCCESS then
begin
Memo.Lines.Add('Erro ao estabelecer contexto: ' + IntToStr(Res));
Exit;
end;

// Obt�m o tamanho necess�rio para armazenar os leitores
Res := SCardListReadersW(hContext, nil,nil, ReaderListSize);
// RetVar := SCardListReadersA(FContext, nil, nil, ReaderListSize);
if (Res <> SCARD_S_SUCCESS) or (ReaderListSize = 0) then
begin
Memo.Lines.Add('Nenhum leitor encontrado ou erro: ' + IntToStr(Res));
Exit;
end;

// Aloca espa�o para armazenar a lista de leitores
GetMem(Readers, ReaderListSize);

try
// Obt�m a lista de leitores
Res := SCardListReadersW(hContext, nil, Pointer(Readers), ReaderListSize);
// SCardListReadersA(FContext, nil, Pointer(ReaderList), ReaderListSize);
if Res <> SCARD_S_SUCCESS then
begin
Memo.Lines.Add('Erro ao listar leitores: ' + IntToStr(Res));
Exit;
end;

// Adiciona os leitores � lista
PtrReader := Readers;
while PtrReader^ <> #0 do
begin
ReaderList.Add(PtrReader);
Inc(PtrReader, StrLen(PtrReader) + 1);
end;

// Exibe os leitores no TMemo
Memo.Lines.AddStrings(ReaderList);
finally
FreeMem(Readers);
end;

finally
// Libera o contexto
SCardReleaseContext(hContext);
ReaderList.Free;
end;
end;

function Hex2Bin(input: string): string;
var
hex, output: string;
Expand Down Expand Up @@ -93,13 +162,24 @@ function Bin2HexExt(const input:string; const spaces, upcase: boolean): string;
else result := AnsiLowerCase(hexresult);
end;

function AnsiToWide(const AnsiStr: AnsiString; CodePage: Cardinal = CP_ACP ): WideString;
var
Len: Integer;
AnsiReader: AnsiString;
begin
Len := MultiByteToWideChar(CodePage, 0, PAnsiChar(AnsiStr), -1, nil, 0);
SetLength(Result, Len - 1);
MultiByteToWideChar(CodePage, 0, PAnsiChar(AnsiStr), -1, PWideChar(Result), Len);
end;

procedure TForm1.ShowData;
begin
label3.caption := IntToHex(pcsc.ReaderState,8);
label4.caption := pcsc.AttrICCType;
label5.caption := pcsc.AttrVendorName;
label6.caption := pcsc.AttrVendorSerial;
label14.caption := IntToHex(pcsc.AttrProtocol,8);
label14.caption := IntToHex(pcsc.AttrProtocol,8)+' ATR:'+Bin2HexExt(pcsc.AttrCardATR,true,true);

end;

procedure TForm1.pcscCardRemoved(Sender: TObject);
Expand All @@ -110,17 +190,21 @@ procedure TForm1.pcscCardRemoved(Sender: TObject);

procedure TForm1.pcscError(Sender: TObject; ErrSource: TErrSource; ErrCode: Cardinal);
begin
if memo1.Lines[memo1.Lines.Count-1]='OnError ' + IntToHex(ErrCode,8) then exit;
memo1.Lines.Add('OnError ' + IntToHex(ErrCode,8));
label1.caption := IntToHex(ErrCode,8);
ShowData;
end;


procedure TForm1.bt_InitClick(Sender: TObject);
var i:integer;
begin
pcsc.Init;
pcsc.UseReaderNum := 0;
end;


procedure TForm1.bt_OpenClick(Sender: TObject);
begin
if pcsc.Open then memo1.lines.add('OPEN: OK')
Expand All @@ -145,13 +229,22 @@ procedure TForm1.bt_DisconnectClick(Sender: TObject);

procedure TForm1.bt_SendClick(Sender: TObject);
begin
label2.caption := Bin2HexExt(pcsc.GetResponseFromCard(Hex2Bin('a0f2000016')), true, true);
label2.caption := Bin2HexExt(pcsc.GetResponseFromCard(Hex2Bin('a0f2000016')), true, true);
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
memo1.Lines.Add(inttostr(pcsc.ReaderList.Count));
for i:=0 to pcsc.ReaderList.Count-1 do begin
memo1.Lines.Add(inttostr(i)+':'+pcsc.ReaderList[i]);
end;
end;

procedure TForm1.pcscCardActive(Sender: TObject);
begin
memo1.Lines.Add('OnCardActive');
ShowData;
memo1.Lines.Add('OnCardActive');
ShowData;
end;

procedure TForm1.pcscCardInserted(Sender: TObject);
Expand Down Expand Up @@ -189,3 +282,4 @@ procedure TForm1.pcscReaderWaiting(Sender: TObject);
end;

end.

Binary file modified PCSCConnector.dcu
Binary file not shown.
14 changes: 8 additions & 6 deletions PCSCConnector.pas
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ implementation
var
ActReaderState : cardinal;
LastReaderState : cardinal;
SelectedReader : PChar;
SelectedReader : String;
ReaderOpen : boolean;
NotifyHandle : HWND;

Expand Down Expand Up @@ -257,12 +257,14 @@ function CardWatcherThread(PContext: pointer): integer;
var
RetVar : cardinal;
RContext : cardinal;
AnsiReader: AnsiString;
RStates : array[0..1] of SCARD_READERSTATEA;
begin
try
RContext := cardinal(PContext^);
FillChar(RStates,SizeOf(RStates),#0);
RStates[0].szReader := SelectedReader;
AnsiReader := AnsiString(SelectedReader);
RStates[0].szReader := PAnsichar(AnsiReader);
RStates[0].pvUserData := nil;
RStates[0].dwEventState := ActReaderState;
while ReaderOpen do
Expand Down Expand Up @@ -334,11 +336,11 @@ function TPCSCConnector.Init: boolean;
if RetVar = SCARD_S_SUCCESS then
begin
ReaderListSize := 0;
RetVar := SCardListReadersA(FContext, nil, nil, ReaderListSize);
RetVar := SCardListReadersW(FContext, nil, nil, ReaderListSize);
if RetVar = SCARD_S_SUCCESS then
begin
SetLength(ReaderList, ReaderListSize);
SCardListReadersA(FContext, nil, Pointer(ReaderList), ReaderListSize);
SCardListReadersW(FContext, nil, Pointer(ReaderList), ReaderListSize);
FReaderList.Clear;
SortOutSubstrings(ReaderList,v,[#0]);
for i := 0 to MAXIMUM_SMARTCARD_READERS do
Expand Down Expand Up @@ -405,8 +407,8 @@ function TPCSCConnector.ConnectSelectedReader: boolean;
var
RetVar : cardinal;
begin
RetVar := SCardConnectA(FContext,
SelectedReader,
RetVar := SCardConnectW(FContext,
PWideChar(SelectedReader),
SCARD_SHARE_EXCLUSIVE,
SCARD_PROTOCOL_Tx,
FCardHandle,
Expand Down
Binary file modified PCSCConnectorD2007.bpl
Binary file not shown.
Binary file modified PCSCConnectorD2007.dcp
Binary file not shown.
3 changes: 3 additions & 0 deletions PCSCConnectorD2007.dpk
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package PCSCConnectorD2007;

{$R *.res}
{$R *.otares}
{$R 'PCSCConnector.dcr'}
{$IFDEF IMPLICITBUILDING This IFDEF should not be used by users}
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
Expand All @@ -23,6 +25,7 @@ package PCSCConnectorD2007;
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$ENDIF IMPLICITBUILDING}
{$DESCRIPTION 'PCSC SmartCard Component D2007'}
{$IMPLICITBUILD ON}

Expand Down
Loading