⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 process_tools.prg

📁 VFP最近技术介绍
💻 PRG
字号:
******************************************************************************************************
** Function Name : Proc[ess]List
** Purpose       : Create a list of running processes
** Description   :
** Parameter(s)  : Array name as string (i.e. "laProcesses"),
**						and does not matter if array will be defined it will be (re)created anyway...
**
**		* Column description of processes array:
**		taProcess[x,1] - Process identifier
**		taProcess[x,2] - Number of execution threads started by the process
**		taProcess[x,3] - Process identifier of the process that created this process (its parent process)
**		taProcess[x,4] - Base priority of any threads created by this process
**		taProcess[x,5] - String that specifies the name of the executable file for the process
**
** Return        : Number of processes founded in memory.
** Side Effect(s): Who knows...
** Notes:        : Created by Lucian Constantin, 2004-03-29
******************************************************************************************************
FUNCTION ProcList(taProcess)
	IF pcount() = 0
		*** Nothing to do
		RETURN 0
	ENDIF

	LOCAL laProcess, hSnapShot, lnBufSize, lcProcInfo, lnResult, lnProcID, lnRow

	laProcess = taProcess

	IF TYPE("&laProcess")="U"
		PUBLIC ARRAY &laProcess[1]
	ELSE
		RELEASE &laProcess
		PUBLIC ARRAY &laProcess[1]
	ENDIF

	*** Declare DLLs needed for the case
	DECLARE INTEGER CreateToolhelp32Snapshot IN kernel32 INTEGER dwFlags, INTEGER th32ProcessID
	DECLARE INTEGER Process32First IN kernel32 INTEGER  hSnapshot, STRING @ lppe
	DECLARE INTEGER Process32Next IN kernel32 INTEGER hSnapshot, STRING @ lppe
	DECLARE INTEGER CloseHandle IN kernel32	INTEGER hObject

	#DEFINE TH32CS_SNAPPROCESS	0x00000002


	*** Prepare structure

	*** Structure used in Process32First() and Process32Next() functions
	*!*	typedef struct tagPROCESSENTRY32 {
	*!*	  DWORD dwSize;					32
	*!*	  DWORD cntUsage;				32
	*!*	  DWORD th32ProcessID;			32
	*!*	  ULONG_PTR th32DefaultHeapID;	32
	*!*	  DWORD th32ModuleID;			32
	*!*	  DWORD cntThreads;				32
	*!*	  DWORD th32ParentProcessID;	32
	*!*	  LONG pcPriClassBase;			32
	*!*	  DWORD dwFlags;				32
	*!*	  TCHAR szExeFile[MAX_PATH];	260+1
	*!*	} PROCESSENTRY32,
	*!*	*PPROCESSENTRY32;
	*** Therefore, 288+ 261 = 549 - make it 550

	*** Obtain a handle to the thread's snapshot
	hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

	lnBufSize = 550
	lcProcInfo= num2dword(lnBufSize) + REPLICATE(CHR(0), lnBufSize -32)

	*** Get the first process
	lnResult = Process32First(hSnapShot, @lcProcInfo)
	lnProcID = 0
	lnRow = 0

	*** Now loop through the rest of processes
	DO WHILE lnResult # 0
		lnRow = lnRow + 1
		DIMENSION &laProcess[lnRow,5]

		*** Extract info
		&laProcess [lnRow,1] = buf2dword(SUBSTR(lcProcInfo,9,4))
		&laProcess [lnRow,2] = buf2dword(SUBSTR(lcProcInfo,21,4))
		&laProcess [lnRow,3] = buf2dword(SUBSTR(lcProcInfo,25,4))
		&laProcess [lnRow,4] = buf2dword(SUBSTR(lcProcInfo,29,4))
		&laProcess [lnRow,5] = STRTRAN(SUBSTR(lcProcInfo,37),CHR(0),"")

		*** Reinit lcProcInfo just "FileName" part so in XP file name does not get cluttered
		***			and in Win9x & ME could do the "Process32Next" corectly
		lcProcInfo= SUBSTR(lcProcInfo,1,36) +REPLICATE(CHR(0), lnBufSize - 36)
		lnResult = Process32Next(hSnapShot, @lcProcInfo)
	ENDDO

	=CloseHandle(hSnapShot)

	RETURN lnRow
ENDFUNC &&* ProcList
******************************************************************************************************************************

******************************************************************************************************************************
** Function Name : Proc[ess] ID
** Purpose       : Finds the Process ID of the program in ?
** Description   :
** Parameter(s)  : Process (EXE file) name as string (i.e. "AcroRd32.EXE")
** Return        : Process ID if found running as LONG INTEGER, 0 otherwise.
** Side Effect(s): Who knows...
** Notes:        : Created by Lucian Constantin, 2004-03-28, with minor editing/enhancements by Ilya Rabyy on 2004-03-29
******************************************************************************************************************************
FUNCTION ProcID(tcProcessName)
	IF pcount() = 0
		*** Nothing to do
		RETURN 0
	ENDIF

	IF TYPE("tcProcessName") # "C"
		RETURN 0
	ENDIF

	LOCAL lcProcessName, lnBufSize, lcProcInfo, lnResult, lnProcID, th32DefaultHeapID
	LOCAL cntUsage, th32ProcessID, th32ModuleID, cntThreads, th32ParentProcessID, pcPriClassBase, dwFlags, szExeFile

	lcProcessName = ALLTRIM(tcProcessName)

	IF EMPTY(lcProcessName)
		RETURN 0
	ENDIF

	*** Declare DLLs needed for the case
	DECLARE INTEGER CreateToolhelp32Snapshot IN Kernel32 INTEGER dwFlags, INTEGER th32ProcessID
	DECLARE INTEGER Process32First IN kernel32 INTEGER hSnapshot, STRING @lpPE
	DECLARE INTEGER Process32Next IN kernel32 INTEGER hSnapshot, STRING @ lpPE
	DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject

	#DEFINE TH32CS_SNAPPROCESS   0x00000002

	*** Structure used in Process32First() and Process32Next() functions
	*!*   typedef struct tagPROCESSENTRY32 {
	*!*     DWORD dwSize;					32
	*!*     DWORD cntUsage;					32
	*!*     DWORD th32ProcessID;			32
	*!*     ULONG_PTR th32DefaultHeapID;	32
	*!*     DWORD th32ModuleID;				32
	*!*     DWORD cntThreads;				32
	*!*     DWORD th32ParentProcessID;		32
	*!*     LONG pcPriClassBase;			32
	*!*     DWORD dwFlags;					32
	*!*     TCHAR szExeFile[MAX_PATH];		260+1
	*!*   } PROCESSENTRY32, *PPROCESSENTRY32;
	*** Therefore, 288+ 261 = 549 - make it 550

	*** Obtain a handle to the thread's snapshot
	hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

	lnBufSize = 550
	lcProcInfo = num2dword(lnBufSize) + REPLICATE(CHR(0), lnBufSize -32)

	*** Get the first process
	lnResult = Process32First(hSnapShot, @lcProcInfo)

	lnProcID = 0
	*** Cycle through the rest of the processes until the one in ? found (or not...)
	DO WHILE lnResult # 0
		*** We will use only the th32ProcessID and szExeFile, but will extract all just in case...
		th32ProcessID = buf2dword(SUBSTR(lcProcInfo, 9, 4))
		szExeFile = STRTRAN(SUBSTR(lcProcInfo,37), CHR(0), "")

		*** Check if we have found the process in ?
		IF ATC(lcProcessName, szExeFile) # 0
			lnProcID = th32ProcessID
			EXIT  && DO WHILE...ENDDO cycle
		ENDIF

		*** Reinit lcProcInfo just "FileName" part so in XP file name does not get cluttered
		***			and in Win9x & ME could do the "Process32Next" corectly
		lcProcInfo= SUBSTR(lcProcInfo,1,36) +REPLICATE(CHR(0), lnBufSize - 36)
		lnResult = Process32Next(hSnapShot, @lcProcInfo)
	ENDDO

	= CloseHandle(hSnapShot)

	CLEAR DLLS CreateToolhelp32Snapshot, Process32First, Process32Next, CloseHandle

	RETURN lnProcID
ENDFUNC		&&ProcID
******************************************************************************************************************************

******************************************************************************************************************************
** Function Name : Kill Proc[ess] ID
** Purpose       : Kills the process with the given ID
** Description   :
** Parameter(s)  : Process ID as LONG INTEGER
** Return        : Integer, non-zero if succeeds, zero otherwise.
** Side Effect(s):
** Notes:        : Created by Lucian Constantin, 2004-03-28, slightly edited by Ilya Rabyy on 2004-03-29
******************************************************************************************************************************
FUNCTION KillProcID(tnProcID)
	IF pcount() = 0
		*** Nothing to do
		RETURN 0
	ENDIF

	IF TYPE('tnProcID') # "N"
		RETURN 0
	ENDIF

	LOCAL lnProcID, hProcess, lnResult

	IF tnProcID = 0
		*** Nothing to do...
		RETURN 0
	ELSE
		lnProcID = tnProcID
	ENDIF

	DECLARE INTEGER OpenProcess IN Kernel32 INTEGER dwDesiredAccess, INTEGER bInheritHandle, INTEGER dwProcId
	DECLARE INTEGER TerminateProcess IN Kernel32 INTEGER hProcess, INTEGER uExitCode
	DECLARE INTEGER CloseHandle IN Kernel32 INTEGER hObject

	#DEFINE PROCESS_TERMINATE 1

	*** Obtain the handle to the process first
	hProcess = OpenProcess(PROCESS_TERMINATE, 0, lnProcID)

	*** If the process found running - "shoot it!"
	lnResult = 0
	IF hProcess # 0
		lnResult = TerminateProcess(hProcess, 0)
	ENDIF

	= CloseHandle(hProcess)

	CLEAR DLLS OpenProcess, TerminateProcess, CloseHandle

	RETURN lnResult
ENDFUNC		&&KillProcID
******************************************************************************************************************************

******************************************************************************************************************************
** Function Name : Kill Proc[ess] N[ame]
** Purpose       : For lazy ones that will find hard to use KillProcID(ProcID(tcProcessName))
** Description   :
** Parameter(s)  : Process (EXE file) name as string (i.e. "AcroRd32.EXE")
** Return        : Integer, non-zero if succeeds, zero otherwise.
** Side Effect(s):
** Notes:        : Created by Lucian Constantin, 2004-03-28
******************************************************************************************************************************
FUNCTION KillProcN(tcProcessName)
	IF pcount() = 0
		*** Nothing to do
		RETURN 0
	ENDIF

	LOCAL lnResult
	lnResult = KillProcID(ProcID(tcProcessName))

	RETURN lnResult
ENDFUNC		&&KillProcID
******************************************************************************************************************************


******************************************************************************************************************************
** Function Name : Buf[fer] 2 DWORD
** Purpose       :
** Description   :
** Parameter(s)  : Buffer as a Char*4 string
** Return        : DWORD type imitation as LONG INTEGER
** Side Effect(s):
** Notes:        :
******************************************************************************************************************************
FUNCTION Buf2DWORD(tcBuffer)

	IF TYPE('tcBuffer') # "C"
		RETURN 0
	ENDIF

	tcBuffer = LEFT(ALLTRIM(tcBuffer), 4)

	LOCAL I, lnRet

	lnRet = ASC(SUBSTR(tcBuffer, 1, 1))
	FOR I = 1 TO 3
		lnRet = lnRet + ASC(SUBSTR(tcBuffer, I + 1, 1)) * 256^I
	NEXT I

	RETURN INT(lnRet)

ENDFUNC		&&Buf2DWORD
******************************************************************************************************************************

******************************************************************************************************************************
** Function Name : Num[eric] 2 DWORD
** Purpose       :
** Description   :
** Parameter(s)  :
** Return        :
** Side Effect(s):
** Notes:        :
******************************************************************************************************************************
FUNCTION Num2DWORD(tnValue)
	#DEFINE m0       256
	#DEFINE m1     65536
	#DEFINE m2  16777216

	LOCAL b0, b1, b2, b3

	b3 = INT(tnValue / m2)
	b2 = INT((tnValue - b3 * m2) / m1)
	b1 = INT((tnValue - b3 * m2 - b2 * m1) / m0)
	b0 = MOD(tnValue, m0)
	RETURN CHR(b0) + CHR(b1) + CHR(b2) + CHR(b3)
ENDFUNC		&&Num2DWORD
******************************************************************************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -