开发者

Foxpro accessing windows running processes and how to end running processes

How do I get a list of the Task manager running processes in Foxpro 9, a开发者_JAVA百科nd how can I kill one of these processes in the list in FoxPro?


Using WMI makes this easy. There's a sample implementation of terminating a process using WIN32_Process at http://www.berezniker.com/content/pages/visual-foxpro/check-if-exe-running-and-optionally-terminate-it which could also easily be adapted to list the processes.


Here's a function to kill all visible instances of a particular program. You need to know its ClassName. I've been able to find ClassNames for common applications (like the Office apps) by searching:

FUNCTION KillApp
*==============================================================================
* Program:              KillApp.PRG
* Purpose:              Close any invisible instances of a specified program
* Author:               Tamar E. Granor
* Last revision:            04/16/02
* Parameters:           tcClassName - the classname of the app to close
* Returns:              Number of instances closed; -1, if parameter problems
* Environment in:       
* Environment out:      Several API functions declared
*==============================================================================
#DEFINE GW_CHILD 5
#DEFINE GW_HWNDNEXT 2
#DEFINE WM_CLOSE 0x10 

LPARAMETERS tcClassName

ASSERT VARTYPE(tcClassName) = "C" AND NOT EMPTY(tcClassName) ;
    MESSAGE "KillApp: Must pass class name of application to kill"

IF VARTYPE(tcClassName) <> "C" OR EMPTY(tcClassName)
    ERROR 11
    RETURN -1
ENDIF

DECLARE LONG GetDesktopWindow IN WIN32API 
DECLARE LONG GetWindow IN WIN32API LONG hWnd, LONG wCmd
DECLARE LONG IsWindowVisible IN WIN32API LONG hWnd
DECLARE LONG GetClassName IN WIN32API LONG hWnd, STRING lpClassName, LONG nMaxCount 
DECLARE LONG PostMessage IN WIN32API LONG hwnd, LONG wMsg, LONG wParam, LONG lParam 

LOCAL lnDesktopHWnd, lnHWnd, lnOldHWnd, lcClass, lnLen, nClosedCount

lnDesktopHWnd = GetDesktopWindow()
lnHWnd = GetWindow( lnDesktopHWnd, GW_CHILD)
lnClosedCount = 0

DO WHILE lnHWnd <> 0
    lcClass = SPACE(256)
    lnLen = GetClassName( lnHWnd, @lcClass, 256)
    lnOldHWnd = lnHWnd
    lnHWnd = GetWindow(lnOldHWnd, GW_HWNDNEXT)
    IF UPPER(LEFT(lcClass, lnLen)) = UPPER(tcClassName)
        lnVisible = IsWindowVisible(lnOldHWnd)
        IF lnVisible = 0
            PostMessage( lnOldHWnd, WM_CLOSE, 0, 0)
            lnClosedCount = lnClosedCount + 1 
        ENDIF 
    ENDIF 
ENDDO

RETURN lnClosedCount

Just realized that a process may not be the same as an application. It looks like the API function for finding processes is EnumProcesses. Check out http://www.news2news.com/vfp/?group=-1&function=246.

Tamar

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜