关闭EXCEL进程示例
** test.prg
#DEFINE MAX_PATH 260
#DEFINE TH32CS_SNAPPROCESS 0x00000002
#DEFINE PROCESS_ALL_ACCESS 0x001F0FFF
DECLARE LONG CreateToolhelp32Snapshot IN Kernel32 LONG, LONG
DECLARE LONG CloseHandle IN Kernel32 LONG
DECLARE LONG Process32First IN Kernel32 LONG, STRING@
DECLARE LONG Process32Next IN Kernel32 LONG, STRING@
DECLARE LONG OpenProcess IN Kernel32 LONG, LONG, LONG
DECLARE LONG GetExitCodeProcess IN Kernel32 LONG, LONG@
DECLARE LONG TerminateProcess IN Kernel32 LONG, LONG
DO WHILE _TerminateProcess("EXCEL.EXE")
INKEY(0.1)
ENDDO
RETURN
FUNCTION _IsProcessFileName(szFileName, dhProcess)
LOCAL stPE, dhSnapshot, szExeFile, blRet, ddPID, ddRet
* PROCESSENTRY32 结构
stPE = BINTOC(36+MAX_PATH, "4RS") + REPLICATE(0h00, 32+MAX_PATH)
blRet = .F.
dhSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) && 进程快照
ddRet = Process32First(dhSnapshot, @stPE) && 第一个进程
DO WHILE ddRet > 0
szExeFile = RIGHT(stPE, MAX_PATH)
szExeFile = LEFT(szExeFile, AT(0h00, szExeFile)-1)
IF UPPER(szExeFile) == UPPER(szFileName)
ddPID = CTOBIN(SUBSTR(stPE,9,4), "4RS")
dhProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ddPID)
blRet = .T.
EXIT
ENDIF
ddRet = Process32Next(dhSnapshot, @stPE) && 下一个进程
ENDDO
CloseHandle(dhSnapshot)
RETURN blRet
ENDFUNC
FUNCTION _TerminateProcess(szFileName)
LOCAL dhProcess, ddExitCode
dhProcess = 0
ddExitCode = 0
IF _IsProcessFileName(szFileName, @dhProcess)
**GetExitCodeProcess(dhProcess, @ddExitCode)
**RETURN (TerminateProcess(dhProcess, ddExitCode) != 0)
RETURN (TerminateProcess(dhProcess, 0) != 0)
ENDIF
RETURN .F.
ENDFUNC