| OLD | NEW |
| 1 #include <stdexcept> | 1 #include <stdexcept> |
| 2 #include <functional> | 2 #include <functional> |
| 3 #include <wctype.h> | 3 #include <wctype.h> |
| 4 // <thread> is C++11, but implemented in VS2012 | 4 // <thread> is C++11, but implemented in VS2012 |
| 5 #include <thread> | 5 #include <thread> |
| 6 | 6 |
| 7 #include "process.h" | 7 #include "process.h" |
| 8 | 8 |
| 9 //------------------------------------------------------- | 9 //------------------------------------------------------- |
| 10 // Windows_Handle | 10 // Windows_Handle |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 { | 24 { |
| 25 this -> ~Windows_Handle() ; | 25 this -> ~Windows_Handle() ; |
| 26 handle = h ; | 26 handle = h ; |
| 27 validate_handle() ; | 27 validate_handle() ; |
| 28 } | 28 } |
| 29 | 29 |
| 30 void Windows_Handle::validate_handle() | 30 void Windows_Handle::validate_handle() |
| 31 { | 31 { |
| 32 if ( handle == INVALID_HANDLE_VALUE ) | 32 if ( handle == INVALID_HANDLE_VALUE ) |
| 33 { | 33 { |
| 34 throw std::runtime_error( "Invalid handle" ) ; | 34 // TODO: This code really deserves use of a proper exception class that pack
ages |
| 35 // Windows API errors consistently. |
| 36 char tmp[256]; |
| 37 sprintf_s(tmp, "Invalid handle. Last error: %d", GetLastError()); |
| 38 throw std::runtime_error( tmp ) ; |
| 35 } | 39 } |
| 36 } | 40 } |
| 37 | 41 |
| 38 //------------------------------------------------------- | 42 //------------------------------------------------------- |
| 39 // process_by_name_CI | 43 // process_by_name_CI |
| 40 //------------------------------------------------------- | 44 //------------------------------------------------------- |
| 41 process_by_name_CI::process_by_name_CI( const wchar_t * name ) | 45 process_by_name_CI::process_by_name_CI( const wchar_t * name ) |
| 42 : name( name ), length( wcslen( name ) ) | 46 : name( name ), length( wcslen( name ) ) |
| 43 {} | 47 {} |
| 44 | 48 |
| 45 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process ) | 49 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process ) |
| 46 { | 50 { |
| 47 return 0 == wcsncmpi( process.szExeFile, name, length ) ; | 51 return 0 == wcsncmpi( process.szExeFile, name, length ) ; |
| 48 } | 52 } |
| 49 | 53 |
| 50 //------------------------------------------------------- | 54 |
| 51 // process_by_any_exe_name_CI | 55 bool process_by_any_exe_with_any_module::operator()( const PROCESSENTRY32W & pro
cess ) |
| 52 //------------------------------------------------------- | |
| 53 bool process_by_any_exe_name_CI::operator()( const PROCESSENTRY32W & process ) | |
| 54 { | 56 { |
| 55 return names.find( process.szExeFile ) != names.end() ; | 57 if (processNames.find(process.szExeFile) != processNames.end()) |
| 58 { |
| 59 if (moduleNames.empty()) |
| 60 return true; |
| 61 |
| 62 ModulesSnapshot ms(process.th32ProcessID); |
| 63 ModulesSnapshot::Pointer me = ms.begin(); |
| 64 while (me != ms.end()) |
| 65 { |
| 66 if (moduleNames.find(me->szModule) != moduleNames.end()) |
| 67 { |
| 68 return true; |
| 69 } |
| 70 me = ms.next(); |
| 71 } |
| 72 } |
| 73 return false; |
| 56 } | 74 } |
| 57 | 75 |
| 58 //------------------------------------------------------- | 76 //------------------------------------------------------- |
| 59 // wcscmpi | 77 // wcscmpi |
| 60 //------------------------------------------------------- | 78 //------------------------------------------------------- |
| 61 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) | 79 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) |
| 62 { | 80 { |
| 63 // Note: Equality of character sequences is case-insensitive in all predicates
below. | 81 // Note: Equality of character sequences is case-insensitive in all predicates
below. |
| 64 // Loop invariant: s1[0..j) == s2[0..j) | 82 // Loop invariant: s1[0..j) == s2[0..j) |
| 65 const size_t LIMIT( 65535 ) ; // Runaway limit of 2^16 - 1 should be acceptabl
y long. | 83 const size_t LIMIT( 65535 ) ; // Runaway limit of 2^16 - 1 should be acceptabl
y long. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ; | 173 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ; |
| 156 } | 174 } |
| 157 | 175 |
| 158 void Snapshot::refresh() | 176 void Snapshot::refresh() |
| 159 { | 177 { |
| 160 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ; | 178 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ; |
| 161 } | 179 } |
| 162 | 180 |
| 163 | 181 |
| 164 //------------------------------------------------------- | 182 //------------------------------------------------------- |
| 183 // ModulesSnapshot |
| 184 //------------------------------------------------------- |
| 185 ModulesSnapshot::ModulesSnapshot(DWORD processId) |
| 186 : handle(::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, p
rocessId)) |
| 187 { |
| 188 module.dwSize = sizeof(MODULEENTRY32); |
| 189 } |
| 190 |
| 191 MODULEENTRY32W * ModulesSnapshot::begin() |
| 192 { |
| 193 return ::Module32FirstW(handle, &module) ? (&module) : 0; |
| 194 } |
| 195 |
| 196 MODULEENTRY32W * ModulesSnapshot::next() |
| 197 { |
| 198 return ::Module32NextW(handle, &module) ? (&module) : 0; |
| 199 } |
| 200 |
| 201 |
| 202 //------------------------------------------------------- |
| 165 // send_message, send_endsession_messages | 203 // send_message, send_endsession_messages |
| 166 //------------------------------------------------------- | 204 //------------------------------------------------------- |
| 167 /** | 205 /** |
| 168 * Default process exit wait time (per message) 5000 ms | 206 * Default process exit wait time (per message) 5000 ms |
| 169 * | 207 * |
| 170 * 5 seconds is time that the system will wait before it considers a process non
-responsive. | 208 * 5 seconds is time that the system will wait before it considers a process non
-responsive. |
| 171 */ | 209 */ |
| 172 static const unsigned int timeout = 5000 ; // milliseconds | 210 static const unsigned int timeout = 5000 ; // milliseconds |
| 173 | 211 |
| 174 /** | 212 /** |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 if ( ! is_running() ) | 445 if ( ! is_running() ) |
| 408 { | 446 { |
| 409 return true ; | 447 return true ; |
| 410 } | 448 } |
| 411 } | 449 } |
| 412 // Assert is_running() | 450 // Assert is_running() |
| 413 } | 451 } |
| 414 // No control path leaves the for-loop. | 452 // No control path leaves the for-loop. |
| 415 } ; | 453 } ; |
| 416 | 454 |
| OLD | NEW |