  | 
				VFP 愛用者社區 本討論區為 Visual Foxpro 愛用者經驗交流的地方, 請多多利用"搜尋"的功能, 先查看看有無前例可循, 如果還有不懂的再發問. 部份主題有附加檔案, 須先註冊成為社區居民才可以下載.   
				 | 
			 
		 
		 
	
		| 上一篇主題 :: 下一篇主題   | 
	 
	
	
		| 發表人 | 
		內容 | 
	 
	
		蔡文華
 
 
  註冊時間: 2005-10-31 文章: 118
 
  第 1 樓
  | 
		
			
				 發表於: 星期二 八月 18, 2009 10:33 am    文章主題: 如何在vfp裡強迫退出被 windows 已執行的系統!! | 
				     | 
			 
			
				
  | 
			 
			
				1. 執行 a.exe
 
2. 執行 b.exe 
 
    我想要在 b 程式中強迫已被執行的 a.exe
 
請問有相關做法嗎? | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		ckp6250
 
 
  註冊時間: 2004-07-30 文章: 1645
 
  第 2 樓
  | 
		
			
				 發表於: 星期二 八月 18, 2009 8:24 pm    文章主題:  | 
				     | 
			 
			
				
  | 
			 
			
				lnResult = ProcList("laProc")
 
Create Cursor cProc (ProcID N(10), ThreadCnt N(10), ParentPID N(10), Priority N(3), FileName C(60))
 
Append From Array laProc
 
Select Iif(Upper(Trim(FileName)) $ "a.exeE,b.exe","zzzzzzzzzzzzz",Upper(FileName)) , * ;
 
		From cProc ;
 
		Order By 1 ;
 
		INTO Cursor cProc
 
					
 
	SCAN FOR ! Upper(Trim(FileName)) $ "a.exe,b.exe"
 
		vExe = Upper(Trim(FileName))
 
		lnProcID = ProcID(vExe)
 
		lnResult = KillProcID(lnProcID)
 
		lnResult = KillProcN(vExe)
 
	ENDSCAN
 
 
 
 
******************************************************************************************************
 
** 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
 
****************************************************************************************************************************** | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		蔡文華
 
 
  註冊時間: 2005-10-31 文章: 118
 
  第 3 樓
  | 
		
			
				 發表於: 星期四 八月 20, 2009 3:09 pm    文章主題: 使用以上大大提供的程式,雖然可以順利的將指定的程式在WINDOWS強迫關閉..但又延伸出其它問題了 | 
				     | 
			 
			
				
  | 
			 
			
				我在加入以上程式後..經過編譯後,本來可以執行的下面片段程式,變成無法執行而且會出現錯誤訊息 
 
DECLARE INTEGER SetForegroundWindow IN USER32 INTEGER
 
DECLARE INTEGER SHOWWINDOW IN USER32 INTEGER, INTEGER
 
DECLARE INTEGER FindWindow IN USER32 STRING @ , STRING @
 
lnHWND=FindWindow(0,thisform.Caption)
 
if lnHWND>0
 
  ShowWindow(lnHWND,9)  && SW_RESTORE --> 這裡會出錯
 
  SetForegroundWindow(lnHWND)
 
endif
 
 
*****************************************************
 
錯誤訊息為 
 
 
    在DLL中找不到進入點SHOWWINDOW
 
 
因為原本是可以執行的..但加入這些函式後,就無法執行,且我重新開機
 
仍無法還原,然後我拿掉這裡加入的函式也無法回到最原始可以執行的
 
狀態....真不知怎麼辦?? | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		ckp6250
 
 
  註冊時間: 2004-07-30 文章: 1645
 
  第 4 樓
  | 
		
			
				 發表於: 星期四 八月 20, 2009 4:13 pm    文章主題:  | 
				     | 
			 
			
				
  | 
			 
			
				如果,您的第二及第三行,改成這樣呢
 
 
DECLARE INTEGER ShowWindow IN user32 INTEGER hwnd,  INTEGER nCmdShow
 
 
DECLARE INTEGER FindWindow IN user32  STRING lpClassName,  STRING lpWindowName | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		CCB2000
 
 
  註冊時間: 2009-03-25 文章: 97
 
  第 5 樓
  | 
		
			
				 發表於: 星期六 八月 22, 2009 1:16 am    文章主題:  | 
				     | 
			 
			
				
  | 
			 
			
				WIN32 API 函數名稱是區分大小寫的:
 
SHOWWINDOW <> ShowWindow
 
 
DECLARE integer "ShowWindow" IN win32api AS "Win_ShowWindow" ;
 
   integer hwnd,;
 
   integer wcmdshow _________________ VFP C++編譯軟件 (VFP C++ Compiler): http://www.baiyujia.com/vfpcompiler | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		goto-dream
 
 
  註冊時間: 2004-05-11 文章: 909
 
  第 6 樓
  | 
		
			
				 發表於: 星期五 十月 02, 2009 3:51 pm    文章主題:  | 
				     | 
			 
			
				
  | 
			 
			
				 	  | CCB2000 寫到: | 	 		  WIN32 API 函數名稱是區分大小寫的:
 
SHOWWINDOW <> ShowWindow
 
 
DECLARE integer "ShowWindow" IN win32api AS "Win_ShowWindow" ;
 
   integer hwnd,;
 
   integer wcmdshow | 	  
 
 
感謝 CCB2000
 
 
DLL原來是有區分大小問題
 
我還以為沒有 註冊耶 _________________ 福隆昌淨水有限公司--淨水器的專家,淨水器,飲水機,濾心!!
 
 
想了解更多,您可上幸福雞湯組.找尋!!丁澐瑄.老師.
 
 
          愛作夢 | 
			 
		  | 
	 
	
		| 回頂端 | 
		 | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
  	 
	    
  	   | 
 	
您 無法 在這個版面發表文章 您 無法 在這個版面回覆文章 您 無法 在這個版面編輯文章 您 無法 在這個版面刪除文章 您 無法 在這個版面進行投票 您 無法 在這個版面附加檔案 您 無法 在這個版面下載檔案
  | 
   
  
		 |