详细代码
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//控制字定义
#define BLOCK0_EN 0x01
#define BLOCK1_EN 0x02
#define BLOCK2_EN 0x04
#define NEEDSERIAL 0x08
#define EXTERNKEY 0x10
#define NEEDHALT 0x20
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
//轻松读卡
{
/*
技术支持:
网站:
*/
unsigned char status;//存放返回值
unsigned char myareano;//区号
unsigned char authmode;//密码类型,用A密码或B密码
unsigned char myctrlword;//控制字
unsigned char mypicckey[6];//密码
unsigned char mypiccserial[4];//卡序列号
unsigned char mypiccdata[48]; //卡数据缓冲
unsigned char (__stdcall *piccreadex)(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *picckey,unsigned char *piccdata0_2);
//判断动态库是否存在
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "OUR_MIFARE.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在应用程序的文件夹找到OUR_MIFARE.dll");
return;
}
//提取函数
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
piccreadex = (unsigned char (__stdcall *)(unsigned char,unsigned char *,unsigned char,unsigned char,unsigned char *,unsigned char *))GetProcAddress(hDll,"piccreadex");
//控制字指定,控制字的含义请查看本公司网站提供的动态库说明
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;
//指定区号
myareano = 8;//指定为第8区
//批定密码模式
authmode = 1;//大于0表示用A密码认证,推荐用A密码认证
//指定密码
mypicckey[0] = 0xff;
mypicckey[1] = 0xff;
mypicckey[2] = 0xff;
mypicckey[3] = 0xff;
mypicckey[4] = 0xff;
mypicckey[5] = 0xff;
status = piccreadex(myctrlword,mypiccserial,myareano,authmode,mypicckey,mypiccdata);
//在下面设定断点,然后查看mypiccserial、mypiccdata,
//调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata,
//开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。
//处理返回函数
switch(status)
{
case 0:
ShowMessage("操作成功");
break;
//......
case 8:
ShowMessage("请将卡放在感应区");
break;
}
//返回解释
/*
#define ERR_REQUEST 8//寻卡错误
#define ERR_READSERIAL 9//读序列吗错误
#define ERR_SELECTCARD 10//选卡错误
#define ERR_LOADKEY 11//装载密码错误
#define ERR_AUTHKEY 12//密码认证错误
#define ERR_READ 13//读卡错误
#define ERR_WRITE 14//写卡错误
#define ERR_NONEDLL 21//没有动态库
#define ERR_DRIVERORDLL 22//动态库或驱动程序异常
#define ERR_DRIVERNULL 23//驱动程序错误或尚未安装
#define ERR_TIMEOUT 24//操作超时,一般是动态库没有反映
#define ERR_TXSIZE 25//发送字数不够
#define ERR_TXCRC 26//发送的CRC错
#define ERR_RXSIZE 27//接收的字数不够
#define ERR_RXCRC 28//接收的CRC错
*/
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
//让设备发出声音
{
/*
技术支持:
网站:
*/
unsigned char (__stdcall *pcdbeep)(unsigned long xms);
//判断动态库是否存在
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "OUR_MIFARE.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在应用程序的文件夹找到OUR_MIFARE.dll");
return;
}
//提取函数
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
pcdbeep = (unsigned char (__stdcall *)(unsigned long))GetProcAddress(hDll,"pcdbeep");
pcdbeep(50);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
//轻松写卡
{
/*
技术支持:
网站:
*/
int i;
unsigned char status;//存放返回值
unsigned char myareano;//区号
unsigned char authmode;//密码类型,用A密码或B密码
unsigned char myctrlword;//控制字
unsigned char mypicckey[6];//密码
unsigned char mypiccserial[4];//卡序列号
unsigned char mypiccdata[48]; //卡数据缓冲
unsigned char (__stdcall *piccwriteex)(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *picckey,unsigned char *piccdata0_2);
//判断动态库是否存在
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "OUR_MIFARE.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在应用程序的文件夹找到OUR_MIFARE.dll");
return;
}
//提取函数
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
piccwriteex = (unsigned char (__stdcall *)(unsigned char,unsigned char *,unsigned char,unsigned char,unsigned char *,unsigned char *))GetProcAddress(hDll,"piccwriteex");
//控制字指定,控制字的含义请查看本公司网站提供的动态库说明
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;
//指定区号
myareano = 8;//指定为第8区
//批定密码模式
authmode = 1;//大于0表示用A密码认证,推荐用A密码认证
//指定密码
mypicckey[0] = 0xff;
mypicckey[1] = 0xff;
mypicckey[2] = 0xff;
mypicckey[3] = 0xff;
mypicckey[4] = 0xff;
mypicckey[5] = 0xff;
//指定卡数据
for(i = 0;i<48;i++)
{
mypiccdata[i] = i;
}
//写卡
status = piccwriteex(myctrlword,mypiccserial,myareano,authmode,mypicckey,mypiccdata);
//在下面设定断点,然后查看mypiccserial、mypiccdata,
//调用完 piccreadex函数可读出卡序列号到 mypiccserial,读出卡数据到mypiccdata,
//开发人员根据自己的需要处理mypiccserial、mypiccdata 中的数据了。
//处理返回函数
switch(status)
{
case 0:
ShowMessage("操作成功");
break;
//......
case 8:
ShowMessage("请将卡放在感应区");
break;
}
//返回解释
/*
#define ERR_REQUEST 8//寻卡错误
#define ERR_READSERIAL 9//读序列吗错误
#define ERR_SELECTCARD 10//选卡错误
#define ERR_LOADKEY 11//装载密码错误
#define ERR_AUTHKEY 12//密码认证错误
#define ERR_READ 13//读卡错误
#define ERR_WRITE 14//写卡错误
#define ERR_NONEDLL 21//没有动态库
#define ERR_DRIVERORDLL 22//动态库或驱动程序异常
#define ERR_DRIVERNULL 23//驱动程序错误或尚未安装
#define ERR_TIMEOUT 24//操作超时,一般是动态库没有反映
#define ERR_TXSIZE 25//发送字数不够
#define ERR_TXCRC 26//发送的CRC错
#define ERR_RXSIZE 27//接收的字数不够
#define ERR_RXCRC 28//接收的CRC错
*/
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
//修改单区密码
{
/*
技术支持:
网站:
*/
unsigned char status;//存放返回值
unsigned char myareano;//区号
unsigned char authmode;//密码类型,用A密码或B密码
unsigned char myctrlword;//控制字
unsigned char mypiccnewkey[6];//新密码
unsigned char mypiccserial[4];//卡序列号
unsigned char mypiccoldkey[6];//旧密码
unsigned char (__stdcall *piccchangesinglekey)(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *piccoldkey,unsigned char *piccnewkey);
//判断动态库是否存在
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "OUR_MIFARE.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在应用程序的文件夹找到OUR_MIFARE.dll");
return;
}
//提取函数
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
piccchangesinglekey = (unsigned char (__stdcall *)(unsigned char ,unsigned char *,unsigned char ,unsigned char ,unsigned char *,unsigned char *))GetProcAddress(hDll,"piccchangesinglekey");
//控制字指定,控制字的含义请查看本公司网站提供的动态库说明
myctrlword = 0;
//指定区号
myareano = 8;//指定为第8区
//批定密码模式
authmode = 1;//大于0表示用A密码认证,推荐用A密码认证
//指定旧密码
mypiccoldkey[0] = 0xff;
mypiccoldkey[1] = 0xff;
mypiccoldkey[2] = 0xff;
mypiccoldkey[3] = 0xff;
mypiccoldkey[4] = 0xff;
mypiccoldkey[5] = 0xff;
//指定新密码,注意:指定新密码时一定要记住,否则有可能找不回密码,导致该卡报废。
mypiccnewkey[0] = 0xff;
mypiccnewkey[1] = 0xff;
mypiccnewkey[2] = 0xff;
mypiccnewkey[3] = 0xff;
mypiccnewkey[4] = 0xff;
mypiccnewkey[5] = 0xff;
status = piccchangesinglekey(myctrlword,mypiccserial,myareano,authmode,mypiccoldkey,mypiccnewkey);
//返回值处理
switch(status)
{
case 0:
ShowMessage("操作成功");
break;
//......
case 8:
ShowMessage("请将卡放在感应区");
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
//读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限
{
/*
技术支持:
网站:
*/
unsigned char devno[4];//设备编号
unsigned char (__stdcall *pcdgetdevicenumber)(unsigned char *devicenumber);
//判断动态库是否存在
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "OUR_MIFARE.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在应用程序的文件夹找到OUR_MIFARE.dll");
return;
}
//提取函数
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
pcdgetdevicenumber = (unsigned char (__stdcall *)(unsigned char *))GetProcAddress(hDll,"pcdgetdevicenumber");
if(!pcdgetdevicenumber(devno))
{
ShowMessage(IntToStr(devno[0]) + "-" + IntToStr(devno[1]) + "-" + IntToStr(devno[2]) + "-" + IntToStr(devno[3]));
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{ //发送字符至读卡器显示
unsigned char (__stdcall *lcddispfull)(LPSTR lcdstr);
//判断动态库是否存在
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "OUR_MIFARE.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在应用程序的文件夹找到OUR_MIFARE.dll");
return;
}
//提取函数
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
lcddispfull = (unsigned char (__stdcall *)(LPSTR ))GetProcAddress(hDll,"lcddispfull");
lcddispfull(Edit1->Text.c_str());
}
//---------------------------------------------------------------------------