Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Closing my browser

Title: Closing my browser Question: How can I close my Web Browser? Answer: The need of controlling the web browser through an application made in Delphi, involves many aspects, but that in a moment was of my interest, was as closing any browser that is found opened, upon saying any, I refer me to instances of the Internet Explorer and/or Netscape Communicator inclusive Opera. This was outlining a challenge and the different alternative could happen, by closing the application obtaining the Handle of the process or processes, something which not always grants a normal exit or perhaps to close enum windows opened in that moment and then obtaining that handle for thereinafter to send the close order, this solution is but clean that the first but not the but elegant. Well after reading me something of the documentation of the IE and something of that of Windows and remember my projects with Visual Fox and the DDE frame my solution in the use of the DDE (Dinamic Data Exchange). Here I show what need to implement: 1. Create a procedure similar to: procedure TForm1.CloseBrowser(BrowserName:string); var DDE:TDDEClientConv; begin DDE:=TDDEClientConv.Create(Self); Try // uncomment to close the last instance of Browser. { if DDE.SetLink(BrowserName,'WWW_Exit') then if DDE.PokeData('anything' ,'anything') then ShowMessage('Browser closed'); } While DDE.SetLink(BrowserName,'WWW_Exit') do if DDE.PokeData('anything' ,'anything') then ShowMessage('Browser was closed'); //This line is not necessary Finally DDE.Free; End; end; 2. The call to the method must be accomplished with 'IExplore for the Internet Explorer, 'Netscape for the Netscape Communicator and 'Opera for the Opera. Do you know some other browser? Tell me to prove this method with them. procedure TForm1.BitBtn4Click(Sender: TObject); begin CloseBrowser ('Opera') end; 3. Observe that especially this procedure is a method of the class TForm1, this will permit the call Self, without external references. 4. This method closes the last opened instance of the browser wished, as we know as much Internet Explorer as Communicator work with separate instances, but in the case of Opera the application is MDI therefore is not needed a iterative process, though either it is a mistake. Of any both way forms of work are you validated with the IF or the WHILE. 5. I need to do an observation in the work with Opera. This navigator is behaved in one way peculiar, inclusive to be closed, since confirms the exit, I advise to you to deactivate this characteristic if is going to use the method with the WHILE. This can carry to you to an infinite loop since is ordered to the browser be Closed and while this open will be followed executing the procedure that means that if You do not confirm the exit the infinite loop can hang-up (crash) the computer. Here it is where it can use an alone IF. I leave to you the rest to your imagination. Well, I wait that this serves to you. Thanks.