Thursday, July 26, 2018

3 Ways to Terminate, Close or Kill a running Executable Application in Visual Foxpro at run-time



How to terminate a running executable application in Visual FoxPro 9.0 at run-time by force.



1. You can use WMI, for example close all Calculator instances:

oWMI = GETOBJECT('winmgmts://')
cQuery = "select * from win32_process where name='calc.exe'"
oResult = oWMI.ExecQuery(cQuery)
FOR EACH oProcess IN oResult
   ? oProcess.Name
   oProcess.Terminate(0)
NEXT

2. Use TaskKill Command.

Use the RUN and Taskkill command. RUN is a Visual FoxPro command that allows you to execute DOS commands, Taskkill, on the other hand, is DOS command that allows you to terminate an exe applications and processes , similar to using the task manager.

private anyvariable
anyvariable=MESSAGEBOX(“Are you sure you want to terminate Ms-Word?”,4+32)
if anyvariable=6
   RUN "Taskkill /f /im Winword.exe”
endif

3. Kill another app programmatically using some api.

IF KillApp("Document1 - Microsoft Word")  && replace w/ the title of the app that you want to close
    ?"app killed"
ELSE
    ?"app still alive..."
ENDIF

FUNCTION KillApp
  LPARAMETER tcCaption
  LOCAL lnWinHandle, lnRetval, lnResult, llRetval
  DECLARE INTEGER WaitForSingleObject IN Win32API ;
  INTEGER hHandle, ;
  INTEGER dwMilliseconds

  DECLARE INTEGER FindWindow IN Win32API ;
  STRING lpClassName, ;
  STRING lpWindowName

  DECLARE INTEGER PostMessage IN Win32API ;
  INTEGER hwnd, ;
  INTEGER wMsg, ;
  INTEGER wParam, ;
  INTEGER lParam

  DECLARE INTEGER IsWindow IN Win32API ;
  INTEGER hwnd

  #DEFINE WM_CLOSE 16 && H10
  #DEFINE INFINITE 4294967295 && HFFFFFFFF

  lnWinHandle = FindWindow(0, tcCaption)
  lnRetval = PostMessage(lnWinHandle, WM_CLOSE, 0, 0)
  lnResult = WaitForSingleObject(lnWinHandle, INFINITE)
  llRetval = (IsWindow(lnWinHandle) = 0)

  RETURN llRetval
ENDFUNC

No comments:

Post a Comment