[DUG] Delphi Digest, Vol 139, Issue 15

David Brennan dugdavid at dbsolutions.co.nz
Fri May 29 09:16:00 NZST 2015


My hat is off. Quite a lot of effort to help a fellow DUGer to that extent. It’s a nice community.

 

From: delphi-bounces at listserver.123.net.nz [mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Jolyon Smith
Sent: Thursday, 28 May 2015 7:05 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Delphi Digest, Vol 139, Issue 15

 

Based on this your declaration of the API function appears to be incorrect:

Documentation:

int API_PCDRead(HANDLE          commHandle,

                int:            DeviceAddress,

                unsigned char:  mode,

                unsigned char:  blk_add,

                unsigned char:  num_blk,

                unsigned char: *snr,

                unsigned char: *buffer);

Your declaration:

function API_PCDRead(comHandle: Thandle;

                     DeviceAddress: integer; 

                     mode:          byte;

                     add_blk,num_blk:integer;
                     snr,buffer:pchar): integer;

 

Your add_blk and num_blk parameters are declared of type Integer (4 bytes) when the function is expecting parameters of type unsigned char (1 byte).  This explains your access violation (the result of the way that memory used for passing parameters is handled when using 'C' calling conventions, if the caller of a function, and the function itself, do not agree on the exact sizes of the parameters being passed).

 

Change your function declaration to:

 

function API_PCDRead(comHandle:       THandle;

                     DeviceAddress:   integer; 

                     mode:            byte;

                     add_blk,num_blk: byte;
                     snr,buffer:      pchar): integer;

 

I would also double check the type declaration for the Handle parameter, just to be certain that the "HANDLE" type expected by the API function does in fact correspond to what appears to be the Windows API specific THandle type that you have declared it as.

This will almost certainly solve your access violation.  Based on the documentation it appears that the way that you are handling the result following the call is correct (though I haven't examined it exhaustively).  You will do yourself an enormous favour by simplifying and tidying this up as well as the initialization part.

Apart from anything else, it will greatly help someone maintaining the code in the future if the variables used in the calling code correspond to the parameters documented for the API.  e.g. if you are passing a buffer in a parameter called "snr" then name your variable "snr", or snrParam, and only access it that way.  Don't use a variable and a pointer that happens to point to that variable (via a side-effect!) or if you absolutely must then at least ensure that the names involved make this usage clear, e.g. snr and ptrsnr.


Finally, the documentation also says that upon calling the function, the snr buffer is expected to hold an 8 byte key value which you are only initialising with 7 bytes (6 x $FF and a $00) so I would double check that this initialization of the snr buffer in your code is correct.


Hope this helps.

 

 

On 28 May 2015 at 18:27, Marshland Engineering <marshland at marshland.co.nz> wrote:

This is running with a USB cable and Windows XP. It installs as a HID device.

4.2    Mifare Appilication Commands

4.2.1 int
API_PCDRead(HANDLEcommHandle,int:DeviceAddress,unsignedchar:mode,unsigned
char:blk_add,unsigned char:num_blk,unsigned char:*snr, unsigned char:*buffer);

Description:
read the appointed length date at the appointed station
Input Parameter Description

commHandle      the serial port handle
DeviceAddress   equipment address
mode            read mode
        ( Request Idle + Key A    mode=00 ,  Request Idle + Key B     mode= 02,
        Request All  + Key A    mode=01 ,  Request All  + Key B     mode=03
        the up number is hex)
blk_add read block address
num_blk read block amount
*snr    a finger, transfer eight byte secret key
*buffer wait receive the variable of output finger

Output Parameter
If Command success
        *snr    4 byte card number
        *buffer the read date (the fact number is num_blk*16)
If Command Failure
        buffer[0]       System Error/Status Codes(You can consult the 2.2)

Return value:
        0x00    Command OK. ( success)
        0x01    Command FAILURE


The full manual is here
http://www.marshland.co.nz/ftp/Misc/DLL.doc
Thanks Wallace.

_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at listserver.123.net.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20150529/b28c9856/attachment-0001.html 


More information about the Delphi mailing list