«á¤@¶ «e¤@¶ ¦^¥Ø¿ý ¦^º¶ |
10.3.2.2 ¦øªA¾¹µ{¦¡ªº½s¼g¡@ ¡@¡@¦øªA¾¹µ{¦¡¥²¶·¥]§t¹ïDLLªº©I¥s¥N½X¡A¦p¡G¡@ function GetGlobalMem: THandle; far; external 'c:\dlls\glbmem'; ¡@¡@³q¹L©I¥s¸Ó¨ç¼Æ¡A¦øªA¾¹¥i¥HÀò±o¥þ§½°O¾ÐÅé¶ôªº¥y¬`¡C ¡@¡@¦b¼g¤J¼Æ¾Ú«e¡A¦øªA¾¹¥²¶·Âê©w¥þ§½°O¾ÐÅé¡A¥HÁקK¦b¼g¤J¹Lµ{¤¤ Windows²¾°Ê¸Ó°O¾ÐÅé¶ôªº¦ì¸m¡C¡@¡@¨ç¼Æ GlobalLockÂê©w¥þ§½°O¾ÐÅé¨Ãªð¦^«ü¦V¸Ó°O¾ÐÅé¶ôªº«ü¼Ð¡G¡@pMem := GlobalLock(hMem); ¡@¡@¹ï pMemªº¥ô¦óקﳣ·|¤Ï¬M¨ì¥þ§½°O¾ÐÅé¶ô¤¤¡C¡@¡@¹ï°O¾ÐÅé¶ô¶i¦æ¾Þ§@«á¡A©I¥s GlobalUnLock¶i¦æ¸ÑÂê¡C°O¾ÐÅé¶ô¾Þ§@¤§«áºÉ¦¸ÑÂê¡A¦³§Q©óWindows¥R¤À§Q¥Î°O¾ÐÅé¸ê·½¡C¡@¡@¦øªA¾¹¼g¤J¼Æ¾Úªº¹ê²{¥N½X¦p¤U¡C¡@ var hMem: THandle; pMem: PChar; begin hMem := GetGlobalMem; {Àò±o¥þ§½°O¾ÐÅé¶ôªº¥y¬`} if hMem <> 0 then begin pMem := GlobalLock(hMem); {¥[Âê¥þ§½°O¾ÐÅé¶ô} if pMem <> nil then begin StrPCopy(pMem,Memo1.text); {¦V¥þ§½°O¾ÐÅé¶ô¼g¤J¼Æ¾Ú} GlobalUnlock(hMem); {¸ÑÂê¥þ§½°O¾ÐÅé¶ô} end else MessageDlg('Couldnot Lock memory block',mtWarning,[mbOK],0); end;¡@ 10.3.2.3 ¥Î¤áµ{¦¡ªº½s¼g¡@ ¡@¡@¥Î¤áµ{¦¡´X¥G¬O¦øªA¾¹µ{¦¡ªºÂ½ª©¡C°ß¤@ªº°Ï§O¦b©ó¤@Ó¬O¼g¤J¼Æ¾Ú¡A¤@Ó¬O¤U¸ü¼Æ¾Ú¡C ¤U±¬O¥Î¤á±q¥þ§½°O¾ÐÅé¶ô¤U¸ü¼Æ¾Úªºµ{¦¡²M³æ¡C¡@ var hMem: THandle; pMem: PChar; begin hMem := GetGlobalMem; {Àò±o¥þ§½°O¾ÐÅé¶ôªº¥y¬`} if hMem <> 0 then begin pMem := GlobalLock(hMem); {¥[Âê¥þ§½°O¾ÐÅé¶ô} if pMem <> nil then begin Memo1.text := StrPas(pMem); {±q¥þ§½°O¾ÐÅé¶ôŪ¨ú¼Æ¾Ú} GlobalUnlock(hMem); {¸ÑÂê¥þ§½°O¾ÐÅé¶ô} end else MessageDlg('Couldnot Lock memory block',mtWarning,[mbOK],0); end; 10.4 §Q¥ÎDLLs¹ê²{µ¡Å髥Ρ@ ¡@¡@¹ê²{µ¡Å髥άO Delphi DLLs¥\¯à¤¤¤@Ó¤Þ¤Hª`¥Øªº¯S¦â¡C·í§A«Ø¥ß¤F¤@Ó¥O¦Û¤vº¡·Nªº¤@¯ëªºµ¡Åé¨Ã§Æ±æ¯à¦b¤£¦PÀ³¥Îµ{¦¡¤¤¨Ï¥Î¡A¯S§O¬O§Æ±æ¯à¦b«DDelphi À³¥Îµ{¦¡¤¤¨Ï¥Î®É¡A§âµ¡Åé°µ¶i¤@ӰʺAÁåµ²®w¤¤¬O³Ì¾A·íªº¡C³o¼Ë§Y¨Ï¥Î¨ä¥¦¤u¨ã¶}µoªºÀ³¥Îµ{¦¡¡A¦pC++¡BVisual Basicµ¥¡A¤]³£¥i¥H¥h©I¥s¥¦¡C¡@¡@¥]§tµ¡Å骺 DLLs¦³100K¥ª¥kªº³¡¥ó®w(Component Library)¶}¾P¡C¥i¥H³q¹L§â´XÓµ¡Åé½sĶ¦¨¤@ÓDLLs¨Ó³Ì¤p¤Æ³oµ§¶}¾P¡CDLl¤¤ªº¤£¦Pµ¡Åé¥i¥H¦@¨É³¡¥ó®w¡C¡@10.4.1 §Q¥ÎDLLs¹ê²{µ¡Å髥Ϊº¤@¯ë¨BÆJ¡@ ¡@¡@§Q¥ÎDLLs¹ê²{µ¡Å髥Ϊº¨BÆJ¬O¡G ¡@¡@1.¦b¾ã¦X¶}µoÀô¹Ò(IDE)¤¤¡A«ö¦Û¤vªº»Ýn³]p¤@Óµ¡Åé¡F ¡@¡@2.½s¼g¤@ӥΩó¿é¥Xªº¨ç¼Æ©Î¹Lµ{¡C¦b¸Ó¨ç¼Æ©Î¹Lµ{¤¤¡A³]pªºµ¡Åé³Q¹ê¨Ò¤Æ¡F ¡@¡@3.«½Æ¨BÆJ1¡B2¡Aª½¨ì§¹¦¨©Ò¦³«¥Îµ¡Å骺³]p¡F ¡@¡@4.¥´¶}¤uµ{¤å¥ó¡A¶i¦æקï¡A¥H¾AÀ³²£¥Í .dll¤å¥óªº»Ýn¡G ¡@¡@(1).§â«O¯d¦rprogram³]¬°library¡F ¡@¡@(2).±quses¤l¥y¤¤¥h±¼Forms³æ¤¸¡F ¡@¡@(3).²¾¥hbegin,end¤§¶¡ªº©Ò¦³¥N½X¡F ¡@¡@(4).¦buses¤l¥y¤U¡Abegin¡Kend¶ô¤§«e¡A¥[¤J«O¯d¦rexprots¡Cexports «á¬O¿é¥X¨ç¼Æ¦W©Î¹Lµ{¦W¡C ¡@¡@5.½sĶ²£¥ÍDLLs¤å¥ó¡F ¡@¡@6.¦b¨ä¥¦À³¥Îµ{¦¡¤¤©I¥s«¥Îµ¡Åé¡C ¡@¡@«¥Îµ¡Å骺©I¥s¦P¤@¯ëDLLs¨ç¼Æ©Î¹Lµ{ªº©I¥s§¹¥þ¤@P¡A¤£¦AÂØz¡CŪªÌ¥i°Ñ¬Ý¤U±ªº¨Ò¤l¡C¡@ 10.4.2 µ¡Å髥ιê¨Ò¡@ ¡@¡@¤U±§Ú̳q¹L¤@Ó¨ãÅ骺¹ê¨Ò¨Ó±Ôzµ¡Å髥Ϊº³]p¹Lµ{¡C§Ú̦b¤@Ó¦W¬°passform.dll ªº¤å¥ó¤¤Àx¦s¤F¤@Ó±K½X³]©wµøµ¡©M¤@Ó±K½XÀˬdµøµ¡¡C¦Ó«á¦b¤@ÓDelphi ½s¼gªºµ{¦¡©M¤@ÓVB½s¼gªºµ{¦¡¤¤¶i¦æ©I¥s¡C¨Æ¹êÃÒ©ú³oºØ¤èªk¬O§¹¥þ¥i¦æªº¡C 10.4.2.1 µ¡Åé«¥ÎDLLsªº³]p¡@ ¡@¡@µ¡Åé«¥ÎDLLsªº³]p¨Ì·Ó(10.4.1)¤¤¤¶²Ðªº¨BÆJ¶i¦æ¡CDLLs¤¤ªº¨âÓµ¡Åé SetPassWordForm©MGetPassWordForm¤À§O¥Î©ó³]©w©MÀˬd±K½X¡C¥¦Ìªº³]p¤¶±¦p¹Ï©Ò¥Ü¡C µ¡ÅéÃþ TSetPassWordForm©w¸q¤F¨âӼƾڦ¨ûVerified©MPassWord¡A¥Î©ó°O¿ý±K½X½T»{ª¬ºA©M³]©wªº±K½X¡CTSetPassWordFormªº©w¸q¦p¤U¡Gtype TSetPassWordForm = class(TForm) Label1: TLabel; Edit1: TEdit; OKBtn: TBitBtn; CancelBtn: TBitBtn; procedure FormCreate(Sender: TObject); procedure Edit1KeyPress(Sender: TObject; var Key: Char); private { Private declarations } Verified: Boolean; public { Public declarations } PassWord: PChar; end; ¡@¡@µøµ¡²£¥Í®É¡A¹ï¼Æ¾Ú¦¨û©M³¡¥óª¬ºA¶i¦æªì©l¤Æ¡G¡@ procedure TSetPassWordForm.FormCreate(Sender: TObject); begin Verified := False; PassWord := StrAlloc(40); OKBtn.Enabled := False; Label1.Caption := 'Please Input PassWord:'; end; ¡@¡@«ö¶s OKBtn¦bµ{¦¡±Ò°Ê®ÉEnabledÄݩʳ]©w¬°False¡Aª½¨ì±K½X³Q¥¿½T³]©w«áEnabledÄݩʤ~«ì´_¬°True¡C³o¼Ë´N«OÃÒ¤F¥u¦³±K½X³Q¥¿½T³]©w«á¡A±K½X³]©wµøµ¡¤~¯à¥¿±`Ãö³¬¡C§_«h¥u¯à«öCancel«ö¶s¨ú®ø¡C¡@¡@¦b±K½X³]©w¥N½X³æ¤¸¤¤©w¸q¤F¤@Ó¿é¥X¨ç¼Æ SetPassWord¡A¥Î©ó²£¥Í±K½X³]©wµøµ¡¨Ãªð¦^³]©wªº±K½X¡G¡@function SetPassWord(PWord: PChar): Boolean; var SetPassWordForm: TSetPassWordForm; begin Result := False; SetPassWordForm := TSetPassWordForm.Create(Application); try with SetPasswordForm do if ShowModal = mrOK then begin StrCopy(PWord,StrUpper(Password)); Result := True; end; finally SetPasswordForm.Free; end; end; ¡@¡@±K½X¦¨¥\³]©w¡A§â PassWordªºÈ«þ¨©µ¹PWord¿é¥X¡A¨Ãªð¦^True¡CÀ³¸Óª`·Nªº¬O¥Ñ©ó PWord¥»¨´N¬O«ü¼Ð«¬¦¡¡A«ü¦V¤@Ó¦r¤¸¦êªº¦ì§}¡A¦]¦ÓÁöµMPWord¥Î©ó¿é¥X¡A¦ý¦b°Ñ¼Æªí¤¤¤´¬°¶ÇȰѼơA¦Ó¤£¬O¶Ç§}°Ñ¼Æ¡C¥t¥~©I¥s¨ç¼ÆStrCopy¡An¨DPWord¦b¶Ç¤J«e¤w¤À°t°O¾ÐÅé¡A§_«h·|¾ÉP¤@Ó¤@¯ë«OÅ@¿ù¡Ctry...finally¥Î©ó«OÅ@µøµ¡©Ò¦û¥Î°O¾ÐÅé¸ê·½¦b¥ô¦ó±¡ªp¤U³£¯à¥¿±`ÄÀ©ñ¡AŪªÌ¥i°Ñ¬Ý²Ä¤Q¤G³¹¡C¡@¡@¦b±K½X³]©wµøµ¡¤¤¡A¬°¤F½T«O¥Î¤á°O¦í¤F³]©wªº±K½X¡A¦b¥Î¤á¿é¤J¨Ã«ö¦^¨®Áä«á¡An¨D¥Î¤á¦A¦¸¿é¤J¶i¦æ½T»{¡C¥u¦³¥Î¤á«·s¿é¤Jªº¦r¤¸¦ê»Pì³]©w±K½X¬Û¦P¡A±K½X³]©wµøµ¡¤~¯à¥¿±`Ãö³¬ ¡C§_«h±Nì³]©w±K½X²MªÅ¡An¨D¥Î¤á¦A¦¸¿é¤J¡C¥H¤W¥\¯àªº¹ê²{¦b½s¿è®ØªºOnKeyPress¨Æ¥ó³B²z¹Lµ{¤¤¡C¡@procedure TSetPassWordForm.Edit1KeyPress(Sender: TObject; var Key: Char); begin if Edit1.text = '' then Exit; if Key = #13 then begin if Verified then if StrPas(PassWord) = Edit1.text then begin OKBtn.Enabled := True; Edit1.Enabled := False; OKBtn.SetFocus; end else begin Verified := False; MessageDlg('PassWord is InValid.',mtWarning,[mbOK],0); Edit1.text := ''; PassWord := ''; Label1.Caption := 'Please Input PassWord:'; end else begin Verified := True; StrPCopy(PassWord,Edit1.text); Edit1.text := ''; Label1.caption := 'Please Verify PassWord:'; end; Key := #0; end; end; ¡@¡@±K½XÀˬdµøµ¡ªº¹ê²{¬Û¹ï²³æ¡A¥u©w¸q¤F¤@Ó¿é¥X¨ç¼Æ GetPassWord¡A¥Î©ó²£¥Í±K½XÀˬdµøµ¡¨Ãªð¦^±K½XÀˬdªºµ²ªG¡C¡@function GetPassword(Password: PChar): Boolean; var GetPasswordForm: TGetPasswordForm; begin Result := False; GetPasswordForm := TGetPasswordForm.Create(Application); try with GetPasswordForm do if ShowModal = mrOK then if UpperCase(Edit1.Text) <> StrPas(StrUpper(Password)) then MessageDlg('Invalid Password', mtWarning, [mbOK], 0) else Result := True; finally PasswordForm.Free; end; end; ¡@¡@ PassWord¬°¿é¤Jªº°Ñ¼Æ¡A¤£¯à¬°ªÅ¡A¥Ñ©I¥s¥H¤W¨ç¼Æªºµ{¦¡t³dºûÅ@¡C¡@¡@µøµ¡¤¤¥Î¤á¿é¤J±K½X®É¦^Åã¦b¿Ã¹õ¤Wªº¦r¤¸¥Ñ½s¿è®Øªº PassWordCharÄݩʽT©w¡C¡@¡@¦b DLLsªº¤uµ{¤å¥ó¤¤¡A§â¨âÓ¿é¥X¨ç¼Æ¼g¨ìexports¤l¥y¤¤¡C¡@library PassForm;¡@ uses GetPass in 'GETPASS.PAS' {PasswordForm}, Setpass in 'SETPASS.PAS' {SetPassWordForm};¡@ exports GetPassword,SetPassWord;¡@ begin end.¡@ 10.4.2.2 DelphiÀ³¥Îµ{¦¡©I¥s«¥Îµ¡Åé¡@ ¡@¡@¦bDelphiÀ³¥Îµ{¦¡¤¤©I¥s«¥Îµ¡Åé¡Aº¥ý¥²¶·¥]§tpassform.dllªº¨âÓ¿é¥X¨ç¼Æ¡G¡@ function GetPassword(Password: PChar): Boolean; far; external 'c:\dlls\PassForm'; function SetPassword(PassWord: PChar): Boolean; far; external 'c:\dlls\PassForm'; ¡@¡@³o¦ì©óµ{¦¡³æ¤¸ªº implementation³¡¤À¡C±K½X³]©w³¡¤Àªº¹ê²{¥N½X¬°¡G¡@ procedure TForm1.SetButtonClick(Sender: TObject); begin PassWord := StrAlloc(40); if SetPassWord(PassWord) = False then MessageDlg('PassWord is not set',mtInformation,[mbOK],0); end; ¡@¡@º¥ý¬°±K½X¦r¤¸¦ê¤À°t°O¾ÐÅé¡C·í±K½X³]©wµ¡Åé«ö Cancel«ö¶s¨ú®ø®É¡AÅã¥Ü¬ÛÀ³ªº¸ê°T¡C¡@¡@±K½XÀˬd³¡¤Àªº¹ê²{¥N½X¬°¡G¡@ procedure TForm1.TestButtonClick(Sender: TObject); begin if PassWord = nil then begin MessageDlg('Set password first', mtInformation, [mbOK], 0); SetButton.SetFocus; Exit; end; if GetPassword(PassWord) then Label1.Caption := 'You are Wellcome !' else Label1.Caption := 'Sorry,You are InValid User.'; end; ¡@¡@®Ú¾Ú±K½XÀˬdªºµ²ªG¡A¦b¼ÐÅҮؤ¤Åã¥Ü¬ÛÀ³ªº¸ê°T¡C¡@ 10.4.2.3 VBÀ³¥Îµ{¦¡©I¥s«¥Îµ¡Åé¡@ ¡@¡@VB¬O·L³n¤½¥q·¥¤O±ÀÂ˪º¤@Ó¥iµø¤Æ¶}µo¤u¨ã¡C¥¦ÁöµM¨Ã¤£¤ä´©°ÊºAÁåµ²®wªº«Ø¥ß¡A¦ý¥i¥H©I¥s¼Ð·ÇªºWindows API°ÊºAÁåµ²®w©M¥Î¨ä¥¦»y¨¥½s¼gªº°ÊºAÁåµ²®w¡C¬°¤FÅçÃÒ©Ò²£¥ÍDLLsªº´¶¾A©Ê¡A§ÚÌ¥ÎVB¶}µo¤F¤@Ó²³æªºµ{¦¡¨Ó©I¥spassform.dll¤¤Àx¦sªºµ¡Åé¡C ¤U±¬O VBµ{¦¡ªº§¹¾ã¥N½X¡A©MDelphiµ{¦¡ªº¹ïÀ³³¡¤À°ò¥»¤@P¡C¡@Option Explicit Declare Function GetPassWord Lib "c:\dlls\passform.dll" (ByVal PassWord As String) As Integer Declare Function SetPassWord Lib "c:\dlls\passform.dll" (ByVal PassWord As String) As Integer¡@ Dim PassWord As String * 40 Sub Check_Click () If PassWord = "" Then MsgBox ("Enter sample password first") SetPass.SetFocus Else If GetPassWord(PassWord) Then StatusLbl.Caption = "You are Welcome!" Else StatusLbl.Caption = "Sorry,You are Invalid User." End If End If End Sub Sub SetPass_Click () If SetPassWord(PassWord) = 0 Then MsgBox ("PassWord is not Set.") End If End Sub ¡@¡@¦³Ãö VBµ{¦¡³]pªº¤@¨Ç¨ãÅé°ÝÃD¡AŪªÌ¥i°Ñ¬Ý¦³ÃöªºVB°Ñ¦Ò®Ñ¡C¡@10.4.3 ¤pµ²¡@ ¡@¡@¥»³¹§ÚÌ°Q½×ªº¬O°ÊºAÁåµ²®wµ{¦¡³]p¡C³\¦h¥iµø¤Æ¶}µo¤u¨ã¡]¦pVisual Basic¡^¤£¤ä´© DLLsªº«Ø¥ß¡A¦ÓDelphi¦b³oùؤS¦³¤W¼ªºªí²{¡C¯S§O¬Oµ¡Å髥ξ÷¨î¬ODelphi¹ïWindows¤UDLLsµ{¦¡³]pªº¤@Ó«¤j§ï¶i¡C¦b¤@¯ëªºDLLsµ{¦¡³]p¤¤¤]Åé²{¤FDelphi§Ö±¶¡B¤è«Kªº¯SÂI¡C°ÊºAÁåµ²®w¬O Windows¤Uµ{¦¡¸s¸s²Õ´ªº¤@ºØ«n¤è¦¡¡A¨Ï¥Î°ÊºAÁåµ²®w¥i¥H·¥¤j¦a«OÅ@¥Î¤á¦b¤£¦P¶}µo¤u¨ã¡B¤£¦P®É´Á©Ò°µªº¤u§@¡C§Q¥Î°ÊºAÁåµ²®w¡A¥Î¤á¥i¥H³v¨B¥hºc¿v¦Û¤vªºµ{¦¡¼Ò¸s²Õ®w¡A¬°¤µ«áªº¤u§@¿n²Ö¯À§÷¡C ¡@ |
«á¤@¶ «e¤@¶ ¦^¥Ø¿ý ¦^º¶ |