Click here to Skip to main content
15,742,263 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi dear
I want to integrate crt450 card reader with my 2010 application
the read card function gave error
rc = CRT450_ReadCard(a, 0x31, 0x0F, true, 0x30, _FingerFeatures1, 0x0000000d);

        public static extern unsafe int CRT450_ReadCard(int a, byte p, byte p_2, bool p_3, byte p_4, byte p_6, int p_5);

this is help file for this function which manufacturer company provide

int APIENTRY CRT450_ReadCard (HANDLE ComHandle, BYTE _ReadOption, BYTE _WaitTime, bool *_IsOutofTime, BYTE *_mode,BYTE _CardData[],int *_CardDataLen)
// Parameter:
// HANDLE ComHandle: Com port Handle
// BYTE _ReadOption: Card swipe modal
●0x30: Swipe once
●0x31: Swipe until valid result
// BYTE _WaitTime: Waiting time for Swiping
●0x05: Data range (0x01~0x0F)
//bool *_IsOutofTime: Set of overtime
●true: Overtime
●false: No overtime
// BYTE *_mode: Format of card data reading
●0x30: ASCII
●0x31: Binary
// int *_CardDataLen: Content of text in every designated track
Format of text in every designated track:
Start word + Status word+ Track data
●Start word : 0x80 for track#1
:0x81 for track#2
:0x82 for track#3
●Status word :0x59, positive read, then track data is the reality content of the track text
:0x4E, negative read, then track data is the error info. shown below
:0x4F, No read, then track data is 0x4E
●Error info :
:0xE1, No STX
:0xE2, No ETX
:0xE3, VRC Error
:0xE4, LRC Error
:0xE5, No Data
︰Format of card read by binary
●Track#1 : b0, b1, b2, b3, b4, b5,P
●Track#2/Track#3: b0, b1, b2 b3,P
︰If reading card by ASCII style ,the device will return data by every character is encoded into 1byte ASCII
●E.g. The first byte in Track #1: 0x03(HEX),
Return encoded data: 0x30(ASCII)
︰If reading card by binary style , device will return the data by 4bits of every character is encoded into 1byte ASCII
●E.g. The first byte at Track #1 :0x03(HEX)
Return track data: 0x30 0x33(ASCII)
// int *_CardDataLen: Length of all designated tracks text
//Returned value:
If the function is succeeded, zero will be returned, and if failed, nonzero will be returned.
///////////////////////////////////////////////////////////////////////Pplease help me

Updated 17-May-12 11:33am
Richard MacCutchan 31-Oct-10 13:29pm    
OK, I give up, what was the error?
Khawar Abbas1 31-Oct-10 16:24pm    
what is the error
Henry Minute 31-Oct-10 20:12pm    
The answer already provided by karle may well be correct, I have no idea. But you have tagged your question as C++ but state (in the question) that it is for a C# app. This is confusing, also someone with C# skills might well ignore your question because of your tag. Interfacing to DLLs written in C++ comes under the general heading of 'Interop/PInvoke' so a better tag would have been 'C#, Interop'.

you can try with this one

public static extern Int32 CommOpen();

public static extern Int32 CommClose(Int32 hcom);

public static extern Int32 GetDeviceCapabilities(Int32 hcom, ref Int32 InputReportByteLength, ref Int32 OutputReportByteLength);

public static extern unsafe Int32 CRT450_ReadCard(Int32 hcom, byte Readoption, byte Waittime, ref bool IsOutofTime, ref byte ReadModel, byte[] ReadDataBuf, ref Int32 CardDataLenth);

//Calling function sample

Int32 hcom = 0;
byte Readoption=0x30;
byte Waittime = 0x0F;
byte ReadModel=0x30;
bool IsOutofTime = false;
byte[] ReadDataBuf = new byte[300];
Int32 CardDataLenth = 0;

hcom = CommOpen();
if (hcom > 0)
 rc = GetDeviceCapabilities(hcom, ref InputReportByteLength, ref OutputReportByteLength);
     if (rc == 0)
        rc = CRT450_ReadCard(hcom, Readoption, Waittime, ref IsOutofTime, ref ReadModel, ReadDataBuf, ref CardDataLenth);

Share this answer
Admittedly, I haven't thoroughly read all the previous responses/comments, but here's something I see.

The api says the function prototype is:
int APIENTRY CRT450_ReadCard (HANDLE ComHandle, BYTE _ReadOption, BYTE _WaitTime, bool *_IsOutofTime, BYTE *_mode,BYTE _CardData[],int *_CardDataLen)

Yet your call is
rc = CRT450_ReadCard(a, 0x31, 0x0F, true, 0x30, _FingerFeatures1, 0x0000000d);

Notice that the last parameter in the prototype is a POINTER to an int...but you pass 0x0000000d - definitely NOT a pointer to memory you can write.

Hope this helps.
Share this answer
Your .NET prototype is not correct.
In C code _IsOutofTime, _mode, _CardData[],_CardDataLen is a pointer an will (or can) be modified from inside the c function.
So your memory will be corrupet when calling this function.

You have to change the .NET prototype.
public static extern unsafe int CRT450_ReadCard(int ComHandle, byte ReadOption, byte WaitTime, ref bool IsOutofTime, ref byte mode, IntPtr CardData, ref int length);

The buffer passed to CardData must be allocated with Marshal.AllocHGlobal( ) and be released with Marshal.FreeHGlobal().

See MSDN[^] for further information.
Share this answer
Khawar Abbas1 1-Nov-10 18:33pm    
thnaks alot karle
now the function return 0 which mean its is successfull
but now there is a exeption in

the exeption is


Windows has triggered a breakpoint in WindowsFormsApplication12.exe.

This may be due to a corruption of the heap, which indicates a bug in WindowsFormsApplication12.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while WindowsFormsApplication12.exe has focus.

The output window may have more diagnostic information.

my code is
rc = CRT450_ReadCard(a, 0x31, 0x0F, ref isouttime, ref bytemod, ptr, ref length);

also how i will get back the data to byte array from ptr
venkispringlife 10-May-12 6:42am    

I am also facing the same problem of "Attempted to read or write protected memory. This is often an indication that other memory is corrupt", while accessing the method

public static extern int GetDeviceCapabilities(long hcom, ref int InputReportByteLength, ref int OutputReportByteLength);

Can you please help me in this regard.

Thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900