Vista Posteos

CFindKillProcess
Readersmay often encounter such a problem ,think to some operation ,found the file is being used by other programs ,in an open state ,and is exclusive to open ,then is not operation file .
Therefore ,in order to operate these files ,open the file the process must be removed .How to clean the scavenging process ?In fact ,in the Windows2000 operating system version has a program called tskill.
exe ,it can be used to clear out a process in ,abercrombie france,enter program name after you can clear the running example .But how to code in tskill realize the function of the how to do ?Aiming at this problem ,the example introduced in the Windows2000 realizing method .
One ,the realization method in Visual C++ programming, most secure kill process method is to run the program to send the WM_CLOSE message ,the code is as follows :HWND hWnd =this.m_hWnd ;/ / get the main window PostMessage ( hWnd ,WM_CLOSE ,0 ,abercrombie,0) ;send this message, usually should wait until the process is terminated ,when the process terminates ,it sends status signals ,and WaitForSingleObject returned WAIT_OBJECT_0 .
If the return of other values ,process or hang out ,either still in processing .In this case ,kill the process is the only way to use the more powerful API function :TerminateProcess ( ) .
If you want to do a bit more beautiful ,can be in a closed prior to the main window to send a WM_QUERYENDSESSION message ,when the user end session ( log out ) or call ExitWindows ( ) function, the application will receive this message, then prepare to withdraw from the process ,abercrombie france,the general will pop up a dialog box ,telling the user : program to rolled out ,if you want to save changes things ,now is the best time ,want to save ? There are three to choose ( Yes/No/Cancel ) .
In addition ,sending the WM_QUERYENDSESSION message can refuse to launch process ( press the Cancel ) ,and if so ,www.monclerfrancesmagasin5s.com,the process will continue .If you want to close the process is suspended ,the use of SendMessageTimeout ( ) function is very important ,but not with SendMessage ( ) function ,the parameter SMTO_NOTIMEOUTIFNOTHUNG is one of only 2000 and Windows Windows XP only sign .
Its meaning is if the thread does not hang ,not time-out ,in other words if the thread is normally processing ,then wait forever ,so the user can see the dialog and decide what to do ,when the user finally made the decision after SendMessageTimeout ( ) ,with the corresponding bOKToKill value return .
In this case in order to improve the reusability of the code ,the implementation details are encapsulated in a CFindKillProcess class, including search and kill the process ,please see EnumProc.
h and EnumProc.cpp files .File another two reusable classes ,one is CProcessIterator ,another is CWindowIterator .This in practice acquisition process of the main window and the process of creating the program name has been described in detail .
The CfindKillProcess member function of class FindProcess ( ) a search into the program ,if you find this process ,it returns the process ID ,then the ID to CFindKillProcess: :KillProcess ( ) function ,KillProcess ( ) function encapsulates the closed window and termination logic ,jordan,it uses the CmainWindowIterator class object to enumerate the process of the main window ( may be more than one ,see obtain a process of the main window and the process of creating the program name ? ) ,and send WM_CLOSE to every window ,and then wait for the process to death .
It has a Boolean parameter is used to indicate when an application process to quit when whether to execute the TerminateProcess ( ) function ,www.abercrombiefrancele.com.For details please see download code .Two ,1 ,Visual C++6.
0 programming steps starting ,generating a console application ,the program named KP ;2 ,in the program code to add CfindKillProcess ,CProcessIterator class definition ;3 ,add the code ,compile and run the application .
In three ,the program code ////////////////////////////////////////////////////// ////////////////// / / Process iterator #pragma once - iterator over all system processes / / Always skips the first ( IDLE ) process with PID=0.
class CProcessIterator {protected: DWORD* m_pids ;/ / array of procssor IDs DWORD m_count ;/ / size of array DWORD m_current ;/ / next array item public: CProcessIterator ( ) ;~CProcessIterator ( DWORD ) ;First ( ) ;DWORD Next ( ) {return m_pids &amp ;&amp ,air jordan pas cher;m_current &lt ;m_count ? :0 ;} DWORD GetCount ( ) {return m_count ;} } ;/ / Handy class to facilitate ////////////////// finding and killing a process by name.
class CFindKillProcess {public: CFindKillProcess ( ) ;~CFindKillProcess ( FindProcess ( DWORD ) ;LPCTSTR lpModname ,BOOL bAddExe=TRUE ) ;BOOL KillProcess ( DWORD PID ,BOOL bZap ) ;} ;///////////////////////////////////////// ///////////////////// ////// #include stdafx.
h #include EnumProc.h / / CProcessIterator all processes CProcessIterator: :CProcessIterator Iterates ( ) {m_pids = NULL ,abercrombie and fitch;} CProcessIterator: { delete :~CProcessIterator ( m_pids ) ;} / / Get first process: /////// /////////// Call EnumProcesses to init array.
Return first one. DWORD CProcessIterator: :First ( ) {m_current = ( DWORD ) -1 ;m_count = 0 ;DWORD nalloc = 1024 ,doudoune moncler;do {delete m_pids m_pids = ;new DWORD ;if ( EnumProcesses ( m_pids ,nalloc*sizeof ( DWORD ) ,moncler,&amp ;m_count ) { m_count sizeof ( DWORD ) /= ) ;m_current = 1 ;/ / skip IDLE process} } while ( nalloc &lt = m_count ;) ;return Next ( ) ;} / / CFindKillProcess to ////// ////////////////////////////////////////////////////////// - find/kill a process by module name.
CFindKillProcess ( ) {} / / CFindKillProcess: :CFindKillProcess: :~CFindKillProcess ( ) {} / / Search for process whose module ////////////////// name matches parameter. / / Find S foo or foo.
exe DWORD CFindKillProcess: :FindProcess ( LPCTSTR modname ,BOOL bAddExe ) {CProcessIterator ITP ;for ( DWORD pid=itp.First ( PID ) pid=itp.Next ( ;;) ) {TCHAR ;CProcessModuleIterator ITM ( PID ) ;HMODULE hModule = itm.
First ( ) ;/ /.EXE if ( hModule ) {GetModuleBaseName ( itm.GetProcessHandle ( ) ,hModule ,name ,_MAX_PATH ) ;string sModName = modname ;if ( strcmpi ( sModName.c_str ) ,( name ) ==0 ) return PID ;sModName = .
Exe ;if ( bAddExe &amp ;&amp ;strcmpi ( sModName.c_str ) ,( name ) ==0 ) return PID return 0 ;} } ;} / / Kill ////////////////// a process cleanly: Close main windows and wait.
/ / bZap=TRUE to force kill. BOOL CFindKillProcess: :KillProcess ( DWORD PID ,BOOL bZap ) {CMainWindowIterator ITW ( PID ) ;for ( HWND hwnd=itw.First ( hWnd ) hwnd=itw.Next ( ;;) ) {DWORD bOKToKill = FALSE ;SendMessageTimeout ( hWnd ,WM_QUERYENDSESSION ,0 ,0 ,SMTO_ABORT IFHUNGSMTO_NOTIMEOUTIFNOTHUNG ,100 ,&amp ;bOKToKill ) ;if ( bOKToKill return FALSE !) ;/ / window doesn want to die: abort PostMessage ( hWnd ,air jordan,WM_CLOSE ,0 ,0) ;} / / I closed the main windows ;now wait for process to die.
BOOL bKilled = TRUE ;HANDLE hp=OpenProcess ( SYNCHRONIZEPROCESS_TERMINATE ,abercrombie,FALSE ,PID ) ;if ( HP ) {if ( WaitForSingleObject ( HP ,5000) != WAIT_OBJECT_0 ) {if ( bZap ) {/ / didn die: force kill it if zap requested TerminateProcess ( HP ,0) ;} else {bKilled = FALSE ;} } CloseHandle ( HP ) ;} return bKilled #include stdafx.
h ;} ////////////////////////////////////////////////////////////////////////// # include EnumProc.h #define TPF _tprintf / / to save typing typedef list&lt ;string&gt ;CStringList ;/ / like MFC ,but with STL / / pre-declare functions int help ( check ) ;/ / for switch: / or inline BOOL isswitch ( TCHAR C ) {return c==L c==L ;} int main ( int argc ,TCH AR* ,TCHAR* ) {CStringList cmdargs ;/ / command-line args ( processes to kill ) BOOL bDisplayOnly=FALSE ;/ / don kill ,just show results BOOL bQuiet=FALSE ;/ / suppress error messages BOOL bZap=FALSE ,air jordan;/ / force-kill process / / Parse command line.
Switches can come in any order,air jordan. for ( int i=1 ;i&lt ;argc ;i++ ) {if ( isswitch ( ) ) {for ( UINT j=1 ;j&lt ;strlen ( ) ;j++ ) {switch ( tolower ( ) ) {case ? help ( 0 ) :return ;case ; :bDisplayOnly=TRUE ,abercrombie and fitch;break ;case bQuiet=TRUE ;break ;case : :bZap=TRUE ;break ;default: return help ( ) ;} } } else {cmdargs.
push_back ( ) ;/ / got a non-switch arg: add to list} } if ( cmdargs.size ) ( &lt ;=0 ) help ( ) ;/ / Now iterate args ( module names ) ,killing each one CStringList: :iterator it ,abercrombie paris;for ( it=cmdargs.
begin ( =cm ) ;it !Dargs.end ( ) ;it++ ) {CFindKillProcess FKP ;DWORD PID = fkp.FindProcess ( it-&gt ) ;c_str ( ) ;if ( PID ) {if ( bDisplayOnly ) {TPF ( _T ( Kill process %d ( 0xbx ) n ) ,PID ,PID ) ;} else {fkp.
KillProcess ( PID ,bZap ) ;} } else if ( !BQuiet ) {TPF ( _T ( Error: Can find process .n ,it-&gt ) ;c_str ( ) ) ;} } return 0 ;} int ( help ) {TPF ( _T ( kp: Kill process from command line.
n. ) ) ;TPF ( _T ( Copyright 2002 Paul DiLascia.nn ) ( _T ) ;TPF ( KP modname1 ? ... ) ) ;TPF ( _T ( where modnameN is a module name ;eg foo or foo.exen.
) ) ,abercrombie france;TPF ( _T ( n ) ) ;TPF ( _T ( /n ( othing ) don kill ,just show resultsn ) ) ;TPF ( _T ( /q ( uiet ) don show errorsn,abercrombie. ) ) ;TPF ( _T ( /z ( AP ) force kill ( ignore WM_QUERYENDSESSION ) n.
) ) ;TPF ( _T ( n ) ) ;return 0 ;} four ,www.airjordanupasachere.com,summary of the case through the introduction of class CfindKillProcess study on Windows2000 thorough elimination process method ,although the procedure only In Windows2000 compiler environment to run ,but this method in Windows98 process control also has draw lessons from a meaning ,abercrombie france.
Related articles: To further understand demand, through sufficient investigation and study, hold to measurable dimensions to layout, layout, dislocation, complete and perfect, the principle of sustainable development, in policy and planning on the scientific layout personnel housing construction.
tkcacama 08.12.2012 0 137
Publicidad

Bloque HTML
Comentarios
Ordenar por: 
Por página:
 
  • Aún no hay comentarios
Información de Entrada
08.12.2012 (4360 días)
Publicidad

 

 

 

Calificar
0 votos
Recomendar
Acciones
Categorías
Dating and Personals (1 publicaciones)
Food Blogs (1 publicaciones)
Tech News (1 publicaciones)
Videos (2 publicaciones)