unit declaredll;
interface
//读卡函数声明
function piccreadex(ctrlword:byte;pserial:pbyte;area:byte;keyA1B0:byte;picckey:pbyte;piccdata0_2:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';
//写卡函数声明
function piccwriteex(ctrlword:byte;pserial:pbyte;area:byte;keyA1B0:byte;picckey:pbyte;piccdata0_2:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';
//驱动蜂鸣器函数声明
function pcdbeep(xms:integer):byte;stdcall;external 'OUR_MIFARE.dll';
//修改单区密码函数声明
function piccchangesinglekey(ctrlword:byte;pserial:pbyte;area:byte;keyA1B0:byte;piccoldkey:pbyte;piccnewkey:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';
//修改单区密码改单区AB密码或访问控制位函数声明
function piccchangesinglekeyex(ctrlword:byte;serial:pbyte;area:byte;keyA1B0:byte;piccoldkey:pbyte;piccdata:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';
//读出设备编号函数声明
function pcdgetdevicenumber(pdevicenumber:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';
//只读取卡号函数
function piccrequest(pserial:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';
//发送显示
function lcddispfull(lcdstr:PChar):byte;stdcall;external 'OUR_MIFARE.dll';
//改写0区0块UID
function piccwriteserial(ctrlword:byte;pserial:pbyte;keyA1B0:byte;picckey:pbyte;piccdata:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';
const
//以下控制字的含义请查看本公司网站提供的动态库说明
BLOCK0_EN = $01; //读块0
BLOCK1_EN = $02; //读块1
BLOCK2_EN = $04; //读块2
NEEDSERIAL = $08; //是否需要只对指定系列号的卡操作
EXTERNKEY = $10; //是否使用外部密码
NEEDHALT = $20; //是否休眠本卡
implementation
procedure TForm1.Button11Click(Sender: TObject);
//用UID卡复制M1卡,写入物理卡号到UID卡的0扇区0块
var
i:integer;
x:LongWord ;
status:byte;//存放返回值
edc:byte;//校验码
authmode:byte;//密码类型,用A密码或B密码
myctrlword:byte;//控制字
mypicckey:array[0..5] of byte;//密码
mypiccserial:array[0..3] of byte;//卡序列号
mypiccdata:array[0..15] of byte;//卡数据缓冲
strs:string;
begin
for i:=0 to 16 do
begin
if i<4 then mypiccserial[i]:=0;
if i<6 then mypicckey[i]:=0;
mypiccdata[i]:=0;
end;
if RichEdit2.Lines[0]>'4294967295' then
begin
Application.MessageBox('卡号最大取值:4294967295', '提示', MB_OK+MB_ICONSTOP) ;
exit;
end;
x:= StrToInt64(RichEdit2.Lines[0]) ;
if x>4294967295 then
begin
Showmessage('卡号最大取值:4294967295') ;
exit;
end;
edc:=0;
if (checkbox4.State = cbchecked) then
begin
for i:=3 downto 0 do
begin
mypiccdata[i]:= x and 255;
edc:=edc xor mypiccdata[i];
x:=x shr 8;
end;
end
else
begin
for i:=0 to 3 do
begin
mypiccdata[i]:= x and 255;
edc:=edc xor mypiccdata[i];
x:=x shr 8;
end;
end;
mypiccdata[4]:= edc;
myctrlword:=BLOCK0_EN;
authmode:=0;
status:=piccwriteserial(myctrlword,@mypiccserial,authmode,@mypicckey,@mypiccdata);
case status of
0:
begin
pcdbeep(38);
strs:= 'UID卡号:'+RightStr('0000000000'+RichEdit2.Lines[0],10)+' 写卡成功!' ;
ListBox1.Items.Add(strs);
listbox1.ItemIndex:=listbox1.Count-1;
Application.MessageBox(PAnsiChar(AnsiString(strs)) , '提示', MB_OK+MB_ICONINFORMATION); MB_ICONQUESTION MB_ICONEXCLAMATION MB_ICONWARNING MB_ICONINFORMATION MB_ICONASTERISK MB_ICONHAND MB_ICONERROR MB_ICONSTOP
if checkbox1.State = cbchecked then
begin
x:=StrToInt64(RichEdit2.Lines[0])+1;
if (checkbox2.State = cbchecked) and (RightStr(inttostr(x),1)='4') then x:=x+1;
if (checkbox3.State = cbchecked) and (RightStr(inttostr(x),1)='7') then x:=x+1;
RichEdit2.Text :=rightstr('0000000000'+inttostr(x),10);
end;
end;
8: Application.MessageBox('请将卡放在感应区', '提示', MB_OK+MB_ICONSTOP);
12:Application.MessageBox('卡密码认证失败!', '提示', MB_OK+MB_ICONSTOP);
else
begin
Application.MessageBox(PAnsiChar(AnsiString('操作失败,错误代码:'+IntToStr(status))), '提示', MB_OK+MB_ICONSTOP);
end;
end;
end;
Delphi 写UID卡0区0块
于 2022-04-08 15:28:04 首次发布