Einzelnen Beitrag anzeigen

Benutzerbild von edosoft
edosoft

Registriert seit: 27. Okt 2003
Ort: Wehingen
258 Beiträge
 
Turbo Delphi für Win32
 
#1

VB-Funktionen in Delphi übersetzen

  Alt 4. Feb 2006, 12:03
Hallo,

Ich habe hier ne DLL zum chipkarten auslesen und da war ne VB demo dabei. Leider krieg ichs nicht ganz hin die funktionen zu übersetzen. könnt ihr mir dabei helfen?

Code:
' ********************************************
' Data Types
' ********************************************

'
' Predefined Cardtypes
'
Enum SCARD_I2C_TYPE
   NO_PREDEFINED_CARD_PARAMETERS = 0
   ' I2C cards from ST-Microelectronics:
   ST14C02C
   ST14C04C
   ST14E32
   M14C04
   M14C16
   M14C32
   M14C64
   M14128
   M14256
   ' I2C cards from GEMplus:
   GFM2K
   GFM4K
   GFM32K
   ' I2C cards from Atmel:
   AT24C01A
   AT24C02
   AT24C04
   AT24C08
   AT24C16
   AT24C164
   AT24C32
   AT24C64
   AT24C128
   AT24C256
   AT24CS128
   AT24CS256
   AT24C512
   AT24C1024
   I2C_TYPE_ENUM_TAIL         ' must always be the last entry in this enum!!
End Enum

'
' Card Parameters Structure
'
Public Type SCARD_I2C_CARD_PARAMETERS
    ucPageSize As Byte             ' Maximal number of bytes that can be written in a successive manner, in a single device select phase.
    ucNumberOfAddressBytes As Byte ' Number of bytes used to address the memory in the I2C card.
    ulMemorySize As Integer        ' The size of the e2prom allocated in the card
End Type

' ********************************************
' Functions
' ********************************************

'
' Initialize an I2C Card
' Parameters:
'      hCard              = Handle to current Card
'      pCardParameters    = Pointer to a SCARD_I2C_CARD_PARAMETERS type
'      lType              = Predefined Cardtype (SCARD_I2C_TYPE)
'
Public Declare Function SCardI2CInit Lib "scardsyn" _
    (ByVal hCard As Long, _
     ByRef pCardParameters As Long, _
     ByVal lType As Long) As Long

'
' Read Bytes from Card
' Parameters:
'      hCard              = Handle to current Card
'      pbReadBuffer       = Array of Bytes where data should be stored in
'      ulReadBufferSize   = Size of ReadBuffer
'      ulAddress          = Offset where read starts
'      ulBytesToRead      = Number of Bytes to read
'
Public Declare Function SCardI2CReadData Lib "scardsyn" _
    (ByVal hCard As Long, _
     ByRef pbReadBuffer As Byte, _
     ByVal ulReadBufferSize As Long, _
     ByVal ulAddress As Long, _
     ByVal ulBytesToRead As Long) As Long

'
' Write Bytes to Card
' Parameters:
'      hCard              = Handle to current Card
'      pbWriteBuffer      = Array of Bytes storing data
'      ulWriteBufferSize  = Size of WriteBuffer
'      ulAddress          = Offset where write starts
'      ulBytesToWrite     = Number of Bytes to write
'
Public Declare Function SCardI2CWriteData Lib "scardsyn" _
    (ByVal hCard As Long, _
     ByRef pbWriteBuffer As Byte, _
     ByVal ulWriteBufferSize As Long, _
     ByVal ulAddress As Long, _
     ByVal ulBytesToWrite As Long) As Long
die funktionen hab ich dabei so aufgefasse:
Delphi-Quellcode:
function SCardI2CInit(hCard: Cardinal; pCardParameters: Pointer; lType: SCARD_I2C_TYPE): Cardinal; stdcall; external 'scardsyn';
function SCardI2CReadData(hCard: Cardinal; out pbReadBuffer: Byte; ulReadBufferSize, ulAddress, ulBytesToRead: Cardinal): Cardinal; stdcall; external 'scardsyn';
function SCardI2CWriteData(hCard: Cardinal; var pbWriteBuffer: Byte; ulWriteBufferSize, ulAddress, ulBytesToWrite: Cardinal): Cardinal; stdcall; external 'scardsyn';
also die SCardI2CReadData und SCardI2CWriteData funktionieren einwandfrei. die OUT und VAR prefixe hab ich mehr oder weniger hergeleitet - ich hoffe das stimmt so.
Probleme hab ich jetzt nur mit der SCardI2CInit funktion. Die gibt mir jedesmal einen fehler zurück (also ungleich 0) obwohl das eigentlich funktionieren müsste. ´die funktion verwendet auch noch 2 selbsdefinierte klassen, die hab ich mal so gemacht:
Delphi-Quellcode:
type SCARD_I2C_CARD_PARAMETERS=record
    ucPageSize: Cardinal;// ' Maximal number of bytes that can be written in a successive manner, in a single device select phase.
    ucNumberOfAddressBytes: Byte;// ' Number of bytes used to address the memory in the I2C card.
    ulMemorySize: Integer //' The size of the e2prom allocated in the card
end;

type SCARD_I2C_TYPE=(NO_PREDEFINED_CARD_PARAMETERS = 0,
   ST14C02C,
   ST14C04C ,
   ST14E32 ,
   M14C04 ,
   M14C16 ,
   M14C32 ,
   M14C64 ,
   M14128 ,
   M14256 ,
   //' I2C car,ds from GEMplus:
   GFM2K ,
   GFM4K ,
   GFM32K,
   //' I2C cards from Atmel:
   AT24C01A ,
   AT24C02 ,
   AT24C04 ,
   AT24C08 ,
   AT24C16 ,
   AT24C164 ,
   AT24C32 ,
   AT24C64 ,
   AT24C128 ,
   AT24C256 ,
   AT24CS128 ,
   AT24CS256 ,
   AT24C512 ,
   AT24C1024 ,
   I2C_TYPE_ENUM_TAIL );
(die kommas sehen ein bisschen merkwürdig aus - so konnt ich die aber am schnellsten tippen. und zum testen ja ok..

der code um die funktionsausführung bei vb sieht so aus:
Code:
Dim rc As Long
Dim CardParameters As SCARD_I2C_CARD_PARAMETERS
Dim pCardParameters As Long
Dim lType As Long

' Type (see SCardI2C.bas "Predefined Cardtypes")
lType = NO_PREDEFINED_PARAMETERS
If I2CTYPE.Text = "ST14C02C" Then lType = ST14C02C
If I2CTYPE.Text = "ST14C04C" Then lType = ST14C04C
If I2CTYPE.Text = "ST14E32" Then lType = ST14E32
If I2CTYPE.Text = "M14C04" Then lType = M14C04
If I2CTYPE.Text = "M14C16" Then lType = M14C16
If I2CTYPE.Text = "M14C32" Then lType = M14C32
If I2CTYPE.Text = "M14C64" Then lType = M14C64
If I2CTYPE.Text = "M14128" Then lType = M14128
If I2CTYPE.Text = "M14256" Then lType = M14256
If I2CTYPE.Text = "GFM2K" Then lType = GFM2K
If I2CTYPE.Text = "GFM4K" Then lType = GFM4K
If I2CTYPE.Text = "GFM32K" Then lType = GFM32K
If I2CTYPE.Text = "AT24C01A" Then lType = AT24C01A
If I2CTYPE.Text = "AT24C02" Then lType = AT24C02
If I2CTYPE.Text = "AT24C04" Then lType = AT24C04
If I2CTYPE.Text = "AT24C08" Then lType = AT24C08
If I2CTYPE.Text = "AT24C16" Then lType = AT24C16
If I2CTYPE.Text = "AT24C164" Then lType = AT24C164
If I2CTYPE.Text = "AT24C32" Then lType = AT24C32
If I2CTYPE.Text = "AT24C64" Then lType = AT24C64
If I2CTYPE.Text = "AT24C128" Then lType = AT24C128
If I2CTYPE.Text = "AT24C256" Then lType = AT24C256
If I2CTYPE.Text = "AT24CS128" Then lType = AT24CS128
If I2CTYPE.Text = "AT24CS256" Then lType = AT24CS256
If I2CTYPE.Text = "AT24C512" Then lType = AT24C512
If I2CTYPE.Text = "AT24C1024" Then lType = AT24C1024

' get pointer do CardParameters
pCardParameters = VarPtr(CardParameters)

' Init
rc = SCardI2CInit(hCard, pCardParameters, lType)
If rc <> OKERR_OK Then
    MESSAGETEXT.Text = HandleError(rc)
    Exit Sub
End If

MESSAGETEXT.Text = "InitI2C successfull"
mein code sieht so aus:
Delphi-Quellcode:
var
  rc: Cardinal;
  CardParameters: SCARD_I2C_CARD_PARAMETERS;
  pCardParameters: Pointer;
  lType: SCARD_I2C_TYPE;

begin

{
' Type (see SCardI2C.bas "Predefined Cardtypes")
lType = NO_PREDEFINED_PARAMETERS
If I2CTYPE.Text = "ST14C02C" Then lType = ST14C02C
If I2CTYPE.Text = "ST14C04C" Then lType = ST14C04C
If I2CTYPE.Text = "ST14E32" Then lType = ST14E32
If I2CTYPE.Text = "M14C04" Then lType = M14C04
If I2CTYPE.Text = "M14C16" Then lType = M14C16
If I2CTYPE.Text = "M14C32" Then lType = M14C32
If I2CTYPE.Text = "M14C64" Then lType = M14C64
If I2CTYPE.Text = "M14128" Then lType = M14128
If I2CTYPE.Text = "M14256" Then lType = M14256
If I2CTYPE.Text = "GFM2K" Then lType = GFM2K
If I2CTYPE.Text = "GFM4K" Then lType = GFM4K
If I2CTYPE.Text = "GFM32K" Then lType = GFM32K
If I2CTYPE.Text = "AT24C01A" Then lType = AT24C01A
If I2CTYPE.Text = "AT24C02" Then lType = AT24C02
If I2CTYPE.Text = "AT24C04" Then lType = AT24C04
If I2CTYPE.Text = "AT24C08" Then lType = AT24C08
If I2CTYPE.Text = "AT24C16" Then lType = AT24C16
If I2CTYPE.Text = "AT24C164" Then lType = AT24C164
If I2CTYPE.Text = "AT24C32" Then lType = AT24C32
If I2CTYPE.Text = "AT24C64" Then lType = AT24C64
If I2CTYPE.Text = "AT24C128" Then lType = AT24C128
If I2CTYPE.Text = "AT24C256" Then lType = AT24C256
If I2CTYPE.Text = "AT24CS128" Then lType = AT24CS128
If I2CTYPE.Text = "AT24CS256" Then lType = AT24CS256
If I2CTYPE.Text = "AT24C512" Then lType = AT24C512
If I2CTYPE.Text = "AT24C1024" Then lType = AT24C1024
}

 lType := ST14C02C;
//' get pointer do CardParameters
pCardParameters := @CardParameters;


//' Init
rc := SCardI2CInit(hCard, pCardParameters, lType);
If rc <> 0 Then
  begin
    edit1.Text := ('error');
    exit;
    end;

edit1.Text := 'InitI2C successfull';

end;
wobei ich mir jetzt nicht ganz sicher bin ist des mit dem pointer (' get pointer do CardParameters) und ob der typ "Long" richtig umgesetzt ist mir Cardinal und auch ob das mit den definierten classen (record und enum) so ganz richtig ist.

Danke schonmal im Vorraus!

mfg.
Dominik Weber
www.edo-soft.com
  Mit Zitat antworten Zitat