OLD | NEW |
1 /** | 1 /** |
2 * \file process.h | 2 * \file process.h |
3 */ | 3 */ |
4 | 4 |
5 #ifndef PROCESS_H | 5 #ifndef PROCESS_H |
6 #define PROCESS_H | 6 #define PROCESS_H |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 #include <set> | 9 #include <set> |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 * Copy assignment declared private and not defined. | 73 * Copy assignment declared private and not defined. |
74 * | 74 * |
75 * \par Implementation | 75 * \par Implementation |
76 * Add "= delete" for C++11. | 76 * Add "= delete" for C++11. |
77 */ | 77 */ |
78 Windows_Handle operator=( const Windows_Handle & ) ; | 78 Windows_Handle operator=( const Windows_Handle & ) ; |
79 | 79 |
80 }; | 80 }; |
81 | 81 |
82 //------------------------------------------------------- | 82 //------------------------------------------------------- |
83 // exe_name_set: case-insensitive wide-string set | 83 // file_name_set: case-insensitive wide-string set |
84 //------------------------------------------------------- | 84 //------------------------------------------------------- |
85 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) ; | 85 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) ; |
86 | 86 |
87 struct exe_name | 87 struct file_name |
88 { | 88 { |
89 /** | 89 /** |
90 * Pointer to wide-character string, which is supposed to be null-terminated. | 90 * Pointer to wide-character string, which is supposed to be null-terminated. |
91 */ | 91 */ |
92 const wchar_t * name ; | 92 const wchar_t * name ; |
93 | 93 |
94 exe_name( const wchar_t * name ) : name( name ) {} ; | 94 file_name( const wchar_t * name ) : name( name ) {} ; |
95 } ; | 95 } ; |
96 | 96 |
97 template <> | 97 template <> |
98 struct std::less< exe_name > | 98 struct std::less< file_name > |
99 : std::binary_function< exe_name, exe_name, bool > | 99 : std::binary_function< file_name, file_name, bool > |
100 { | 100 { |
101 bool operator()( const exe_name & a, const exe_name & b ) const | 101 bool operator()( const file_name & a, const file_name & b ) const |
102 { | 102 { |
103 return wcscmpi( a.name, b.name ) < 0 ; | 103 return wcscmpi( a.name, b.name ) < 0 ; |
104 } | 104 } |
105 } ; | 105 } ; |
106 | 106 |
107 struct exe_name_set | 107 struct file_name_set |
108 : public std::set< exe_name > | 108 : public std::set< file_name > |
109 { | 109 { |
110 exe_name_set( const wchar_t * exe_name_list[], size_t n_exe_names ) | 110 file_name_set(){}; |
| 111 |
| 112 file_name_set( const wchar_t * file_name_list[], size_t n_file_names ) |
111 { | 113 { |
112 for ( unsigned int j = 0 ; j < n_exe_names ; ++ j ) | 114 for ( unsigned int j = 0 ; j < n_file_names ; ++ j ) |
113 { | 115 { |
114 insert( exe_name( exe_name_list[ j ] ) ) ; | 116 insert( file_name( file_name_list[ j ] ) ) ; |
115 } | 117 } |
116 } | 118 } |
117 } ; | 119 } ; |
118 | 120 |
119 //------------------------------------------------------- | 121 //------------------------------------------------------- |
120 //------------------------------------------------------- | 122 //------------------------------------------------------- |
121 /** | 123 /** |
122 * Filter by process name. Comparison is case-insensitive. | 124 * Filter by process name. Comparison is case-insensitive. With ABP module loade
d |
123 */ | 125 */ |
124 class process_by_any_exe_name_CI | 126 class process_by_any_exe_with_any_module |
125 : public std::unary_function< PROCESSENTRY32W, bool > | 127 : public std::binary_function< PROCESSENTRY32W, file_name_set, bool > |
126 { | 128 { |
127 const exe_name_set & names ; | 129 const file_name_set & processNames ; |
| 130 const file_name_set & moduleNames; |
128 public: | 131 public: |
129 bool operator()( const PROCESSENTRY32W & ) ; | 132 bool operator()( const PROCESSENTRY32W & ) ; |
130 process_by_any_exe_name_CI( const exe_name_set & names ) | 133 process_by_any_exe_with_any_module( const file_name_set & names, const file_na
me_set & moduleNames ) |
131 : names( names ) | 134 : processNames( names ), moduleNames( moduleNames ) |
132 {} | 135 {} |
133 } ; | 136 } ; |
134 | 137 |
| 138 |
135 //------------------------------------------------------- | 139 //------------------------------------------------------- |
136 // Process utility functions. | 140 // Process utility functions. |
137 //------------------------------------------------------- | 141 //------------------------------------------------------- |
138 /** | 142 /** |
139 * A promiscuous filter admits everything. | 143 * A promiscuous filter admits everything. |
140 */ | 144 */ |
141 struct every_process | 145 struct every_process |
142 : public std::unary_function< PROCESSENTRY32W, bool > | 146 : public std::unary_function< PROCESSENTRY32W, bool > |
143 { | 147 { |
144 bool operator()( const PROCESSENTRY32W & ) { return true ; } ; | 148 bool operator()( const PROCESSENTRY32W & ) { return true ; } ; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 Snapshot() ; | 261 Snapshot() ; |
258 | 262 |
259 /** | 263 /** |
260 * Reconstruct the current instance with a new system snapshot. | 264 * Reconstruct the current instance with a new system snapshot. |
261 */ | 265 */ |
262 void refresh() ; | 266 void refresh() ; |
263 | 267 |
264 /** | 268 /** |
265 * Return a pointer to the first process in the snapshot. | 269 * Return a pointer to the first process in the snapshot. |
266 */ | 270 */ |
267 PROCESSENTRY32W * begin() ; | 271 PROCESSENTRY32W * first() ; |
268 | |
269 /** | |
270 * The end pointer is an alias for the null pointer. | |
271 */ | |
272 inline PROCESSENTRY32W * end() const { return 0 ; } | |
273 | 272 |
274 /** | 273 /** |
275 * Return a pointer to the next process in the snapshot. | 274 * Return a pointer to the next process in the snapshot. |
276 * begin() must have been called first. | 275 * begin() must have been called first. |
277 */ | 276 */ |
278 PROCESSENTRY32W * next() ; | 277 PROCESSENTRY32W * next() ; |
| 278 } ; |
| 279 |
| 280 class ModulesSnapshot |
| 281 { |
| 282 /** |
| 283 * Handle to the process snapshot. |
| 284 */ |
| 285 Windows_Handle handle; |
279 | 286 |
280 /** | 287 /** |
281 * Type definition for pointer to underlying structure. | 288 * Buffer for reading a single module entry out of the snapshot. |
282 */ | 289 */ |
283 typedef PROCESSENTRY32W * Pointer ; | 290 MODULEENTRY32W module; |
284 } ; | 291 |
| 292 /** |
| 293 * Copy constructor declared private and not defined. |
| 294 * |
| 295 * \par Implementation |
| 296 * Add "= delete" for C++11. |
| 297 */ |
| 298 ModulesSnapshot(const ModulesSnapshot&); |
| 299 |
| 300 /** |
| 301 * Copy assignment declared private and not defined. |
| 302 * |
| 303 * \par Implementation |
| 304 * Add "= delete" for C++11. |
| 305 */ |
| 306 ModulesSnapshot operator=(const ModulesSnapshot&); |
| 307 |
| 308 |
| 309 public: |
| 310 /** |
| 311 * Default constructor takes the snapshot. |
| 312 */ |
| 313 ModulesSnapshot(DWORD processId); |
| 314 |
| 315 /** |
| 316 * Return a pointer to the first process in the snapshot. |
| 317 */ |
| 318 MODULEENTRY32W* first(); |
| 319 |
| 320 /** |
| 321 * Return a pointer to the next process in the snapshot. |
| 322 * begin() must have been called first. |
| 323 */ |
| 324 MODULEENTRY32W* next(); |
| 325 }; |
| 326 |
285 | 327 |
286 //------------------------------------------------------- | 328 //------------------------------------------------------- |
287 // initialize_process_list | 329 // initialize_process_list |
288 //------------------------------------------------------- | 330 //------------------------------------------------------- |
289 /** | 331 /** |
290 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 332 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. |
291 * \tparam Admittance Function type for argument 'admit' | 333 * \tparam Admittance Function type for argument 'admit' |
292 * \tparam Extractor Function type for argument 'extract' | 334 * \tparam Extractor Function type for argument 'extract' |
293 * \param admit A unary predicate function class that determines what's included | 335 * \param admit A unary predicate function class that determines what's included |
294 * A process appears in the list only if the predicate returns true. | 336 * A process appears in the list only if the predicate returns true. |
295 * The use of this predicate is analogous to that in std::copy_if. | 337 * The use of this predicate is analogous to that in std::copy_if. |
296 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. | 338 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. |
297 */ | 339 */ |
298 template< class T, class Admittance, class Extractor > | 340 template<class T, class Admittance, class Extractor> |
299 void initialize_process_list( std::vector< T > & v, Snapshot & snap, Admittance
admit = Admittance(), Extractor extract = Extractor() ) | 341 void initialize_process_list(std::vector<T>& v, Snapshot& snap, Admittance admit
= Admittance(), Extractor extract = Extractor()) |
300 { | 342 { |
301 Snapshot::Pointer p = snap.begin() ; | 343 PROCESSENTRY32W* p = snap.first(); |
302 while ( p != snap.end() ) | 344 while (p != NULL) |
303 { | 345 { |
304 if ( admit( * p ) ) | 346 if (admit(*p )) |
305 { | 347 { |
306 /* | 348 /* |
307 * We don't have C++11 emplace_back, which can construct the element in p
lace. | 349 * We don't have C++11 emplace_back, which can construct the element in p
lace. |
308 * Instead, we copy the return value of the converter. | 350 * Instead, we copy the return value of the converter. |
309 */ | 351 */ |
310 v.push_back( extract( * p ) ); | 352 v.push_back(extract(*p)); |
311 } | 353 } |
312 p = snap.next() ; | 354 p = snap.next(); |
313 } | 355 } |
314 } ; | 356 }; |
315 | 357 |
316 //------------------------------------------------------- | 358 //------------------------------------------------------- |
317 // initialize_process_set | 359 // initialize_process_set |
318 //------------------------------------------------------- | 360 //------------------------------------------------------- |
319 /** | 361 /** |
320 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 362 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. |
321 * \tparam Admittance Function type for argument 'admit' | 363 * \tparam Admittance Function type for argument 'admit' |
322 * \tparam Extractor Function type for argument 'extract' | 364 * \tparam Extractor Function type for argument 'extract' |
323 * \param admit A unary predicate function class that determines what's included | 365 * \param admit A unary predicate function class that determines what's included |
324 * A process appears in the list only if the predicate returns true. | 366 * A process appears in the list only if the predicate returns true. |
325 * The use of this predicate is analogous to that in std::copy_if. | 367 * The use of this predicate is analogous to that in std::copy_if. |
326 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. | 368 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. |
327 */ | 369 */ |
328 template< class T, class Admittance, class Extractor > | 370 template<class T, class Admittance, class Extractor> |
329 void initialize_process_set( std::set< T > & set, Snapshot & snap, Admittance ad
mit = Admittance(), Extractor extract = Extractor() ) | 371 void initialize_process_set(std::set< T > & set, Snapshot& snap, Admittance admi
t = Admittance(), Extractor extract = Extractor()) |
330 { | 372 { |
331 Snapshot::Pointer p = snap.begin() ; | 373 PROCESSENTRY32W* p = snap.first(); |
332 while ( p != snap.end() ) | 374 while (p != NULL) |
333 { | 375 { |
334 if ( admit( * p ) ) | 376 if (admit(*p)) |
335 { | 377 { |
336 set.insert( extract( * p ) ); | 378 set.insert(extract(*p)); |
337 } | 379 } |
338 p = snap.next() ; | 380 p = snap.next(); |
339 } | 381 } |
340 } ; | 382 }; |
341 | 383 |
342 //------------------------------------------------------- | 384 //------------------------------------------------------- |
343 // enumerate_windows | 385 // enumerate_windows |
344 //------------------------------------------------------- | 386 //------------------------------------------------------- |
345 | 387 |
346 /** | 388 /** |
347 * States of a window enumeration. | 389 * States of a window enumeration. |
348 */ | 390 */ |
349 typedef enum | 391 typedef enum |
350 { | 392 { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 * Set of process identifiers matching one of the executable names. | 500 * Set of process identifiers matching one of the executable names. |
459 */ | 501 */ |
460 std::set< DWORD > pid_set ; | 502 std::set< DWORD > pid_set ; |
461 | 503 |
462 /** | 504 /** |
463 * Set of executable names by which to filter. | 505 * Set of executable names by which to filter. |
464 * | 506 * |
465 * The argument of the filter constructor is a set by reference. | 507 * The argument of the filter constructor is a set by reference. |
466 * Since it does not make a copy for itself, we define it as a class member to
provide its allocation. | 508 * Since it does not make a copy for itself, we define it as a class member to
provide its allocation. |
467 */ | 509 */ |
468 exe_name_set exe_names ; | 510 file_name_set file_names ; |
| 511 /** |
| 512 * Set of module (DLL) names by which to filter. |
| 513 */ |
| 514 file_name_set module_names ; |
469 | 515 |
470 /** | 516 process_by_any_exe_with_any_module filter ; |
471 * Filter function object matches on any of the exe names specified in the con
structor. | |
472 */ | |
473 process_by_any_exe_name_CI filter ; | |
474 | 517 |
475 /** | 518 /** |
476 * Copy function object copies just the process ID. | 519 * Copy function object copies just the process ID. |
477 */ | 520 */ |
478 copy_PID copy ; | 521 copy_PID copy ; |
479 | 522 |
480 /** | 523 /** |
481 * Snapshot of running processes. | 524 * Snapshot of running processes. |
482 */ | 525 */ |
483 Snapshot & snapshot ; | 526 Snapshot & snapshot ; |
(...skipping 30 matching lines...) Expand all Loading... |
514 if ( ! b ) | 557 if ( ! b ) |
515 { | 558 { |
516 // Assert the process that created the window is not in our pid_set | 559 // Assert the process that created the window is not in our pid_set |
517 return true ; | 560 return true ; |
518 } | 561 } |
519 return f( window ) ; | 562 return f( window ) ; |
520 } | 563 } |
521 } ; | 564 } ; |
522 | 565 |
523 public: | 566 public: |
524 Process_Closer( Snapshot & snapshot, const wchar_t * exe_name_list[], size_t n
_exe_names ) | 567 template <size_t n_file_names, size_t n_module_names> |
525 : snapshot( snapshot ), exe_names( exe_name_list, n_exe_names ), filter( exe
_names ) | 568 Process_Closer(Snapshot & snapshot, const wchar_t* (&file_name_list)[n_file_na
mes], const wchar_t* (&module_name_list)[n_module_names]) |
| 569 : snapshot(snapshot), file_names(file_name_list, n_file_names), module_names
(module_name_list, n_module_names), filter(file_names, module_names) |
526 { | 570 { |
527 update() ; | 571 update() ; |
528 } | 572 } |
| 573 template <size_t n_file_names> |
| 574 Process_Closer(Snapshot & snapshot, const wchar_t * (&file_name_list)[n_file_n
ames]) |
| 575 : snapshot(snapshot), file_names(file_name_list, n_file_names), module_names
(), filter(file_names, module_names) |
| 576 { |
| 577 update() ; |
| 578 } |
529 | 579 |
530 /** | 580 /** |
531 * Refresh our state to match the snapshot state. | 581 * Refresh our state to match the snapshot state. |
532 */ | 582 */ |
533 void refresh() | 583 void refresh() |
534 { | 584 { |
535 pid_set.clear() ; | 585 pid_set.clear() ; |
536 update() ; | 586 update() ; |
537 } | 587 } |
538 | 588 |
539 bool is_running() { return ! pid_set.empty() ; } ; | 589 bool is_running() { return ! pid_set.empty() ; } ; |
540 | 590 |
541 bool contains( DWORD pid ) const { return pid_set.find( pid ) != pid_set.end()
; } ; | 591 bool contains( DWORD pid ) const { return pid_set.find( pid ) != pid_set.end()
; } ; |
542 | 592 |
543 template< class F > | 593 template< class F > |
544 bool iterate_our_windows( F f ) | 594 bool iterate_our_windows( F f ) |
545 { | 595 { |
546 only_our_processes< F > g( * this, f ) ; | 596 only_our_processes< F > g( * this, f ) ; |
547 return enumerate_windows( g ) ; | 597 return enumerate_windows( g ) ; |
548 } | 598 } |
549 | 599 |
550 /* | 600 /* |
551 * Shut down every process in the pid_set. | 601 * Shut down every process in the pid_set. |
552 */ | 602 */ |
553 bool shut_down() ; | 603 bool shut_down() ; |
554 | 604 |
555 } ; | 605 } ; |
556 | 606 |
557 #endif | 607 #endif |
OLD | NEW |