Einzelnen Beitrag anzeigen

Dr.MAD

Registriert seit: 16. Jan 2006
18 Beiträge
 
#1

Massives Problem mit MCardInitialize = ???

  Alt 15. Mär 2011, 23:01
Hallo Leute,

mir tun von der Fehlersuche schon die Augen weh.

Ich bekomme von der function MCardInitialize immer den Facility-Status MCARD_E_BITORDER_CHANGED zurück.

Damit kann ich bei der Initiierung nix anfangen?!?

Hier die MCard DLL API Specification für MCSCM.dll http://www.txsystems.com/downloads/M...ication1.9.pdf
Die MCSCM.dll ist im Anhang.

Evtl. auch Wichtig scarderr.h: http://source.winehq.org/source/include/scarderr.h

Seht euch mal meinen Code an ob euch was einfällt (Win7 x64 D2010):
Delphi-Quellcode:
const
  DEFAULT_BUFFER_SIZE = 255;


const
  SCARD_AUTOALLOCATE = -1; // Flag to have Resource Manager allocate buffer space
  SCARD_SCOPE_USER = 0; // The context is a user context, and any database operations are performed within the domain of the user.
  SCARD_SCOPE_TERMINAL = 1; // The context is that of the current terminal,and any database operations are performed within the domain of that terminal. (The calling application must have appropriate access permissions for any database actions.)
  SCARD_SCOPE_SYSTEM = 2; // The context is the system context, and any database operations are performed within the domain of the system. (The calling application must have appropriate access permissions for any database actions.)



type
// Values are 32 bit values laid out as follows:
//
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
// Sev - is the severity code
//
// 00 - Success
// 01 - Informational
// 10 - Warning
// 11 - Error
//
// C - is the Customer code flag
//
// R - is a reserved bit
//
// Facility - is the facility code
//
// Code - is the facility's status code

  TSCardSeverityCodes = (
    STATUS_SEVERITY_SUCCESS,
    STATUS_SEVERITY_INFORMATIONAL,
    STATUS_SEVERITY_WARNING,
    STATUS_SEVERITY_ERROR
  );

  TSCardFacilityCodes = (
    FACILITY_SYSTEM, // = 0
    FCundef1, FCundef2, FCundef3, FCundef4, FCundef5, FCundef6, FCundef7, FCundef8, FCundef9,
    FCundef10, FCundef11, FCundef12, FCundef13, FCundef14, FCundef15,
    FACILITY_SCARD // = 16
  );

  TSCardFacilityStatus = (
    SCARD_S_SUCCESS, // = 0 = NO_ERROR
    SCARD_F_INTERNAL_ERROR,
    SCARD_E_CANCELLED,
    SCARD_E_INVALID_HANDLE,
    SCARD_E_INVALID_PARAMETER,
    SCARD_E_INVALID_TARGET,
    SCARD_E_NO_MEMORY,
    SCARD_F_WAITED_TOO_LONG,
    SCARD_E_INSUFFICIENT_BUFFER,
    SCARD_E_UNKNOWN_READER,
    SCARD_E_TIMEOUT, // = 10
    SCARD_E_SHARING_VIOLATION,
    SCARD_E_NO_SMARTCARD,
    SCARD_E_UNKNOWN_CARD,
    SCARD_E_CANT_DISPOSE,
    SCARD_E_PROTO_MISMATCH,
    SCARD_E_NOT_READY,
    SCARD_E_INVALID_VALUE,
    SCARD_E_SYSTEM_CANCELLED,
    SCARD_F_COMM_ERROR,
    SCARD_F_UNKNOWN_ERROR, // = 20
    SCARD_E_INVALID_ATR,
    SCARD_E_NOT_TRANSACTED,
    SCARD_E_READER_UNAVAILABLE,
    SCARD_P_SHUTDOWN,
    SCARD_E_PCI_TOO_SMALL,
    SCARD_E_READER_UNSUPPORTED,
    SCARD_E_DUPLICATE_READER,
    SCARD_E_CARD_UNSUPPORTED,
    SCARD_E_NO_SERVICE,
    SCARD_E_SERVICE_STOPPED, // = 30
    SCARD_E_UNEXPECTED,
    SCARD_E_ICC_INSTALLATION,
    SCARD_E_ICC_CREATEORDER,
    SCARD_E_UNSUPPORTED_FEATURE,
    SCARD_E_DIR_NOT_FOUND,
    SCARD_E_FILE_NOT_FOUND,
    SCARD_E_NO_DIR,
    SCARD_E_NO_FILE,
    SCARD_E_NO_ACCESS,
    SCARD_E_WRITE_TOO_MANY, // = 40
    SCARD_E_BAD_SEEK,
    SCARD_E_INVALID_CHV,
    SCARD_E_UNKNOWN_RES_MNG,
    SCARD_E_NO_SUCH_CERTIFICATE,
    SCARD_E_CERTIFICATE_UNAVAILABLE,
    SCARD_E_NO_READERS_AVAILABLE,
    SCARD_E_COMM_DATA_LOST,
    SCARD_E_NO_KEY_CONTAINER,
    SCARD_E_SERVER_TOO_BUSY, // = 49
    SCundef50, SCundef51, SCundef52, SCundef53, SCundef54, SCundef55, SCundef56, SCundef57, SCundef58, SCundef59,
    SCundef60, SCundef61, SCundef62, SCundef63, SCundef64, SCundef65, SCundef66, SCundef67, SCundef68, SCundef69,
    SCundef70, SCundef71, SCundef72, SCundef73, SCundef74, SCundef75, SCundef76, SCundef77, SCundef78, SCundef79,
    SCundef80, SCundef81, SCundef82, SCundef83, SCundef84, SCundef85, SCundef86, SCundef87, SCundef88, SCundef89,
    SCundef90, SCundef91, SCundef92, SCundef93, SCundef94, SCundef95, SCundef96, SCundef97, SCundef98, SCundef99,
    SCundef100,
    SCARD_W_UNSUPPORTED_CARD, // = 101
    SCARD_W_UNRESPONSIVE_CARD,
    SCARD_W_UNPOWERED_CARD,
    SCARD_W_RESET_CARD,
    SCARD_W_REMOVED_CARD,
    SCARD_W_SECURITY_VIOLATION,
    SCARD_W_WRONG_CHV,
    SCARD_W_CHV_BLOCKED,
    SCARD_W_EOF,
    SCARD_W_CANCELLED_BY_USER, // = 110
    SCARD_W_CARD_NOT_AUTHENTICATED,
    SCARD_W_CACHE_ITEM_NOT_FOUND,
    SCARD_W_CACHE_ITEM_STALE,
    SCARD_W_CACHE_ITEM_TOO_BIG // = 114
  );

  // The base value for MCARD_xxx error codes is 0x90100800, which is in fact the SCARD_xxx base value
  // with the COSTOMER_CODE_FLAG set and an additional offset of 0x800 added.

  TMCardFacilityStatus = (
    MCARD_S_SUCCESS, // = 0
    MCARD_E_INTERNAL_ERROR,
    MCARD_E_NOT_IMPLEMENTED,
    MCARD_E_NOT_INITIALIZED,
    MCARD_E_INCOMPATIBLE_READER,
    MCARD_E_UNKNOWN_CARD,
    MCARD_E_BUFFER_TOO_SMALL,
    MCARD_E_INVALID_PARAMETER,
    MCARD_E_READ_ONLY_ATTRIBUTE,
    MCARD_E_INVALID_HANDLE,
    MCARD_E_PROTOCOL_MISMATCH, // = 10
    MCARD_E_PROTOCOL_ERROR,
    MCARD_E_CHAL_RESP_FAILED,
    MCARD_E_INVALID_MEMORY_RANGE,
    MCARD_E_INVALID_MEMORY_ZONE_ID,
    MCARD_E_INVALID_PIN_ID,
    MCARD_E_INVALID_CHAL_RESP_ID,
    MCARD_E_ERASURE_NEEDED,
    MCARD_E_BITORDER_CHANGED,
    MCARD_W_NOT_ALL_DATA_READ,
    MCARD_W_NOT_ALL_DATA_WRITTEN, // = 20
    MCARD_W_PIN_VERIFY_NEEDED,
    MCARD_W_PIN_VERIFY_FAILED,
    MCARD_W_NO_PIN_ATTEMPTS_LEFT,
    MCARD_W_NO_UNITS_TO_DECREMENT,
    MCARD_W_REMOVED_CARD,
    MCARD_W_TIMEOUT,
    MCARD_W_ABORTED,
    MCARD_W_PROTECTED_AREA // = 28
  );



type
  SCARDCONTEXT = ULONG_PTR; // Cardinal
  PSCARDCONTEXT = ^SCARDCONTEXT;
  LPSCARDCONTEXT = ^SCARDCONTEXT;

  function SCardListReadersW(hContext: SCARDCONTEXT; mszGroups: LPCSTR; mszReaders: LPTSTR; pcchReaders: PDWORD): ULONG; stdcall; external 'WinSCard.dll';

  function SCardEstablishContext(dwScope: DWORD; pvReserved1: Pointer; pvReserved2: Pointer; phContext: LPSCARDCONTEXT): ULONG; stdcall; external 'WinSCard.dll';
  function SCardReleaseContext(hContext: SCARDCONTEXT): ULONG; stdcall; external 'WinSCard.dll';

  function SCardFreeMemory(hContext: SCARDCONTEXT; pvMem: Pointer): ULONG; stdcall; external 'WinSCard.dll';



type
  PMCARDCONTEXT = ^MCARDCONTEXT;
  MCARDCONTEXT = SCARDCONTEXT; // SCARDHANDLE

  PMCARDHANDLE = ^MCARDHANDLE;
  MCARDHANDLE = SCARDCONTEXT; // SCARDHANDLE

  PMCARD_FEATURES = ^MCARD_FEATURES;
  MCARD_FEATURES = Record
                     dwFlags: DWORD;
                     bytMemoryZones: BYTE;
                     byPINs: BYTE;
                     byCounters: BYTE;
                     byCRs: BYTE;
                   end;

  PMCARD_MEMORY = ^MCARD_MEMORY;
  MCARD_MEMORY = Record
                   dwFlags: DWORD;
                   dwSize: DWORD;
                 end;

  PMCARD_PIN = ^MCARD_PIN;
  MCARD_PIN = Record
                dwFlags: DWORD;
                bySize: BYTE;
                byRetries: BYTE;
              end;

  PMCARD_CR = ^MCARD_CR;
  MCARD_CR = Record
               dwFlags: DWORD;
               dwChallengeLen: DWORD;
               dwResponseLen: DWORD;
               byRetries: BYTE;
             end;

  PMCARD_COUNTER = ^MCARD_COUNTER;
  MCARD_COUNTER = Record
                    dwFlags: DWORD;
                    bySize: BYTE;
                    dwUnits: DWORD;
                  end;

  function MCardInitialize(hScardContext: SCARDCONTEXT; szReaderName: LPCTSTR; phMCardContext: PMCARDCONTEXT; pdwDllVersion: PDWORD): ULONG; stdcall; external 'MCSCM.dll'; // MCard DLL API von SCM Microsystems (Chipdrive)
  function MCardShutdown(hMCardContext: MCARDCONTEXT): ULONG; stdcall; external 'MCSCM.dll';
  function MCardConnect(hMCardContext: MCARDCONTEXT; dwConnectMode: DWORD; byCardType: BYTE; phMCard: PMCARDHANDLE): ULONG; stdcall; external 'MCSCM.dll';
  function McardDisconnect(hMCard: MCARDHANDLE; dwDisposition: DWORD): ULONG; stdcall; external 'MCSCM.dll';
  function McardGetAttrib(hMCard: MCARDHANDLE; dwAttrId: DWORD; pbAttr: LPBYTE; pcbAttrLen: LPDWORD): ULONG; stdcall; external 'MCSCM.dll';
  function McardSetAttrib(hMCard: MCARDHANDLE; dwAttrId: DWORD; pbAttr: LPBYTE; cbAttrLen: DWORD): ULONG; stdcall; external 'MCSCM.dll';
  function MCardReadMemory(hMCard: MCARDHANDLE; bMemZone: BYTE; dwOffset: DWORD; pbReadBuffer: LPBYTE; pbReadLen: LPDWORD): ULONG; stdcall; external 'MCSCM.dll';
  function McardWriteMemory(hMCard: MCARDHANDLE; bMemZone: BYTE; dwOffset: DWORD; pbWriteBuffer: LPBYTE; pcbWriteLen: LPDWORD): ULONG; stdcall; external 'MCSCM.dll';
  function McardSetMemoryWriteProtection(hMCard: MCARDHANDLE; bMemZone: BYTE; dwOffset: DWORD; pbcLen: LPDWORD): ULONG; stdcall; external 'MCSCM.dll';
  function McardSetMemoryReadProtection(hMCard: MCARDHANDLE; bMemZone: BYTE; dwOffset: DWORD; pbcLen: LPDWORD): ULONG; stdcall; external 'MCSCM.dll';
  function McardReadMemoryStatus(hMCard: MCARDHANDLE; bMemZone: BYTE; dwOffset: DWORD; pbBuffer: PBYTE; pcbLen: LPDWORD): ULONG; stdcall; external 'MCSCM.dll';
  function MCardVerifyPIN(hMCard: MCARDHANDLE; bPinNumber: BYTE; pbBufferWithPIN: PBYTE; pbcLen: BYTE): ULONG; stdcall; external 'MCSCM.dll';
  function MCardChangePIN(hMCard: MCARDHANDLE; bPinNumber: BYTE; pbBufferWithOldPIN: PBYTE; cbOldLen: BYTE; pbBufferWithNewPIN: PBYTE; cbNewLen: BYTE): ULONG; stdcall; external 'MCSCM.dll';
  function McardChallengeResponse(hMCard: MCARDHANDLE; bChallengeID: BYTE; pbChallengeBuffer: PBYTE; cbChallengeLen: BYTE; pbResponseBuffer: PBYTE; cbResponseLen: PBYTE): ULONG; stdcall; external 'MCSCM.dll';
  function MCardDeductCounter(hMCard: MCARDHANDLE; bCounterID: BYTE; dwUnits: DWORD): ULONG; stdcall; external 'MCSCM.dll';
  function MCardSetCounter(hMCard: MCARDHANDLE; bCounterID: BYTE; pbCounter: PBYTE; cbCounterLen: BYTE): ULONG; stdcall; external 'MCSCM.dll';
  function MCardWaitForCardState(hMCard: MCARDHANDLE; dwExpectedCardState: DWORD; pdwCardState: PDWORD; dwTimeOut: DWORD): ULONG; stdcall; external 'MCSCM.dll';



implementation

{$R *.dfm}




function ConvertDecToHex(const Dec: Int64; var Hex: String; const MinHexDigits: Byte): Boolean;
var n: Int64;
    c: Char;
begin
  Result:=False;
  Hex:='';
  n:=Dec;
  while n<>0 do begin
   if n mod 16 < 10 then c:=chr(n mod 16+ord('0')) else c:=chr(n mod 16+ord('A')-10);
   Hex:=c+Hex;
   n:=n div 16;
  end;
  while Length(Hex)<MinHexDigits do Hex:='0'+Hex;
  Result:=True;
end;




function GetSorMCardReturnCode(Code: Cardinal): String;
var SeverityCode: Byte; // Bit 1 und 2 ( 2 Bit's)
    // CustomerFlag: Boolean; // Bit 3 ( 1 Bit)
    // ReservedBit: Boolean; // Bit 4 ( 1 Bit)
    FacilityCode: Word; // Bit 5 - 16 (12 Bit's)
    FacilityStatus: Word; // Bit 17 - 32 (16 Bit's)
    Hex: String;
begin
  // Severity code - Die ersten beiden Bit's um 30 Bit's nach rechts verschieben
  SeverityCode:=Code shr 30;
  // Bit 1 - 4 entfernen, also alles um 4 Bit's nach links verschieben
  Code:=Code shl 4;
  // ... und wieder 4 zurück nach rechts
  Code:=Code shr 4;
  // FacilityCode - Bit 5 - 16 um 16 Bit's nach rechts verschieben
  FacilityCode:=Code shr 16;
  // Bit 1- 16 entfernen, also alles um 16 Bit's nach links verschieben
  Code:=Code shl 16;
  // ... und wieder 16 zurück nach rechts
  Code:=Code shr 16;
  //Result vorerfassen
  Result:=GetEnumName(TypeInfo(TSCardSeverityCodes),SeverityCode)+' | '+
          GetEnumName(TypeInfo(TSCardFacilityCodes),FacilityCode)+' | ';
  // FacilityStatus hinzufügen
  if Code<2048 then begin // S Card - FacilityStatus
    FacilityStatus:=Code;
    Result:=Result+GetEnumName(TypeInfo(TSCardFacilityStatus),FacilityStatus);
  end else begin // M Card - FacilityStatus
    FacilityStatus:=Code-2048; // 2048 = 0x800
    ConvertDecToHex(FacilityStatus,Hex,2);
    Result:=Result+GetEnumName(TypeInfo(TMCardFacilityStatus),FacilityStatus)+'('+IntToStr(FacilityStatus)+'=0x'+Hex+')';
  end;
end;




function GetSmartCardReadersList(var SmartCardReaders: TStringList): Cardinal;
var X: Integer;
    Size: Cardinal;
    WideAry: Array of WideChar;
    TmpStr: String;
begin
  // Evtl. StringList leeren
  SmartCardReaders.Clear;
  // Größe für Array einlesen
  Result:=SCardListReadersW(0, nil, nil, @Size);
  if (Result=0) and (Size>0) then begin
    // Speicher reservieren
    SetLength(WideAry, Size);
    // Reader-Liste als Multi-String (xxxxx+#0+xxxxx+#0+xxxxx+#0+#0) einlesen
    Result:=SCardListReadersW(0, nil, @WideAry[0], @Size);
    if Result=0 then begin
      // Multi-String -> StringList
      for X:=Low(WideAry) to High(WideAry) do begin
        if WideAry[X]<>#0 then TmpStr:=TmpStr+WideAry[X] else begin
          if TmpStr<>'then SmartCardReaders.Add(TmpStr);
          TmpStr:='';
        end;
      end;
    end;
    // Speicher freigeben
    SetLength(WideAry, 0);
  end;
end;





procedure TForm1.Button1Click(Sender: TObject);
var slReaders: TStringList;
    ret: Cardinal;
    Hex: String;
    hSCard: SCARDCONTEXT;
    PReader: PWideChar;
    PhMCard: PMCARDCONTEXT;
    MCardDllVer: DWORD;
begin
  Memo1.Clear;
  slReaders:=TStringList.Create;

  // Alle SmartCard-Lesegeräte erfassen
  GetSmartCardReadersList(slReaders);
  Memo1.Lines.Add('Erfasste SmartCard-Lesegeräte:');
  Memo1.Lines.Add(slReaders.Text);

  if slReaders.Count>0 then begin

    // Ressourcenmanager einbinden
    ret:=SCardEstablishContext(0, nil, nil, @hSCard); // SCARD_SCOPE_USER | SCARD_SCOPE_SYSTEM
    Memo1.Lines.Add('Ressourcenmanager einbinden - ret: '+IntToStr(ret));
    ConvertDecToHex(hSCard,Hex,8);
    Memo1.Lines.Add('Ressourcenmanager einbinden - hSCard: '+IntToStr(hSCard)+' = '+Hex);
    Memo1.Lines.Add('');

    if ret=0 then begin

      // MCard starten
      PReader:=PWideChar(slReaders.Strings[0]);
      Memo1.Lines.Add('Initiiere '+String(PReader));
      ret:=MCardInitialize(0, PReader, PhMCard, @MCardDllVer);
      ConvertDecToHex(ret, Hex, 8);
      Memo1.Lines.Add('MCard starten - ret: '+IntToStr(ret)+' = '+Hex+' = '+GetSorMCardReturnCode(ret));
      //Memo1.Lines.Add('MCard starten - hMCard: '+IntToStr(PhMCard^));
      Memo1.Lines.Add('MCard starten - MCardDllVer: '+IntToStr(MCardDllVer));
      Memo1.Lines.Add('');

      if ret=0 then begin


        // ...


        // MCard beenden
        ret:=MCardShutdown(PhMCard^);
        ConvertDecToHex(ret, Hex, 8);
        Memo1.Lines.Add('MCard beenden - ret: '+IntToStr(ret)+' = '+Hex+' = '+GetSorMCardReturnCode(ret));
        Memo1.Lines.Add('');

      end;

      // Ressourcenmanager lösen
      ret:=SCardReleaseContext(hSCard);
      Memo1.Lines.Add('Ressourcenmanager lösen - ret: '+IntToStr(ret));
      Memo1.Lines.Add('');

    end;
  end;
  slReaders.Free;
end;



Als Anleitung dient mir dieser VB-Code :

Code:
VERSION 5.00
Begin VB.Form Form1 
   BorderStyle    =  3  'Fixed Dialog
   Caption        =  "MCARD VB Sample"
   ClientHeight   =  2940
   ClientLeft     =  3630
   ClientTop      =  2070
   ClientWidth    =  10710
   LinkTopic      =  "Form1"
   MaxButton      =  0   'False
   MinButton      =  0   'False
   PaletteMode    =  1  'UseZOrder
   ScaleHeight    =  2940
   ScaleWidth     =  10710
   ShowInTaskbar  =  0   'False
   Begin VB.TextBox Text1 
      Height         =  375
      Left           =  2880
      TabIndex       =  3
      Text           =  "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0"
      Top            =  120
      Width          =  7695
   End
   Begin VB.Frame Frame1 
      Caption        =  "Data OUT"
      Height         =  1935
      Left           =  3840
      TabIndex       =  1
      Top            =  840
      Width          =  6735
      Begin VB.Label Label1 
         BorderStyle    =  1  'Fixed Single
         Caption        =  "Data"
         Height         =  1575
         Left           =  120
         TabIndex       =  2
         Top            =  240
         Width          =  6495
      End
   End
   Begin VB.CommandButton Command1 
      Caption        =  "Test"
      Height         =  420
      Left           =  120
      TabIndex       =  0
      Top            =  120
      Width          =  1335
   End
   Begin VB.Label Label5 
      Caption        =  "Label5"
      Height         =  255
      Left           =  120
      TabIndex       =  8
      Top            =  1800
      Width          =  2895
   End
   Begin VB.Label Label4 
      Caption        =  "Label4"
      Height         =  255
      Left           =  120
      TabIndex       =  7
      Top            =  1440
      Width          =  2895
   End
   Begin VB.Label LabelReader
      Caption        =  "Reader: "
      Height         =  255
      Left           =  1920
      TabIndex       =  6
      Top            =  240
      Width          =  855
   End
   Begin VB.Label Label3 
      Caption        =  "Label3"
      Height         =  255
      Left           =  120
      TabIndex       =  5
      Top            =  1080
      Width          =  2895
   End
   Begin VB.Label Label2 
      Caption        =  "Label2"
      Height         =  375
      Left           =  120
      TabIndex       =  4
      Top            =  720
      Width          =  2895
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'*********************************************************************
'**
'** MCARD API Visual Basic Sample
'**
'** (c) SCM Microsystems, 2003
'**
'** http://www.scmmicro.de
'**
'*********************************************************************

Const MCARDTYPE_SLE4428 = &H3
Const MCARDTYPE_SLE4442 = &H6

Const MCARD_ATTR_TYPE = &H0

' Memory card specific error codes
Const SCARD_CODE_BASE = &H80100000
Const COSTOMER_CODE_FLAG = &H20000000
Const MCARD_OFFSET = &H800
Const MCARD_E_OFFSET = &H0
Const MCARD_W_OFFSET = &H65
Const MCARD_CODE_BASE = ((SCARD_CODE_BASE + COSTOMER_CODE_FLAG) + MCARD_OFFSET)
Const MCARD_E_CODE_BASE = (MCARD_CODE_BASE + MCARD_E_OFFSET)
Const MCARD_W_CODE_BASE = (MCARD_CODE_BASE + MCARD_W_OFFSET)
' success code
Const MCARD_S_SUCCESS = &H0
' MCARD_E_xxx error codes (indicate the "bad" errors)
' 0xA0100801: an internal error has occured
Const MCARD_E_INTERNAL_ERROR = MCARD_E_CODE_BASE + &H1
' 0xA0100802: function not implemented
Const MCARD_E_NOT_IMPLEMENTED = MCARD_E_CODE_BASE + &H2
' 0xA0100803: MCardInitialize not called
Const MCARD_E_NOT_INITIALIZED = MCARD_E_CODE_BASE + &H3
' 0xA0100804: this DLL does not work with the specified reader
Const MCARD_E_INCOMPATIBLE_READER = MCARD_E_CODE_BASE + &H4
' 0xA0100805: could not identify card
Const MCARD_E_UNKNOWN_CARD = MCARD_E_CODE_BASE + &H5
' 0xA0100811: the buffer for return daa is too small
Const MCARD_E_BUFFER_TOO_SMALL = MCARD_E_CODE_BASE + &H11
' 0xA0100812: one or more parameters are invalid
Const MCARD_E_INVALID_PARAMETER = MCARD_E_CODE_BASE + &H12
' 0xA0100821: protocoll error while connecting to card
Const MCARD_E_PROTO_MISMATCH = MCARD_E_CODE_BASE + &H21
' 0xA0100822: protocol error during card access
Const MCARD_E_PROTOCOL_ERROR = MCARD_E_CODE_BASE + &H22
' 0xA0100827: Challenge Response Failed
Const MCARD_E_CHAL_RESP_FAILED = MCARD_E_CODE_BASE + &H23
' 0xA0100826: Invalid memory range
Const MCARD_E_INVALID_MEMORY_RANGE = MCARD_E_CODE_BASE + &H24
' 0xA0100831: specified memory zone ID is invalid for current card
Const MCARD_E_INVALID_MEMORY_ZONE_ID = MCARD_E_CODE_BASE + &H31
' 0xA0100832: specified PIN ID is invalid for current card
Const MCARD_E_INVALID_PIN_ID = MCARD_E_CODE_BASE + &H32
' 0xA0100833: spezcified challenge/response ID is invalid for current card
Const MCARD_E_INVALID_CHAL_RESP_ID = MCARD_E_CODE_BASE + &H33

' MCARD_W_xxx warning codes (a problem occured, but it's up to the
'                           application to decide how bad it is)

' 0xA0100866: could not read all data from card
Const MCARD_W_NOT_ALL_DATA_READ = MCARD_W_CODE_BASE + &H1
' 0xA0100867: could not write all data to card
Const MCARD_W_NOT_ALL_DATA_WRITTEN = MCARD_W_CODE_BASE + &H2
' 0xA0100876: PIN must be verified before access is possible
Const MCARD_W_PIN_VERIFY_NEEDED = MCARD_W_CODE_BASE + &H11
' 0xA0100877: PIN verification failed
Const MCARD_W_PIN_VERIFY_FAILED = MCARD_W_CODE_BASE + &H12
' 0xA0100878: no PIN verification attempts left, card probably locked
Const MCARD_W_NO_PIN_ATTEMPTS_LEFT = MCARD_W_CODE_BASE + &H13
' 0xA0100879: no units left in the card to decrement
Const MCARD_W_NO_UNITS_TO_DECREMENT = MCARD_W_CODE_BASE + &H14

'*********************************************************************
Private Declare Function SCardEstablishContext Lib "WINSCARD.DLL" ( _
  ByVal swScope As Long, _
  ByVal pvReserved1 As Long, _
  ByVal pvReserved2 As Long, _
  ByRef phSCardContext As Long _
) As Long
'*********************************************************************
Private Declare Function SCardReleaseContext Lib "WINSCARD.DLL" ( _
  ByVal hSCardContext As Long _
) As Long
'*********************************************************************
Private Declare Function MCardInitialize Lib "MCSCM.DLL" ( _
  ByVal hSCardContext As Long, _
  ByVal scReaderName As String, _
  ByRef phMCardContext As Long, _
  ByRef pdwDLLVersion As Long _
) As Long
'*********************************************************************
Private Declare Function MCardShutdown Lib "MCSCM.DLL" ( _
  ByVal hMCardContext As Long _
) As Long
'*********************************************************************
Private Declare Function MCardConnect Lib "MCSCM.DLL" ( _
  ByVal hMCardContext As Long, _
  ByVal dwConnectMode As Long, _
  ByVal byCardType As Byte, _
  ByRef phMCard As Long _
) As Long
'*********************************************************************
Private Declare Function MCardDisconnect Lib "MCSCM.DLL" ( _
  ByVal hMCardContext As Long, _
  ByVal dwDisposition As Long _
) As Long
'*********************************************************************
Private Declare Function MCardGetAttrib Lib "MCSCM.DLL" ( _
  ByVal hMCardContext As Long, _
  ByVal dwAttrId As Long, _
  ByVal pbAttr As String, _
  ByRef pcbAttrLen As Long _
) As Long
'*********************************************************************
Private Declare Function MCardReadMemory Lib "MCSCM.DLL" ( _
  ByVal hMCard As Long, _
  ByVal bMemZone As Byte, _
  ByVal dwOffset As Long, _
  ByVal pbReadBuffer As String, _
  ByRef pbReadLen As Long _
) As Long
'*********************************************************************
Private Declare Function MCardWriteMemory Lib "MCSCM.DLL" ( _
  ByVal hMCard As Long, _
  ByVal bMemZone As Byte, _
  ByVal dwOffset As Long, _
  ByVal pbWriteBuffer As String, _
  ByRef pcbWriteLen As Long _
) As Long
'*********************************************************************
Private Declare Function MCardVerifyPIN Lib "MCSCM.DLL" ( _
  ByVal hMCard As Long, _
  ByVal bPinNumber As Byte, _
  ByVal dataPIN As String, _
  ByVal cbLen As Byte _
) As Long
'*********************************************************************
Private Declare Function MCardChangePIN Lib "MCSCM.DLL" ( _
  ByVal hMCard As Long, _
  ByVal bPinNumber As Byte, _
  ByVal dataOldPIN As String, _
  ByVal cbLenOldPin As Byte, _
  ByVal dataNewPIN As String, _
  ByVal cbNewOldPin As Byte _
) As Long

Private Sub DisplayErr( _
  ByVal S As String, _
  ByVal L As Long _
)
  Label1.Caption = Label1.Caption & S & " returned " & MCardErrToStr(L) & Chr(10) & Chr(13)
End Sub

Private Sub AddLog( _
  ByVal S As String _
)
  Label1.Caption = Label1.Caption & S & Chr(10) & Chr(13)
End Sub

Function MCardGetAttribLong( _
  ByVal hMCardContext As Long, _
  ByVal dwAttrId As Long, _
  ByRef pbAttr As Long _
) As Long
 
  Dim S As String
  Dim lSize As Long
  Dim bData As Byte
  Dim lReturn As Long
   
  S = String(100, 0)
  lSize = 4
  lReturn = MCardGetAttrib(hMCardContext, dwAttrId, S, lSize)
  If (lReturn <> 0) Then
    DisplayErr "MCardGetAttrib", lReturn
    MCardGetAttribLong = lReturn
    Exit Function
  End If
 
  pbAttr = Asc(S)
 
  S = Right(S, Len(S) - 1)
  pbAttr = pbAttr + (Asc(S) * &HFF)
 
  S = Right(S, Len(S) - 1)
  pbAttr = pbAttr + (Asc(S) * &HFFFF)
 
  S = Right(S, Len(S) - 1)
  pbAttr = pbAttr + (Asc(S) * &HFFFFFF)
     
End Function

Private Function MCardErrToStr( _
  ByVal Err As Long _
) As String

  Select Case Err
    Case MCARD_S_SUCCESS
      MCardErrToStr = "MCARD_S_SUCCESS"
    Case MCARD_E_INTERNAL_ERROR
      MCardErrToStr = "MCARD_E_INTERNAL_ERROR"
    Case MCARD_E_NOT_IMPLEMENTED
      MCardErrToStr = "MCARD_E_NOT_IMPLEMENTED"
    Case MCARD_E_NOT_INITIALIZED
      MCardErrToStr = "MCARD_E_NOT_INITIALIZED"
    Case MCARD_E_INCOMPATIBLE_READER
      MCardErrToStr = "MCARD_E_INCOMPATIBLE_READER"
    Case MCARD_E_UNKNOWN_CARD
      MCardErrToStr = "MCARD_E_UNKNOWN_CARD"
    Case MCARD_E_BUFFER_TOO_SMALL
      MCardErrToStr = "MCARD_E_BUFFER_TOO_SMALL"
    Case MCARD_E_INVALID_PARAMETER
      MCardErrToStr = "MCARD_E_INVALID_PARAMETER"
    Case MCARD_E_PROTO_MISMATCH
      MCardErrToStr = "MCARD_E_PROTO_MISMATCH"
    Case MCARD_E_PROTOCOL_ERROR
      MCardErrToStr = "MCARD_E_PROTOCOL_ERROR"
    Case MCARD_E_CHAL_RESP_FAILED
      MCardErrToStr = "MCARD_E_CHAL_RESP_FAILED"
    Case MCARD_E_INVALID_MEMORY_RANGE
      MCardErrToStr = "MCARD_E_INVALID_MEMORY_RANGE"
    Case MCARD_E_INVALID_MEMORY_ZONE_ID
      MCardErrToStr = "MCARD_E_INVALID_MEMORY_ZONE_ID"
    Case MCARD_E_INVALID_PIN_ID
      MCardErrToStr = "MCARD_E_INVALID_PIN_ID"
    Case MCARD_E_INVALID_CHAL_RESP_ID
      MCardErrToStr = "MCARD_E_INVALID_CHAL_RESP_ID"
    Case MCARD_W_NOT_ALL_DATA_READ
      MCardErrToStr = "MCARD_W_NOT_ALL_DATA_READ"
    Case MCARD_W_NOT_ALL_DATA_WRITTEN
      MCardErrToStr = "MCARD_W_NOT_ALL_DATA_WRITTEN"
    Case MCARD_W_PIN_VERIFY_NEEDED
      MCardErrToStr = "MCARD_W_PIN_VERIFY_NEEDED"
    Case MCARD_W_PIN_VERIFY_FAILED
      MCardErrToStr = "MCARD_W_PIN_VERIFY_FAILED"
    Case MCARD_W_NO_PIN_ATTEMPTS_LEFT
      MCardErrToStr = "MCARD_W_NO_PIN_ATTEMPTS_LEFT"
    Case MCARD_W_NO_UNITS_TO_DECREMENT
      MCardErrToStr = "MCARD_W_NO_UNITS_TO_DECREMENT"
    Case Else
      MCardErrToStr = "unknown error"
  End Select
   
  MCardErrToStr = MCardErrToStr + " (0x" + Hex(Err) + ")"
   
End Function

Private Sub Command1_Click()
  TestFunc (Text1.Text)
End Sub

Private Sub TestFunc( _
  ReaderName As String _
)
  Dim hSCardCtx As Long
  Dim lReturn As Long
 
  Label1.Caption = "Test..." & Chr(10) & Chr(13)
   
  lReturn = SCardEstablishContext(0, 0, 0, hSCardCtx)
  If (lReturn <> 0) Then
    DisplayErr "SCardEstablishContext", lReturn
    Exit Sub
  End If

  Label2.Caption = "hSCardCtx: 0x" + Hex(hSCardCtx)

  DoMCardTest hSCardCtx

  lReturn = SCardReleaseContext(hSCardCtx)
  If (lReturn <> 0) Then
    DisplayErr "SCardReleaseContext", lReturn
    Exit Sub
  End If

  Label1.Caption = Label1.Caption & Chr(10) & Chr(13) & "test ok"
End Sub

Private Sub DoMCardTest( _
  hSCardCtx As Long _
)
  Dim hMCardCtx As Long
  Dim dwDLLVersion As Long
  Dim lReturn As Long
  Dim ReaderName As String
     
  ReaderName = Text1.Text
 
  lReturn = MCardInitialize(hSCardCtx, ReaderName, hMCardCtx, dwDLLVersion)
  If (lReturn <> 0) Then
    DisplayErr "MCardInitialize", lReturn
    Exit Sub
  End If
   
  Label3.Caption = "hMCardCtx: 0x" + Hex(hMCardCtx)

  DoConnectToMemCard hSCardCtx, hMCardCtx

  lReturn = MCardShutdown(hMCardCtx)
  If (lReturn <> 0) Then
    DisplayErr "MCardShutdown", lReturn
    Exit Sub
  End If
End Sub

Private Sub DoConnectToMemCard( _
  hSCardCtx As Long, _
  hMCardCtx As Long _
)
  Dim hMCard As Long
    Dim lAttr As Long

  lReturn = MCardConnect(hMCardCtx, 1, 0, hMCard)
  If (lReturn <> 0) Then
    DisplayErr "MCardConnect", lReturn
    Exit Sub
  End If

  Label4.Caption = "hMCard: 0x" + Hex(hMCard)

  DoCardTest hSCardCtx, hMCardCtx, hMCard
 
  lReturn = MCardDisconnect(hMCard, 0)
  If (lReturn <> 0) Then
    DisplayErr "MCardDisconnect", lReturn
    Exit Sub
  End If
End Sub

Private Sub DoCardTest( _
  hSCardCtx As Long, _
  hMCardCtx As Long, _
  hMCard As Long _
)
  Dim lReturn As Long
  Dim lAttr As Long
 
  lReturn = MCardGetAttribLong(hMCard, MCARD_ATTR_TYPE, lAttr)
  If (lReturn <> 0) Then
    DisplayErr "MCardGetAttrib", lReturn
    Exit Sub
  End If

  Label5.Caption = "Type: 0x" + Hex(lAttr)

  Dim strPin As String
  Dim strPinNew As String

  If (lAttr = MCARDTYPE_SLE4428) Then
    AddLog "MCARDTYPE_SLE4428"
    strPin = Chr(&H0) & Chr(&H0)
    strPinNew = Chr(&H12) & Chr(&H34)
    DoPinTest hMCard, strPin, strPinNew, 2
  End If

  If (lAttr = MCARDTYPE_SLE4442) Then
    AddLog "MCARDTYPE_SLE4442"
    strPin = Chr(&H0) & Chr(&H0) & Chr(&H0)
    strPinNew = Chr(&H12) & Chr(&H34) & Chr(&H56)
    DoPinTest hMCard, strPin, strPinNew, 3
  End If
End Sub

Private Sub DoPinTest( _
  hMCard As Long, _
  strPin As String, _
  strPinNew As String, _
  lLen As Long _
)
  Dim lReturn As Long
 
  lReturn = MCardVerifyPIN(hMCard, 0, strPin, lLen)
  If (lReturn <> 0) Then
    DisplayErr "MCardVerifyPIN", lReturn
    Exit Sub
  End If
 
  lReturn = MCardChangePIN(hMCard, 0, strPin, lLen, strPinNew, lLen)
  If (lReturn <> 0) Then
    DisplayErr "MCardChangePIN (1)", lReturn
    Exit Sub
  End If

  lReturn = MCardChangePIN(hMCard, 0, strPinNew, lLen, strPin, lLen)
  If (lReturn <> 0) Then
    DisplayErr "MCardChangePIN (2)", lReturn
    Exit Sub
  End If
 
  AddLog "PIN Test end"
 
End Sub
Angehängte Dateien
Dateityp: rar DLLs.rar (244,6 KB, 19x aufgerufen)
Matthias
  Mit Zitat antworten Zitat