unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,head;

type
  TForm1 = class(TForm)
    Button2: TButton;
    List: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  mBuf:array[0..100] of Byte;
  mFun,mP1,mP2,mP3,mP4,rt:Word;
  lP1,lP2:LongWord;
  mHand:array[0..16] of Word;
  mHardID:array[0..16] of LongWord;
  rc:array[0..4] of Word;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
    hDic32: THandle;
    i,num:Integer;
    str:string;
    mRockeyNumber:Word;
    tmpBuf:array[0..30] of Byte;
    cStr:array[0..100] of Char;
begin

    List.Items.Clear();

    hDic32:=Loadlibrary('Nrclient.dll');
    if hDic32 = 0 then
    begin
        List.Items.Add('No found Nrclient.dll');
        exit;
    end;

    Rockey := GetProcAddress(hDic32, 'NetRockey');
    
    mP1:=$c44c;
    mP2:=$c8f8;
    mP3:=0;
    mP4:=0;
    for i:=0 to 30 do
       mBuf[i]:=0;
    rt:=0;
    mFun:=RY_FIND;
    rt:= Rockey(mFun,mHand[0],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
    if rt<>0 then
    begin
        FmtStr(str,'Can not find ROCKEY1,error:%x ',[rt]);
        List.Items.Add(str);
        exit;
    end;
    List.Items.Add('Find the first ROCKEY');

    mHardID[0]:=lp1;
    mFun:=RY_OPEN;
    lP2:=0;
    rt:= Rockey(mFun,mHand[0],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
    if rt<>0 then
    begin
        FmtStr(str,'Can not open ROCKEY1,error:%x ',[rt]);
        List.Items.Add(str);
        exit;
    end;
    List.Items.Add('Open ROCKEY1');

    i:=1;
    while rt = 0 do
    begin
        mFun:=RY_FIND_NEXT;
        rt:= Rockey(mFun,mHand[i],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
        if rt<>0 then
           break;

        mHardID[i]:=lP1;
        FmtStr(str,'Find the %xROCKEY',[i+1]);
        List.Items.Add(str);

        mFun:=RY_OPEN;
        rt:= Rockey(mFun,mHand[i],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
        if rt<>0 then
        begin
            FmtStr(str,'Can not open ROCKEY%x',[i+1]);
            List.Items.Add(str);
            break;
        end;
        FmtStr(str,'Open ROCKEY%x',[i+1]);
        List.Items.Add(str);
        i:=i+1;
    end;
    mRockeyNumber:=i;
for num:=0 to mRockeyNumber-1 do
begin
  List.Items.Add('*******************************************');
  FmtStr(str,'Begin to test the %xROCKEY',[num+1]);
  List.Items.Add(str);
{Write}
  mFun:=RY_WRITE;
  for i:=0 to 10 do
      mBuf[i]:=31;
  mP1:=400;
  mP2:=11;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Write ROCKEY Error %x',[rt]);
        List.Items.Add(str);
        break;
   end;
   FmtStr(str,'Write ROCKEY Success',[i+1]);
   List.Items.Add('Write ROCKEY Success');
{Read}
  mFun:=RY_READ;
  for i:=0 to 10 do
      tmpBuf[i]:=0;
  mP1:=400;
  mP2:=11;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,tmpBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Read ROCKEY Error',[rt]);
        List.Items.Add(str);
        break;
   end
   else
   begin
      for i:=0 to 10 do
          if mBuf[i] <> tmpBuf[i] then
              break;
      if i = 11 then
         List.Items.Add('Read ROCKEY Success')
      else
          List.Items.Add('Read ROCKEY Success,But have some differences from writed');
   end;
{Random}
  mFun:=RY_RANDOM;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Detect Random number Error %x',[rt]);
        List.Items.Add(str);
        break;
   end;
   FmtStr(str,'Random Number%x:',[mP1]);
   List.Items.Add(str);
{Seed}
  mFun:=RY_SEED;
  lP2:=$12345678;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Seed key Error %x',[rt]);
        List.Items.Add(str);
        break;
   end;
   FmtStr(str,'Seed%x:Return value:%x,%x,%x,%x',[lP2,mP1,mP2,mP3,mP4]);
   List.Items.Add(str);
   rc[0] := mP1; rc[1] := mP2;
   rc[2] := mP3; rc[3] := mP4;
{Write User ID
  mFun:=RY_WRITE_USERID;
  lP1:=100;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Write User ID Error %x',[rt]);
        List.Items.Add(str);
        break;
   end;
   FmtStr(str,'Write USER ID%x',[lP1]);
   List.Items.Add(str);}
{Read User ID}
  mFun:=RY_READ_USERID;
  lP1:=0;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Read User ID Error %x',[rt]);
        List.Items.Add(str);
        break;
   end;
   FmtStr(str,'The User ID is %x',[lP1]);
   List.Items.Add(str);
{Set module
  mFun:=RY_SET_MOUDLE;
  mP1:=7;
  mP2:=$2122;
  mP3:=1;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Set Module %x got error %x',[mP1,rt]);
        List.Items.Add(str);
        break;
   end;
   FmtStr(str,'Set module %x,value is %x,can be decrease',[mP1,mP2]);
   List.Items.Add(str);}
{First check module}
  mFun:=RY_CHECK_MOUDLE;
  mP1:=0;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Check module %x error %x',[mP1,rt]);
        List.Items.Add(str);
        break;
   end;
   if mP1 = 1 then
      if mP2 = 1 then
            FmtStr(str,'Check module %x,valid,can be decrease',[mP1])
      else
            FmtStr(str,'Check module %x,invalid,can not be decrese',[mP1])
   else
     if mP2 = 1 then
            FmtStr(str,'Check module %x,invalid,can be decrese',[mP1])
      else
            FmtStr(str,'Check module %x,invalid,can not be decrese',[mP1]) ;
   List.Items.Add(str);

{Decrease}
 mFun:=RY_DECREASE;
  mP1:=0;
   rt:= Rockey(mFun,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);
   if rt<>0 then
   begin
        FmtStr(str,'Decrese module %x error %x',[mP1,rt]);
        List.Items.Add(str);
        break;
   end;
   FmtStr(str,'Decrese module %x successfully',[mP1]);
   List.Items.Add(str);
 end;



  for num:=0 to mRockeyNumber-1 do
  begin
    rt:= Rockey(RY_CLOSE,mHand[num],lP1,lP2,mP1,mP2,mP3,mP4,mBuf[0]);

    List.Items.Add('Close Rockey4ND');
    break;

   end;
   FreeLibrary(hDic32);

end;


procedure TForm1.Button2Click(Sender: TObject);
begin
close()
end;

end.

