Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ShellExec to close a Website - possible? 1

Status
Not open for further replies.

german12

Programmer
Nov 12, 2001
563
DE
You can use a shellexec function to open any website.

But can you use a similar function to close the website again if, for example, you only want to look at the website for a moment. (e.g. wait window 5) ???

Of course you can manually click on the cross in the top right of the website with the mouse, but if you want to quickly look at several websites one after the other, it would be desirable to be able to do this automatically in an individual time sequence.

The function I use to open a web-page is this one:

Code:
*Example:
*Source: Book with function by Rick Strahl "Internet Applications with VFp 6,0 - Page 194 edited 1999"

shellExec("[URL unfurl="true"]https://www.google.com/search?client=firefox-b-d&q=aktienkurs+rheinmetall/")[/URL]


FUNCTION ShellExec
LPARAMETERS lcLink,lcAction,lcParms

lcAction = IIF(EMPTY(lcAction), "Open",lcAction)
lcParms = IIF(EMPTY(lcParms),"",lcParms)

DECLARE INTEGER ShellExecute IN shell32.DLL ;
      INTEGER hWinHandle, ;
      STRING cOperation, ;
      STRING cFileName, ;
      STRING cParameters, ;
      STRING cDirectory, ;
      INTEGER nShowWindow

DECLARE INTEGER FindWindow ;
      IN WIN32API ;
      STRING cNull,STRING cWinname

RETURN ShellExecute(FindWindow(0,_Screen.Caption),;
                   lcAction,lcLink, ;
                   lcParms,SYS(2023),1)


There is another effect - when you open too many internet-windows (means you let every site open) it would be better not to stretch the tabs across the windows, there could be too many then, or the memory will be overloaded.
This can be avoided you can do this in the browser settings - but then you have to change it back later because it is normally a sensible setting.

Thanks for help
Klaus


Peace worldwide - it starts here...
 
With ShellExecute you don't get hands on the started process. To have control over the browser you'd need to automate it, which only works with Internet Explorer, that's not a real option anymore.

To at least get hands on the Window you'd need to switch to CreateProcess. Not as easy as it sounds to switch from ShellExecute to CreateProcess. I'll get back to this after having a full example. In the meantime you can get an impression of how complex it will get from a ample of Rick Strahl:
What you get with CreateProcess, that's not available with ShellExecute or RUN is a process handle. That can be used in several functions, one is TerminateProcess, i.e. what you want to do.

Klaus said:
when you open too many internet-windows (means you let every site open) it would be better not to stretch the tabs across the windows, there could be too many then, or the memory will be overloaded.
Browsers will give tabs a minimum width, there's never the problem of too many tabs not being accessible. What is true is that browsers with too many open tabs become unresponsive. So sure, you better not open 100 tabs. Why would you? OPening a browser is something you only do to let a user look at it and work with it, one website at a time would be something I'd do. Why would you, say, opüen all URLs you know?

If you open all URLs to provcess them, don't open them in a browser, just use http requests to get the result page html and process that, there's no need to open a browser at all, if you want to do that.

Chriss
 
You can't do this with ShellExecute(). That's because ShellExecute() simply launches a program or URL. Once it has done that, VFP has no control over it.

You might be able to do it with Windows Scripting. Something like this:

Code:
* To launch the web page
lcTarget = "[URL unfurl="true"]https://www.tek-tips.com/viewthread.cfm?qid=1829740"[/URL]
lcWindow = "Firefox"   && a unique word from the browser's title bar

oSH = CREATEOBJECT("wscript.shell")
oSH.Run(lcTarget)

* When you are ready to close the web page
oSH.AppActivate(lcWindow)
* (you might need to put a short delay here to give
* the window time to activate)
osH.Sendkeys("^W")  && send Ctrl+W to close the tab

This is just off the top of my head. I haven't tested it.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hello,

in my lib I found a function which maybe able to do that if you have the windows handle.

Code:
Function killappbyhandle(tnHwnd)
  Declare Integer GetWindowThreadProcessId In win32API ;
    Integer HWnd, Integer @lpdwProcessId
  Declare Integer OpenProcess In Win32API ;
    integer dwDesiredAccess, Integer bInheritHandle, Integer dwProcessID
  Declare Integer TerminateProcess In Win32API ;
    integer hProcess, Integer uExitCode
  Local lnProcessId
  lnProcessId = 0
  GetWindowThreadProcessId(m.tnHwnd, @lnProcessId)
  Return TerminateProcess(OpenProcess(1, 1, m.lnProcessId), 0) > 0
Endfunc


Regards
tom
 
This is to iterate through open windows and check for part of caption
Code:
Function api_getwindowbyname(cName)
  Local lnfound,lcBuffer,lnoldhwnd,lnhWnd,lnretval
  lnfound = findwindow(0,cName)
  If lnfound>0
    Return lnfound
  Endif

  lnoldhwnd = GetActiveWindow()
  lnhWnd = GetWindow(lnoldhwnd, 0) && First
  Do While lnhWnd <> 0
    lcBuffer = Replicate(Chr(0), 255)
    lnretval = GetWindowText(lnhWnd, @lcBuffer, Len(lcBuffer))
    If lnretval > 0 Then
      cBuffer =  Upper(Left(lcBuffer,lnretval))
      If !Empty(At(Upper(cName),cBuffer))
        lnfound = lnhWnd
        Exit
      Endif
    Endif
    lnoldhwnd = lnhWnd
    lnhWnd = GetWindow(lnoldhwnd, 2) && next
  Enddo
  Return lnfound
Endfunc
 
Mike Lewis,
I tested your code - it runs well.
Thank you.


Peace worldwide - it starts here...
 

What does it mean - if you have the windows handle?

Peace worldwide - it starts here...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top