| 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 "installer-lib.h" | 8 #include "installer-lib.h" |
| 9 #include "handle.h" | 9 #include "handle.h" |
| 10 | 10 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 * Filter by process name. Comparison is case-insensitive. Windows Store app pro
cesses excluded | 95 * Filter by process name. Comparison is case-insensitive. Windows Store app pro
cesses excluded |
| 96 */ | 96 */ |
| 97 class process_by_any_exe_not_immersive | 97 class process_by_any_exe_not_immersive |
| 98 : public std::unary_function<PROCESSENTRY32W, bool> | 98 : public std::unary_function<PROCESSENTRY32W, bool> |
| 99 { | 99 { |
| 100 /** | 100 /** |
| 101 * Set of file names from which to match candidate process names. | 101 * Set of file names from which to match candidate process names. |
| 102 * | 102 * |
| 103 * This is a reference to, not a copy of, the set. | 103 * This is a reference to, not a copy of, the set. |
| 104 * The lifetime of this object must be subordinate to that of its referent. | 104 * The lifetime of this object must be subordinate to that of its referent. |
| 105 * The set used to instantiate this class is a member of Process_Closer, | 105 * The set used to instantiate this class is a member of ProcessCloser, |
| 106 * and so also is this class. | 106 * and so also is this class. |
| 107 * Hence the lifetimes are coterminous, and the reference is not problematic. | 107 * Hence the lifetimes are coterminous, and the reference is not problematic. |
| 108 */ | 108 */ |
| 109 const file_name_set & processNames; | 109 const file_name_set & processNames; |
| 110 public: | 110 public: |
| 111 bool operator()( const PROCESSENTRY32W & ); | 111 bool operator()( const PROCESSENTRY32W & ); |
| 112 process_by_any_exe_not_immersive(const file_name_set & names) : processNames(
names ) {} | 112 process_by_any_exe_not_immersive(const file_name_set & names) : processNames(
names ) {} |
| 113 } ; | 113 } ; |
| 114 | 114 |
| 115 | 115 |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 buffer.dwSize = sizeof( result_type ) ; | 320 buffer.dwSize = sizeof( result_type ) ; |
| 321 } | 321 } |
| 322 | 322 |
| 323 public: | 323 public: |
| 324 /** | 324 /** |
| 325 * Reconstruct the current instance with a new system snapshot. | 325 * Reconstruct the current instance with a new system snapshot. |
| 326 * | 326 * |
| 327 * This function uses reinitialization assignment in the Windows_Handle class, | 327 * This function uses reinitialization assignment in the Windows_Handle class, |
| 328 * which takes care of closing the old handle. | 328 * which takes care of closing the old handle. |
| 329 */ | 329 */ |
| 330 void refresh() | 330 void Refresh() |
| 331 { | 331 { |
| 332 handle = make_handle(); | 332 handle = make_handle(); |
| 333 } | 333 } |
| 334 | 334 |
| 335 /** | 335 /** |
| 336 * Retrieve the first snapshot item into our member buffer. | 336 * Retrieve the first snapshot item into our member buffer. |
| 337 * | 337 * |
| 338 * \return | 338 * \return |
| 339 * Pointer to our member buffer if there was a first item | 339 * Pointer to our member buffer if there was a first item |
| 340 * 0 otherwise | 340 * 0 otherwise |
| (...skipping 25 matching lines...) Expand all Loading... |
| 366 */ | 366 */ |
| 367 const result_type * next() | 367 const result_type * next() |
| 368 { | 368 { |
| 369 return Traits::next(handle, &buffer) ? &buffer : 0; | 369 return Traits::next(handle, &buffer) ? &buffer : 0; |
| 370 } | 370 } |
| 371 } ; | 371 } ; |
| 372 | 372 |
| 373 /** | 373 /** |
| 374 * A snapshot of all processes running on the system. | 374 * A snapshot of all processes running on the system. |
| 375 */ | 375 */ |
| 376 struct Process_Snapshot | 376 struct ProcessSnapshot |
| 377 : public Snapshot< Process_Snapshot_Traits > | 377 : public Snapshot< Process_Snapshot_Traits > |
| 378 { | 378 { |
| 379 Process_Snapshot() | 379 ProcessSnapshot() |
| 380 : Snapshot( 0 ) | 380 : Snapshot( 0 ) |
| 381 {} | 381 {} |
| 382 } ; | 382 } ; |
| 383 | 383 |
| 384 /** | 384 /** |
| 385 * A snapshot of all modules loaded for a given process. | 385 * A snapshot of all modules loaded for a given process. |
| 386 */ | 386 */ |
| 387 struct Module_Snapshot | 387 struct Module_Snapshot |
| 388 : public Snapshot< Module_Snapshot_Traits > | 388 : public Snapshot< Module_Snapshot_Traits > |
| 389 { | 389 { |
| 390 Module_Snapshot( DWORD process_id ) | 390 Module_Snapshot( DWORD process_id ) |
| 391 : Snapshot( process_id ) | 391 : Snapshot( process_id ) |
| 392 {} | 392 {} |
| 393 } ; | 393 } ; |
| 394 | 394 |
| 395 //------------------------------------------------------- | 395 //------------------------------------------------------- |
| 396 // initialize_process_list | 396 // initialize_process_list |
| 397 //------------------------------------------------------- | 397 //------------------------------------------------------- |
| 398 /** | 398 /** |
| 399 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 399 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. |
| 400 * \tparam Admittance Function type for argument 'admit' | 400 * \tparam Admittance Function type for argument 'admit' |
| 401 * \tparam Extractor Function type for argument 'extract' | 401 * \tparam Extractor Function type for argument 'extract' |
| 402 * \param admit A unary predicate function class that determines what's included | 402 * \param admit A unary predicate function class that determines what's included |
| 403 * A process appears in the list only if the predicate returns true. | 403 * A process appears in the list only if the predicate returns true. |
| 404 * The use of this predicate is analogous to that in std::copy_if. | 404 * The use of this predicate is analogous to that in std::copy_if. |
| 405 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. | 405 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. |
| 406 */ | 406 */ |
| 407 template<class T, class Admittance, class Extractor> | 407 template<class T, class Admittance, class Extractor> |
| 408 void initialize_process_list(std::vector<T>& v, Process_Snapshot &snap, Admittan
ce admit = Admittance(), Extractor extract = Extractor()) | 408 void initialize_process_list(std::vector<T>& v, ProcessSnapshot &snap, Admittanc
e admit = Admittance(), Extractor extract = Extractor()) |
| 409 { | 409 { |
| 410 const PROCESSENTRY32W* p = snap.first(); | 410 const PROCESSENTRY32W* p = snap.first(); |
| 411 while (p != 0) | 411 while (p != 0) |
| 412 { | 412 { |
| 413 if (admit(*p )) | 413 if (admit(*p )) |
| 414 { | 414 { |
| 415 /* | 415 /* |
| 416 * We don't have C++11 emplace_back, which can construct the element in p
lace. | 416 * We don't have C++11 emplace_back, which can construct the element in p
lace. |
| 417 * Instead, we copy the return value of the converter. | 417 * Instead, we copy the return value of the converter. |
| 418 */ | 418 */ |
| 419 v.push_back(extract(*p)); | 419 v.push_back(extract(*p)); |
| 420 } | 420 } |
| 421 p = snap.next(); | 421 p = snap.next(); |
| 422 } | 422 } |
| 423 }; | 423 }; |
| 424 | 424 |
| 425 //------------------------------------------------------- | 425 //------------------------------------------------------- |
| 426 // initialize_process_set | 426 // initialize_process_set |
| 427 //------------------------------------------------------- | 427 //------------------------------------------------------- |
| 428 /** | 428 /** |
| 429 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 429 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. |
| 430 * \tparam Admittance Function type for argument 'admit' | 430 * \tparam Admittance Function type for argument 'admit' |
| 431 * \tparam Extractor Function type for argument 'extract' | 431 * \tparam Extractor Function type for argument 'extract' |
| 432 * \param admit A unary predicate function class that determines what's included | 432 * \param admit A unary predicate function class that determines what's included |
| 433 * A process appears in the list only if the predicate returns true. | 433 * A process appears in the list only if the predicate returns true. |
| 434 * The use of this predicate is analogous to that in std::copy_if. | 434 * The use of this predicate is analogous to that in std::copy_if. |
| 435 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. | 435 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar
gument and returns an element of type T. |
| 436 */ | 436 */ |
| 437 template<class T, class Admittance, class Extractor> | 437 template<class T, class Admittance, class Extractor> |
| 438 void initialize_process_set(std::set< T > & set, Process_Snapshot &snap, Admitta
nce admit = Admittance(), Extractor extract = Extractor()) | 438 void initialize_process_set(std::set< T > & set, ProcessSnapshot &snap, Admittan
ce admit = Admittance(), Extractor extract = Extractor()) |
| 439 { | 439 { |
| 440 const PROCESSENTRY32W* p = snap.first(); | 440 const PROCESSENTRY32W* p = snap.first(); |
| 441 while (p != 0) | 441 while (p != 0) |
| 442 { | 442 { |
| 443 if (admit(*p)) | 443 if (admit(*p)) |
| 444 { | 444 { |
| 445 set.insert(extract(*p)); | 445 set.insert(extract(*p)); |
| 446 } | 446 } |
| 447 p = snap.next(); | 447 p = snap.next(); |
| 448 } | 448 } |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 { | 581 { |
| 582 // Assert EnumWindows failed | 582 // Assert EnumWindows failed |
| 583 data.status = error ; | 583 data.status = error ; |
| 584 r = false ; | 584 r = false ; |
| 585 } | 585 } |
| 586 } | 586 } |
| 587 return r ; | 587 return r ; |
| 588 } | 588 } |
| 589 | 589 |
| 590 //------------------------------------------------------- | 590 //------------------------------------------------------- |
| 591 // Process_Closer | 591 // ProcessCloser |
| 592 //------------------------------------------------------- | 592 //------------------------------------------------------- |
| 593 class Process_Closer | 593 class ProcessCloser |
| 594 { | 594 { |
| 595 /** | 595 /** |
| 596 * Set of process identifiers matching one of the executable names. | 596 * Set of process identifiers matching one of the executable names. |
| 597 */ | 597 */ |
| 598 std::set< DWORD > pid_set ; | 598 std::set< DWORD > pid_set ; |
| 599 | 599 |
| 600 /** | 600 /** |
| 601 * Set of executable names by which to filter. | 601 * Set of executable names by which to filter. |
| 602 * | 602 * |
| 603 * The argument of the filter constructor is a set by reference. | 603 * The argument of the filter constructor is a set by reference. |
| 604 * Since it does not make a copy for itself, we define it as a class member to
provide its allocation. | 604 * Since it does not make a copy for itself, we define it as a class member to
provide its allocation. |
| 605 */ | 605 */ |
| 606 file_name_set process_names ; | 606 file_name_set process_names ; |
| 607 | 607 |
| 608 process_by_any_exe_not_immersive filter ; | 608 process_by_any_exe_not_immersive filter ; |
| 609 | 609 |
| 610 /** | 610 /** |
| 611 * Copy function object copies just the process ID. | 611 * Copy function object copies just the process ID. |
| 612 */ | 612 */ |
| 613 copy_PID copy ; | 613 copy_PID copy ; |
| 614 | 614 |
| 615 /** | 615 /** |
| 616 * Snapshot of running processes. | 616 * Snapshot of running processes. |
| 617 */ | 617 */ |
| 618 Process_Snapshot & snapshot ; | 618 ProcessSnapshot & snapshot ; |
| 619 | 619 |
| 620 void update() | 620 void update() |
| 621 { | 621 { |
| 622 initialize_process_set( pid_set, snapshot, filter, copy ) ; | 622 initialize_process_set( pid_set, snapshot, filter, copy ) ; |
| 623 } ; | 623 } ; |
| 624 | 624 |
| 625 template< class F > | 625 template< class F > |
| 626 class only_our_processes | 626 class only_our_processes |
| 627 { | 627 { |
| 628 Process_Closer & self ; | 628 ProcessCloser & self ; |
| 629 | 629 |
| 630 F f ; | 630 F f ; |
| 631 | 631 |
| 632 public: | 632 public: |
| 633 only_our_processes( Process_Closer & self, F f ) | 633 only_our_processes( ProcessCloser & self, F f ) |
| 634 : f( f ), self( self ) | 634 : f( f ), self( self ) |
| 635 {} | 635 {} |
| 636 | 636 |
| 637 bool operator()( HWND window ) | 637 bool operator()( HWND window ) |
| 638 { | 638 { |
| 639 bool b ; | 639 bool b ; |
| 640 try | 640 try |
| 641 { | 641 { |
| 642 b = self.contains( creator_process( window ) ) ; | 642 b = self.contains( creator_process( window ) ) ; |
| 643 } | 643 } |
| 644 catch ( ... ) | 644 catch ( ... ) |
| 645 { | 645 { |
| 646 // ignore window handles that are no longer valid | 646 // ignore window handles that are no longer valid |
| 647 return true ; | 647 return true ; |
| 648 } | 648 } |
| 649 if ( ! b ) | 649 if ( ! b ) |
| 650 { | 650 { |
| 651 // Assert the process that created the window is not in our pid_set | 651 // Assert the process that created the window is not in our pid_set |
| 652 return true ; | 652 return true ; |
| 653 } | 653 } |
| 654 return f( window ) ; | 654 return f( window ) ; |
| 655 } | 655 } |
| 656 } ; | 656 } ; |
| 657 | 657 |
| 658 public: | 658 public: |
| 659 template <size_t n_file_names> | 659 template <size_t n_file_names> |
| 660 Process_Closer(Process_Snapshot & snapshot, const wchar_t * (&file_name_list)[
n_file_names]) | 660 ProcessCloser(ProcessSnapshot & snapshot, const wchar_t * (&file_name_list)[n_
file_names]) |
| 661 : snapshot(snapshot), process_names(file_name_list), filter(process_names) | 661 : snapshot(snapshot), process_names(file_name_list), filter(process_names) |
| 662 { | 662 { |
| 663 update() ; | 663 update() ; |
| 664 } | 664 } |
| 665 | 665 |
| 666 /** | 666 /** |
| 667 * Refresh our state to match the snapshot state. | 667 * Refresh our state to match the snapshot state. |
| 668 */ | 668 */ |
| 669 void refresh() | 669 void Refresh() |
| 670 { | 670 { |
| 671 pid_set.clear() ; | 671 pid_set.clear() ; |
| 672 update() ; | 672 update() ; |
| 673 } | 673 } |
| 674 | 674 |
| 675 bool is_running() { return ! pid_set.empty() ; } ; | 675 bool IsRunning() { return ! pid_set.empty() ; } ; |
| 676 | 676 |
| 677 bool contains( DWORD pid ) const { return pid_set.find( pid ) != pid_set.end()
; } ; | 677 bool contains( DWORD pid ) const { return pid_set.find( pid ) != pid_set.end()
; } ; |
| 678 | 678 |
| 679 template< class F > | 679 template< class F > |
| 680 bool iterate_our_windows( F f ) | 680 bool iterate_our_windows( F f ) |
| 681 { | 681 { |
| 682 only_our_processes< F > g( * this, f ) ; | 682 only_our_processes< F > g( * this, f ) ; |
| 683 return enumerate_windows( g ) ; | 683 return enumerate_windows( g ) ; |
| 684 } | 684 } |
| 685 | 685 |
| 686 /* | 686 /* |
| 687 * Shut down every process in the pid_set. | 687 * Shut down every process in the pid_set. |
| 688 */ | 688 */ |
| 689 bool shut_down() ; | 689 bool ShutDown() ; |
| 690 | 690 |
| 691 } ; | 691 } ; |
| 692 | 692 |
| 693 #endif | 693 #endif |
| OLD | NEW |