*** 有關Timer event *** |
尚未結案
|
dw1112
一般會員 發表:8 回覆:9 積分:3 註冊:2009-06-16 發送簡訊給我 |
各位,
我的程式會先啟動Security部份,此部份是驗証"加密鎖"是否已插上及密碼是否正確等等,若沒有問題則正常地進入主screen. 問題是我要系统每10秒檢查USB key是否仍然插上等等,我在Security部份宣告一個timer event. 但好像進入主screen後,該timer event消失了。有沒有高手指點? Code list as follows for security.pas unit unit_Security; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, NoxTimerAppApis, ExtCtrls, Math; type Tfrm_Security = class(TForm) Button_Fail: TButton; Panel1: TPanel; ListBox: TListBox; Panel2: TPanel; display_message: TLabel; Button_OK: TButton; Timer1: TTimer; Label1: TLabel; Label2: TLabel; Label3: TLabel; procedure Button_FailClick(Sender: TObject); procedure FormShow(Sender: TObject); procedure Button_OKClick(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } Rtn:Integer; //函數返回值 KeyHandle:array [0..7] of Integer; KeyNumber:Integer; procedure ListMSG(S:String); //LISTBOX INSERT procedure RtnMSG(sTrue,sFalse:String); procedure Check_USBKey(); function Generate_Random_number(): String; function StringToPAnsiChar(stringVar : string) : PAnsiChar; public { Public declarations } class function Execute : boolean; end; var frm_Security: Tfrm_Security; mem_RandomPassword: String; StorageBuffer:array [0..16] of AnsiChar; //掉電保持區資料 MemBuffer:array [0..7] of AnsiChar; //記憶體區資料 uPin:array [0..32] of AnsiChar; //用戶密碼 APPID:Integer; //應用程式標識 GUID:array [0..32] of AnsiChar; //硬體唯一ID year,month,day,hour,minute:Integer; TimeMode:Integer; //時鐘模式 CountMode:Integer; //次數模式 Remain,nMax:Integer; implementation var var_USBOK_item1, var_USBOK_item2, var_USBOK_item3, var_USBOK_item4, var_USBOK_item5, var_USBOK_item6, var_USBOK_item7, var_USBOK_item8, var_USBOK_item9 : Boolean; //Indicate any error for valiate USB Key {$R *.dfm} procedure Tfrm_Security.ListMSG(S: String); begin ListBox.Items.Add(FormatDateTime('tt',now) ': ' S); end; procedure Tfrm_Security.RtnMSG(sTrue, sFalse: String); begin if Rtn=0 then ListMSG(sTrue) else ListMSG(sFalse ' 錯誤碼: ' IntToStr(NoxTimerAppApis.NoxGetLastError())); end; procedure Tfrm_Security.Button_FailClick(Sender: TObject); begin ModalResult := mrAbort; end; class function Tfrm_Security.Execute: boolean; begin with Tfrm_Security.Create(nil) do try Result := ShowModal = mrOk; finally Free; end; end; procedure Tfrm_Security.FormShow(Sender: TObject); begin mem_RandomPassword := Generate_Random_number(); Check_USBKey(); Timer1.Interval := 10000; // 30 seconds end; procedure Tfrm_Security.Button_OKClick(Sender: TObject); begin ModalResult := mrOK; end; procedure Tfrm_Security.Check_USBKey(); { var StorageBuffer:array [0..16] of AnsiChar; //掉電保持區資料 MemBuffer:array [0..7] of AnsiChar; //記憶體區資料 uPin:array [0..32] of AnsiChar; //用戶密碼 APPID:Integer; //應用程式標識 GUID:array [0..32] of AnsiChar; //硬體唯一ID year,month,day,hour,minute:Integer; TimeMode:Integer; //時鐘模式 CountMode:Integer; //次數模式 Remain,nMax:Integer; } begin ListBox.Items.Clear; //查找加密鎖 APPID:=StrToInt('0xA008128C'); //應用程式標識,通過設號工具設置 Rtn:=NoxFind(APPID,@KeyHandle,@KeyNumber); if Rtn = 0 then var_USBOK_item1 := True else var_USBOK_item1 := False; RtnMSG('找到『' IntToStr(KeyNumber) '』只加密鎖! (成功!)','查找加密鎖失敗,請連接加密鎖! (失敗!)'); //獲取GUID Rtn:=NoxGetuid(keyHandle[0],@GUID); if Rtn = 0 then var_USBOK_item2 := True else var_USBOK_item2 := False; RtnMSG('GUID: ' GUID ' (成功!)','獲取失敗' ' (失敗!)'); //打開加密鎖 uPin:='1248f4a7e6ba3201'; // 用戶密碼,通過設號工具設置 Rtn:=NoxOpen(KeyHandle[0],uPin); if Rtn = 0 then var_USBOK_item3 := True else var_USBOK_item3 := False; RtnMSG('打開加密鎖成功!' ' (成功!)','打開加密鎖失敗!' ' (失敗!)' ); //讀取掉電保持區資料 Rtn:=NoxReadStorage(KeyHandle[0],StorageBuffer); if Rtn = 0 then var_USBOK_item4 := True else var_USBOK_item4 := False; RtnMSG('讀取掉電保持區資料:' StorageBuffer ' (成功!)','讀取掉電保持區資料失敗' ' (失敗!)' ); //寫入記憶體區資料 //Rtn:= NoxWriteMem(KeyHandle[0],PAnsiChar( @mem_RandomPassword )); Rtn:= NoxWriteMem(KeyHandle[0], StringToPAnsiChar(mem_RandomPassword) ); if Rtn = 0 then var_USBOK_item5 := True else var_USBOK_item5 := False; RtnMSG('寫入隨機密碼於暫存記憶體' ' (成功!)','寫入記憶體資料失敗' ' (失敗!)' ); //讀取記憶體區資料 Rtn:= NoxReadMem(KeyHandle[0],@MemBuffer); if Rtn = 0 then var_USBOK_item6 := True else var_USBOK_item6 := False; RtnMSG('讀取隨機密碼於暫存記憶體區資料:' MemBuffer ' (成功!)','讀取記憶體區資料失敗' ' (失敗!)' ); //如果設置了時鐘,可調用NoxGetExpiryDateTime獲取失效時間 //獲取失效時間 Rtn:=NoxGetExpiryDateTime(keyHandle[0],@TimeMode,@year,@month,@day,@hour,@minute); if Rtn = 0 then var_USBOK_item7 := True else var_USBOK_item7 := False; RtnMSG('時間模式:' IntToStr(TimeMode) '失效時間:' IntToStr(year) '年' IntToStr(month) '月' IntToStr(day) '日' IntToStr(hour) '時' IntToStr(minute) '分' ' (成功!)', '無法獲取失效時間' ' (失敗!)' ); //如果設置了次數,可調用NoxGetRemnantCount獲取失效次數 //獲取失效次數 Rtn:=NoxTimerAppApis.NoxGetRemnantCount(keyHandle[0],@Remain,@nMax,@CountMode); if Rtn = 0 then var_USBOK_item8 := True else var_USBOK_item8 := False; self.RtnMSG('次數模式:' IntToStr(CountMode) ' 剩餘運行次數:' IntToStr(Remain) ' 最大運行次數:' IntToStr(nMax) ' (成功!)','無法獲取使用次數' ' (失敗!)' ); //關閉加密鎖 Rtn:=NoxClose(KeyHandle[0]); if Rtn = 0 then var_USBOK_item9 := True else var_USBOK_item9 := False; RtnMSG('關閉加密鎖成功' ' (成功!)','關閉加密鎖失敗' ' (失敗!)' ); if var_USBOK_item1 and var_USBOK_item2 and var_USBOK_item3 and var_USBOK_item4 and var_USBOK_item5 and var_USBOK_item6 and var_USBOK_item7 and var_USBOK_item8 and var_USBOK_item9 then begin display_message.Caption := '驗証成功!'; display_message.Font.Color := clBlue; Button_OK.Visible := True; Button_Fail.Visible := False; Timer1.Enabled := True; // If check ok, Timer will enable in order to check Key exists or not per 30 seconds end else begin display_message.Caption := '驗証失敗! 請檢查"加密鎖"是否插上。'; display_message.Font.Color := clRed; Button_OK.Visible := False; Button_Fail.Visible := True; Timer1.Enabled := False; end; end; function Tfrm_Security.Generate_Random_number():String; begin Randomize; Result := IntToStr( Random(99999999)); end; function Tfrm_Security.StringToPAnsiChar(stringVar : string) : PAnsiChar; Var AnsString : AnsiString; InternalError : Boolean; begin InternalError := false; Result := ''; try if stringVar <> '' Then begin AnsString := AnsiString(StringVar); Result := PAnsiChar(PAnsiString(AnsString)); end; Except InternalError := true; end; if InternalError or (String(Result) <> stringVar) then begin Raise Exception.Create('Conversion from string to PAnsiChar failed!'); end; end; procedure Tfrm_Security.Timer1Timer(Sender: TObject); begin //查找加密鎖 APPID:=StrToInt('0xA008128C'); //應用程式標識,通過設號工具設置 Rtn:=NoxFind(APPID,@KeyHandle,@KeyNumber); //獲取GUID Rtn:=NoxGetuid(keyHandle[0],@GUID); //打開加密鎖 uPin:='1248f4a7e6ba3201'; // 用戶密碼,通過設號工具設置 Rtn:=NoxOpen(KeyHandle[0],uPin); //讀取記憶體區資料 Rtn:= NoxReadMem(KeyHandle[0],@MemBuffer); if Rtn = 0 then Label1.Caption := '讀取隨機密碼於暫存記憶體區資料 (成功!)' else Label1.Caption := '讀取記憶體區資料失敗 (失敗!)'; //關閉加密鎖 Rtn:=NoxClose(KeyHandle[0]); Label2.Caption := TimeToStr(time()) ' ------- ' IntToStr(Rtn); if MemBuffer = mem_RandomPassword then Label3.Caption := 'Equal' else begin Label3.Caption := 'NOT Equal'; MessageDLG('Error !', mtError, [mbOK], 0); end; end; end. |
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
|
dw1112
一般會員 發表:8 回覆:9 積分:3 註冊:2009-06-16 發送簡訊給我 |
|
chiehjj
初階會員 發表:21 回覆:63 積分:26 註冊:2002-12-31 發送簡訊給我 |
|
Ktop_Robot
站務副站長 發表:0 回覆:3511 積分:0 註冊:2007-04-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |