?? mainform.pas
字號:
unit MainForm;
interface
{$H+}
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, sChip;
type
HANDLE = LongWord;
TMainForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
Settings1: TMenuItem;
TargetUserID1: TMenuItem;
Passwordforreading1: TMenuItem;
Passwordforwriting1: TMenuItem;
Operations1: TMenuItem;
Open1: TMenuItem;
Close1: TMenuItem;
Openfromlist1: TMenuItem;
RWbyindex1: TMenuItem;
N1: TMenuItem;
Query1: TMenuItem;
SerialNo1: TMenuItem;
N2: TMenuItem;
Setnewreadpassword1: TMenuItem;
Setnewwritepassword1: TMenuItem;
ClearOutput1: TMenuItem;
Memo1: TMemo;
procedure Exit1Click(Sender: TObject);
procedure TargetUserID1Click(Sender: TObject);
procedure Passwordforreading1Click(Sender: TObject);
procedure Passwordforwriting1Click(Sender: TObject);
procedure Open1Click(Sender: TObject);
procedure Close1Click(Sender: TObject);
procedure Openfromlist1Click(Sender: TObject);
procedure RWbyindex1Click(Sender: TObject);
procedure Query1Click(Sender: TObject);
procedure SerialNo1Click(Sender: TObject);
procedure Setnewreadpassword1Click(Sender: TObject);
procedure Setnewwritepassword1Click(Sender: TObject);
procedure ClearOutput1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
hKey : THandle;
UID : WORD ;
szReadPswd : PassArray;
szWritePswd : PassArray;
szKeyPath : Array[0..MAX_PATH*2] of Char;
LineCount : Integer;
function TryOpen() : Boolean;
public
{ Public declarations }
procedure DisplayMessage(info: AnsiString);
end;
var
MainForm1: TMainForm;
implementation
uses Unit2,Unit3,Unit4,Unit5,Unit6;
{$R *.dfm}
function TMainForm.TryOpen() : Boolean;
Var
info: String;
HardSerial: array[0..20] of Char;
rc :Boolean;
begin
Result := False;
if(hKey <> INVALID_HANDLE_VALUE) then
begin
rc := GetsChipSerial(hKey, @HardSerial, 16);
if(rc) then
begin
info := Format('Old handle(0x%x) is still valid.', [hKey]);
DisplayMessage(info);
Result := true;
Exit;
end;
info := Format('Old handle(0x%x) is not valid. Close it.', [hKey]);
DisplayMessage(info);
ClosesChip(hKey);
hKey := INVALID_HANDLE_VALUE;
Exit;
end;
hKey := OpensChip(UID, @szKeyPath);
if(hKey = INVALID_HANDLE_VALUE) then
begin
info := Format('Err: Can not find sChip of USERID = 0x%X', [UID]);
DisplayMessage(info);
Exit;
end;
Result := True;
info := Format('Open a new handle value = 0x%x, and path = %s', [hKey, szKeyPath]);
DisplayMessage(info);
end;
procedure TMainForm.DisplayMessage(info: AnsiString);
var
x: AnsiString;
begin
LineCount := LineCount + 1;
x := IntToStr(LineCount) + ': ' + info;
Memo1.Lines.Append(x);
end;
procedure TMainForm.Exit1Click(Sender: TObject);
begin
if(hKey <> INVALID_HANDLE_VALUE) then
ClosesChip(hKey);
Close;
end;
procedure TMainForm.TargetUserID1Click(Sender: TObject);
var
rc: integer;
info: String;
begin
Form2.UserID := UID;
rc := Form2.ShowModal();
if(rc = mrOk) then
begin
UID := Form2.UserID;
info := Format('Current UserID is change to %04x', [UID]);
DisplayMessage(info);
end;
end;
procedure TMainForm.Passwordforreading1Click(Sender: TObject);
Var
rc: Integer;
info: String;
begin
Form3.DialogType := 1;
Form3.szPassword := szReadPswd;
rc := Form3.ShowModal();
if(rc = mrOk) then
begin
szReadPswd := Form3.szPassword;
info := Format('Current Read Password of software is change to %02x%02x%02x%02x',
[szReadPswd[0], szReadPswd[1], szReadPswd[2], szReadPswd[3]]);
DisplayMessage(info);
end;
end;
procedure TMainForm.Passwordforwriting1Click(Sender: TObject);
Var
rc: Integer;
info: String;
begin
Form3.DialogType := 0;
Form3.szPassword := szWritePswd;
rc := Form3.ShowModal();
if(rc = mrOk) then
begin
szWritePswd := Form3.szPassword;
info := Format('Current write Password of software is change to %02x%02x%02x%02x',
[szWritePswd[0], szWritePswd[1], szWritePswd[2], szWritePswd[3]]);
DisplayMessage(info);
end;
end;
procedure TMainForm.Open1Click(Sender: TObject);
begin
TryOpen();
end;
procedure TMainForm.Close1Click(Sender: TObject);
Var
info: String;
begin
//
info := Format('Close handle = $%x', [hKey]);
ClosesChip(hKey);
hKey := INVALID_HANDLE_VALUE;
DisplayMessage(info);
end;
procedure TMainForm.Openfromlist1Click(Sender: TObject);
Var
rc: Integer;
info: String;
Context: THandle;
Store: array[0..MAX_PATH*16] of Char;
szDevPaths: PChar;
KeyPath: string;
begin
if( CreateEnumsChipContext(@Context, UID) = 0) then
begin
CloseEnumsChipContext(Context);
DisplayMessage('Err : No sChip is attached!');
Exit;
end;
szDevPaths := @Store;
ListKeyForm.ListBox1.Clear();
EnumsChipPaths(Context, szDevPaths);
while( StrLen(szDevPaths) > 0 ) do
begin
info := szDevPaths;
ListKeyForm.ListBox1.Items.Add(info);
szDevPaths := szDevPaths + Strlen(szDevPaths) + 1;
end;
CloseEnumsChipContext(Context);
rc := ListKeyForm.ShowModal();
if(rc <> mrOk) then Exit;
if(hKey <> 0) and (hKey <> INVALID_HANDLE_VALUE) then
begin
info := Format('Ok : Old Handle $%x = is closed.', [hKey]);
ClosesChip(hKey);
hKey := INVALID_HANDLE_VALUE;
DisplayMessage(info);
end;
KeyPath := ListKeyForm.szPath;
hKey := OpensChipByPath(PChar(KeyPath));
if(hKey <> INVALID_HANDLE_VALUE) then
info := Format('Open handle = 0x%x at path = %s', [hKey, KeyPath])
else
info := Format('Open fail at path = %s', [KeyPath]);
DisplayMessage(info);
end;
procedure TMainForm.RWbyindex1Click(Sender: TObject);
begin
Form5.hKey := hKey;
Form5.szReadPswd := szReadPswd;
Form5.szWritePswd := szWritePswd;
Form5.ShowModal();
end;
procedure TMainForm.Query1Click(Sender: TObject);
begin
Form6.hKey := hKey;
Form6.ShowModal();
end;
procedure TMainForm.SerialNo1Click(Sender: TObject);
Var
rc: Boolean;
info: String;
szSerial: array[0..20] of Char;
begin
rc := GetsChipSerial(hKey, szSerial, 20);
if(rc) then
info := Format('serial no is %s.', [szSerial])
else
info := 'Err: get no serial no. Maybe key is absent.';
DisplayMessage(info);
end;
procedure TMainForm.Setnewreadpassword1Click(Sender: TObject);
Var
rc: Integer;
info: String;
NewPass: PassArray;
ret: Boolean;
begin
Form3.DialogType := Integer($80000001);
rc := Form3.ShowModal();
if(rc = mrOk) then
NewPass := Form3.szPassword
else
Exit;
if(NewPass[0] <> Byte($01)) then //check type
begin
DisplayMessage('Err: Wrong password type.');
Exit;
end;
ret := SetsChipPassword(hKey, @szWritePswd[0], @NewPass[0]);
if(ret) then
begin
info := Format('Key''s read password is changed to %02x %02x %02x %02x',
[NewPass[0], NewPass[1], NewPass[2], NewPass[3]]);
DisplayMessage(info);
end
else
DisplayMessage('Err: change read password failed.');
end;
procedure TMainForm.Setnewwritepassword1Click(Sender: TObject);
Var
rc: Integer;
info: String;
NewPass: PassArray;
ret: Boolean;
begin
Form3.DialogType := Integer($80000000);
rc := Form3.ShowModal();
if(rc = mrOk) then
NewPass := Form3.szPassword
else
Exit;
if(NewPass[0] <> Byte($00)) then //check type
begin
DisplayMessage('Err: Wrong password type.');
Exit;
end;
ret := SetsChipPassword(hKey, @szWritePswd[0], @NewPass[0]);
if(ret) then
begin
info := Format('Key''s write password is changed to %02x %02x %02x %02x',
[NewPass[0], NewPass[1], NewPass[2], NewPass[3]]);
DisplayMessage(info);
end
else
DisplayMessage('Err: change write password failed.');
end;
procedure TMainForm.ClearOutput1Click(Sender: TObject);
begin
//
Memo1.Clear;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var i : integer;
begin
//
hKey := INVALID_HANDLE_VALUE;
UID := $ffff;
for i := 0 to 3 do
begin
szReadPswd[i] := Byte($ff);
szWritePswd[i] := Byte($ff);
end;
LineCount := 0;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -