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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically close apps from within VFP? 3

Status
Not open for further replies.

pt777

Technical User
Mar 27, 2004
62
US
Any suggestions on programmatically (safely) closing non-VFP and/or VFP apps: i.e., "Calculator","MyExtra.exe", etc.?

...
*******************
The following API calls does bring the Calculator window to top (but doesn't attempt to close it)
DECLARE Long FindWindow in Win32API String, String
DECLARE Long BringWindowToTop in Win32API Long
Local lnHWND,lcTitle
lcTitle = "Calculator" && or My.Exe or whatever
lnHWND = FindWindow(null,m.lcTitle)
IF m.lnHWND >0 &&if calculator is open
BringWindowToTop(m.lnHWND)
ENDIF
*******************

How might we close the window (captioned lcTitle) using API calls or such?

Thanks in advance,
Philip
 
pt777

The following example code closes Acrobat Reader
Code:
[COLOR=blue]DECLARE INTEGER GetActiveWindow  IN Win32API

DECLARE INTEGER GetWindow IN Win32API;
[tab]INTEGER hWnd,;
[tab]INTEGER nType

DECLARE INTEGER SendMessage IN Win32API	;
[tab]INTEGER hwnd,;
[tab]INTEGER uMsg,;
[tab]INTEGER wParam,;
[tab]INTEGER lParam

DECLARE INTEGER GetWindowText IN Win32API ;
[tab]INTEGER hWnd,;
[tab]STRING @cText,;
[tab]INTEGER nType

#DEFINE WM_CLOSE   0x0010

lcTitle = [Adobe Reader]
hNext = GetActiveWindow()[/color][COLOR=green] && Current app's window
* Iterate through the open windows[/color][COLOR=blue]
DO WHILE hNext # 0
[tab]cText = REPLICATE(CHR(0),80)
[tab]GetWindowText(hNext,@cText,80)[/color][COLOR=green]&& Get window title[/color][COLOR=blue]
[tab]IF UPPER(ALLTRIM(lcTitle)) $ UPPER(cText)[/color][COLOR=green]* parameter text is present in window title[/color][COLOR=blue]
[tab][tab]SendMessage(hNext,WM_CLOSE,0,0)
[tab][tab]EXIT
[tab]ENDIF
[tab]hNext = GetWindow(hNext,2) [/color][COLOR=green]&& Next window[/color][COLOR=blue]
ENDDO[/color]

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommandertm.com
PDFcommandertm.co.uk


 
HI

DECLARE INTEGER SendMessage IN Win32API ;
INTEGER hWnd, INTEGER uMsg, INTEGER wParam, ;
INTEGER lParam
DECLARE Long FindWindow in Win32API String, String
DECLARE Long BringWindowToTop in Win32API Long

Local lnHWND,lcTitle
lcTitle = "Calculator" && or My.Exe or whatever
lnHWND = FindWindow(null,m.lcTitle)
IF m.lnHWND >0 &&if calculator is open
BringWindowToTop(m.lnHWND)
ENDIF
*******************
SendMessage(lnHWND,0x0010,0,0) && destroys the calculator
*******************
(When I came with this answer to post, I see Chris has also said the same ahead of me)

Also
DECLARE INTEGER DestroyWindow IN Win32API INTEGER hWnd
DestroyWindow(hWnd) should do it.

:)

____________________________________________
ramani - (Subramanian.G) :)
 

This will remove any running instance of the calculator running.
Code:
terminateProcess('CALC.EXE')
FUNCTION terminateProcess(lcProcess)
lcComputer = "."
loWMIService = Getobject("winmgmts:" ;
    + "{impersonationLevel=impersonate}!\\" + lcComputer + "\root\cimv2")
colProcessList = loWMIService.ExecQuery ;
    ("Select * from Win32_Process")
For Each loProcess In colProcessList
   IF UPPER(loProcess.name) = lcProcess
     loProcess.terminate()
   endif
Next


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Wow, I've been studying news2 and web-links for months trying to figure both the API functions (of Chris and Ramani) and the WMI (of Mike) to accomplish this vital routine.
I also asked the question twice or thrice before on this forum.

MIKE: terminateProcess('CALC.EXE') is relatively unsafe (i.e., data can get lost for VFP data); but it certainly works great: i.e., for cleaning out extra OUTLOOK processes and variable-captioned processes that sometimes accumulated! (Last time I experimented with 'winmgmts' I damaged both the registry and Semantics Anti-V (window's restore points gave not-a-little trouble)) Thanks for this invaluable code.

CHRIS: Your code is SAFE for exiting captioned VFP-Apps and such (i.e., allows cleanup routines)

RAMANI: Your code builds immensely upon Chris's by bringing the window upfront before prompting/closing it. This seems too perfect (for now).

Again, Thanks all for this invaluable code.

Philip
 
pt777

Code:
MIKE: terminateProcess('CALC.EXE') is relatively unsafe (i.e., data can get lost for VFP data); but it certainly works great: i.e., for cleaning out extra OUTLOOK processes and variable-captioned processes that sometimes accumulated!  (Last time I experimented with 'winmgmts' I damaged both the registry and Semantics Anti-V (window's restore points gave not-a-little trouble))  Thanks for this invaluable code.

WMI is definetely not for the inexperienced. It is to be used with caution. Although closing the calculator shouldn't affect a VFP application.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top