📄 rwcard.pas
字号:
unit RWCard;
///////////////////////////////////////////////
// 本模块基于明华公司写卡器 //
// 若更换写卡器重写本模块内的函数即可 //
///////////////////////////////////////////////
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ConstDef;
type
TFrmRWCard = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
Function bFunCardDevIni: Boolean;
Function bFunCardDevExit: Boolean;
Function bFunTestCardDev: Boolean;
Function bFunChkCardStatus: Boolean;
Function hFunGetLoadICMemDLL(wsEMemDT: widestring): THandle;
Function bFunFreeICMemDLL(hICMemDLL: THandle): Boolean;
Function bFunReadCardMem: Boolean;
Function bFunChkEMemDataType(var ReadCD: RecRWCustDataType): Boolean;
Function bFunWriteCardMem(sInICSC: string; sOutICSC: string): Boolean;
Function bFunWriteCardCustData(WriteCD: RecRWCustDataType;
WriteCK: RecWriteCardKindType): Boolean;
Function bFunReadCardCustData(var ReadCD: RecRWCustDataType;
var ReadCK: RecWriteCardKindType): Boolean;
private
{ Private declarations }
public
{ Public declarations }
hCardDLL{写卡器函数动态链接库句柄}: THandle;
liCardDev: Longint; //写卡器句柄
EICK: CardKindType; //电卡类型
aCMem: Array[CEMemOrn..CEMemEnd] of byte;
end;
var
FrmRWCard: TFrmRWCard;
Function sFunAddStrToStr(sStrIn:string; sStrAdd:string):WideString;
export;far;external 'NIMStrans.dll';
implementation
uses CardDLLFT{CardDLL FunctionType};
{$R *.DFM}
{OnCreate-OnDestory/ OnShow-OnClose/ .Create-.Free / }
procedure TFrmRWCard.FormCreate(Sender: TObject);
begin //dynamic Load 'MWIC_32.DLL'
liCardDev:=0; //初始化变量
hCardDLL:=LoadLibrary('MWIC_32.DLL');
if hCardDLL=0 then wFunShowError('动态链接库(DLL)加载错误!',[mbOK]);
end;
procedure TFrmRWCard.FormDestroy(Sender: TObject);
begin //dynamic Free 'MWIC_32.DLL'
if hCardDLL>0 then FreeLibrary(hCardDLL);
end;
Function TFrmRWCard.hFunGetLoadICMemDLL(wsEMemDT: widestring): THandle;
var
hLoadICMemDLL: THandle;
begin //dynamic Load IC卡内存数据计算DLL
//根据数据格式类型装入IC卡内存数据计算DLL
hLoadICMemDLL:=0;
if wsEMemDT=TaiYuanMemKind then begin
hLoadICMemDLL:=LoadLibrary('NImsTYMem.DLL');
if hLoadICMemDLL=0 then wFunShowError('动态链接库(DLL)加载错误!',[mbOK]);
end
else wFunShowError('系统暂不支持此种表型!',[mbOK]);
Result:=hLoadICMemDLL;
end;
Function TFrmRWCard.bFunFreeICMemDLL(hICMemDLL: THandle): Boolean;
begin //dynamic Free IC卡内存数据计算DLL
if hICMemDLL>0 then Result:=FreeLibrary(hICMemDLL)
else Result:=False;
end;
Function TFrmRWCard.bFunCardDevIni: Boolean;
Label TOError,TOInied;
var
iCount: integer;
begin //Get CardDevice Handle, Open CardDevice
if hCardDLL=0 then goto TOError;
if liCardDev>0 then goto TOInied;//写卡器已经打开
for iCount:=0 to 3 do begin //init COM1~COM4
liCardDev:=FTic_init(GetProcAddress(hCardDLL,'ic_init'))(iCount,9600);
if liCardDev>0 then break;
end; if liCardDev<=0 then goto TOError;
FTdv_beep(GetProcAddress(hCardDLL,'dv_beep'))(liCardDev,40{*10ms});
TOInied:
Result:=True; Exit;
TOError:
Result:=False; Exit;
end;
Function TFrmRWCard.bFunCardDevExit: Boolean;
Label TOError,TOExited;
var
iRet:integer;
begin //dynamic Free CardDevice Handle, Close CardDevice
if hCardDLL=0 then goto TOError;
if liCardDev<=0 then goto TOExited;//写卡器已经关闭
FTdv_beep(GetProcAddress(hCardDLL,'dv_beep'))(liCardDev,20{*10ms});
iRet:=FTic_exit(GetProcAddress(hCardDLL,'ic_exit'))(liCardDev);
if iRet<>0 then goto TOError; liCardDev:=0;
TOExited:
Result:=True; Exit;
TOError:
Result:=False; Exit;
end;
Function TFrmRWCard.bFunTestCardDev: Boolean;
begin //Test CardDevice
Result:=bFunCardDevIni;
end;
Function TFrmRWCard.bFunChkCardStatus: Boolean;
Label TOChkOK,TOError;
var
siRet,siOut: smallint;
wAsk: Word;
begin //Test CardDevice
repeat
siRet:=FTget_status(GetProcAddress(hCardDLL,'get_status'))(liCardDev,@siOut);
if siRet<>0 then goto TOError;
if siOut<>1 then begin
wAsk:=wFunShowASK('写卡器内无卡,请插卡!',[mbOK]+[mbCancel]);
if wAsk=mrCancel then goto TOError;
end;
until siOut=1;
siRet:=FTchk_4442(GetProcAddress(hCardDLL,'chk_4442'))(liCardDev);
if siRet<>0 then begin
wFunShowError('非系统用卡!',[mbOK]); goto TOError;
end;
siRet:=FTrsct_4442(GetProcAddress(hCardDLL,'rsct_4442'))(liCardDev,@siOut);
if siRet<>0 then goto TOError;
if siOut=0 then begin
wFunShowError('失效卡(无法读写)!',[mbOK]); goto TOError;
end;
TOChkOK:
Result:=True; Exit;
TOError:
Result:=False; Exit;
end;
Function TFrmRWCard.bFunReadCardMem: Boolean;
var
siRet: smallint;
begin
siRet:=FTsrd_4442(GetProcAddress(hCardDLL,'srd_4442'))
(liCardDev,CEMemOrn,CEMemLen,@aCMem[CEMemOrn]);
if siRet=0 then Result:=True
else begin
wFunShowError('无法读取卡内数据!',[mbOK]);Result:=False;
end;
end;
Function TFrmRWCard.bFunChkEMemDataType(var ReadCD: RecRWCustDataType): Boolean;
begin
if (aCMem[CEMemOrn]=$68) or (aCMem[CEMemOrn]=$ff) then
begin
ReadCD.sEMemDT:=TaiYuanMemKind;
Result:=True;
end
else Result:=False;
end;
Function TFrmRWCard.bFunWriteCardMem(sInICSC: string; sOutICSC: string): Boolean;
Label TOWriteError,TOWriteOK,TOPswError;
var
aAsc: Array[0..6] of char;
aPsw: Array[0..3] of char;
siRet: smallint;
begin
siRet:=FTturn_on(GetProcAddress(hCardDLL,'turn_on'))(liCardDev);
if siRet<>0 then goto TOWriteError;//对卡上电
strpcopy(aAsc,sInICSC);
FTasc_hex(GetProcAddress(hCardDLL,'asc_hex'))(aAsc,aPsw,3);
siRet:=FTcsc_4442(GetProcAddress(hCardDLL,'csc_4442'))(liCardDev,3,aPsw);
if siRet<>0 then goto TOPswError;//核对进入密码
siRet:=FTswr_4442(GetProcAddress(hCardDLL,'swr_4442'))
(liCardDev,CEMemOrn,CEMemLen,@aCMem[CEMemOrn]);
if siRet<>0 then goto TOWriteError;
strpcopy(aAsc,sOutICSC);
FTasc_hex(GetProcAddress(hCardDLL,'asc_hex'))(aAsc,aPsw,3);
siRet:=FTwsc_4442(GetProcAddress(hCardDLL,'wsc_4442'))(liCardDev,3,aPsw);
if siRet<>0 then goto TOPswError;//设置退出密码
siRet:=FTturn_off(GetProcAddress(hCardDLL,'turn_off'))(liCardDev);
if siRet<>0 then goto TOWriteError;//对卡下电
TOWriteOK:
Result:=True; Exit;
TOPswError:
wFunShowError('IC卡密码错!',[mbOK]);
Result:=False; Exit;
TOWriteError:
wFunShowError('无法写入卡内数据!',[mbOK]);
Result:=False; Exit;
end;
Function TFrmRWCard.bFunWriteCardCustData(WriteCD: RecRWCustDataType;
WriteCK: RecWriteCardKindType): Boolean;
Label TOExitOK,TOWriteError,TOExitError;
var
hICMemDLL{IC卡内存函数动态链接库句柄}: THandle;
ReadCK: RecWriteCardKindType;
bRet,bSet: Boolean;
sInICSC,sOutICSC: string;
begin //写用户数据
if not bFunChkCardStatus then goto TOWriteError;
if not bFunReadCardMem then goto TOWriteError;
hICMemDLL:=hFunGetLoadICMemDLL(WriteCD.sEMemDT);
if hICMemDLL=0 then goto TOExitError;
ReadCK.ECK:=FTget_ecardkind(GetProcAddress(hICMemDLL,
'get_ecardkind'))(@aCMem[CEMemOrn]);
bRet:=False; bSet:=False;
case WriteCK.ECK of
EmptyCard: begin //写空白卡
if ReadCK.ECK=CloseCard then
begin bSet:=True; sInICSC:=WriteCD.Cust.sICSC; end
else if ReadCK.ECK=ForChkCard then
wFunShowError('检查卡尚未使用!',[mbOK])
else if ReadCK.ECK=ChkedCard then
begin bSet:=True; sInICSC:=OriCardPsw; end
else wFunShowError('卡型错误!',[mbOK]);
sOutICSC:=OriCardPsw;
end;
NewCustCard: begin //写开户卡
sInICSC:=OriCardPsw; sOutICSC:=OriCardPsw;
if ReadCK.ECK=EmptyCard then bSet:=True//ReadCK.ECK为空卡
else wFunShowError('非空白卡!',[mbOK]);
end;
PatchForBuyCard: begin //补待购卡
sInICSC:=OriCardPsw; sOutICSC:=OriCardPsw;
if ReadCK.ECK=EmptyCard then bSet:=True//ReadCK.ECK为空卡
else wFunShowError('非空白卡!',[mbOK]);
end;
BuyCard: begin //写申购卡
if (ReadCK.ECK=ForBuyCard) or (ReadCK.ECK=PatchForBuyCard)
then begin bSet:=True; sInICSC:=OriCardPsw; end
else if (ReadCK.ECK=BuyCard) or (ReadCK.ECK=PatchBuyCard)
then begin bSet:=True; sInICSC:=WriteCD.Cust.sICSC; end
else wFunShowError('非申购卡!',[mbOK]);
sOutICSC:=WriteCD.Cust.sICSC;
end;
PatchBuyCard: begin //补申购卡
sInICSC:=OriCardPsw;
if ReadCK.ECK=EmptyCard then bSet:=True//ReadCK.ECK为空卡
else wFunShowError('非空白卡!',[mbOK]);
sOutICSC:=WriteCD.Cust.sICSC;
end;
ForChkCard: begin//写待查卡
sInICSC:=OriCardPsw; sOutICSC:=OriCardPsw;
if ReadCK.ECK=EmptyCard then bSet:=True//ReadCK.ECK为空卡
else wFunShowError('非空白卡!',[mbOK]);
end;
UnBuyCard: begin //写结算卡
if ReadCK.ECK=ForBuyCard then begin
bSet:=True; sInICSC:=OriCardPsw; end
else if ReadCK.ECK=BuyCard then begin
bSet:=True; sInICSC:=WriteCD.Cust.sICSC; end
else wFunShowError('非申购卡!',[mbOK]);
sOutICSC:=WriteCD.Cust.sICSC;
end;
CloseCard: ; //写销户卡
else
bRet:=False; bSet:=False;
end;
if bSet then bRet:=FTset_ecardmem(GetProcAddress(hICMemDLL,
'set_ecardmem'))(WriteCD,WriteCK,@aCMem[CEMemOrn]);
if not bRet then goto TOWriteError;
if not bFunWriteCardMem(sInICSC,sOutICSC) then goto TOWriteError;
TOExitOK:
Result:=True; Exit;
TOWriteError:
bFunFreeICMemDLL(hICMemDLL);
TOExitError:
Result:=False; Exit;
end;
Function TFrmRWCard.bFunReadCardCustData(var ReadCD: RecRWCustDataType;
var ReadCK: RecWriteCardKindType): Boolean;
Label TOExitOK,TOExitError,TOReadError;
var
hICMemDLL{IC卡内存函数动态链接库句柄}: THandle;
bRet: Boolean;
begin //读用户数据
if not bFunChkCardStatus then goto TOReadError;
if not bFunReadCardMem then goto TOReadError;
if not bFunChkEMemDataType(ReadCD) then goto TOReadError;
hICMemDLL:=hFunGetLoadICMemDLL(ReadCD.sEMemDT);
if hICMemDLL=0 then goto TOExitError;
ReadCK.ECK:=FTget_ecardkind(GetProcAddress(hICMemDLL,
'get_ecardkind'))(@aCMem[CEMemOrn]);
bRet:=FTget_ecarddata(GetProcAddress(hICMemDLL,
'get_ecarddata'))(ReadCD,ReadCK,@aCMem[CEMemOrn]);
if not bRet then goto TOReadError;
TOExitOK:
Result:=True; Exit;
TOReadError:
bFunFreeICMemDLL(hICMemDLL);
TOExitError:
Result:=False; Exit;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -