| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 // TODO: This code really deserves use of a proper exception class that pack ages | |
| 35 // Windows API errors consistently. | |
| 34 char tmp[256]; | 36 char tmp[256]; |
| 35 » sprintf(tmp, "Invalid handle. Last error: %d", GetLastError()); | 37 sprintf_s(tmp, "Invalid handle. Last error: %d", GetLastError()); |
|
Eric
2014/03/27 16:52:44
Using sprintf() seems safe here, but it's such a b
| |
| 36 throw std::runtime_error( tmp ) ; | 38 throw std::runtime_error( tmp ) ; |
| 37 } | 39 } |
| 38 } | 40 } |
| 39 | 41 |
| 40 //------------------------------------------------------- | 42 //------------------------------------------------------- |
| 41 // process_by_name_CI | 43 // process_by_name_CI |
| 42 //------------------------------------------------------- | 44 //------------------------------------------------------- |
| 43 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 ) |
| 44 : name( name ), length( wcslen( name ) ) | 46 : name( name ), length( wcslen( name ) ) |
| 45 {} | 47 {} |
| 46 | 48 |
| 47 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process ) | 49 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process ) |
| 48 { | 50 { |
| 49 return 0 == wcsncmpi( process.szExeFile, name, length ) ; | 51 return 0 == wcsncmpi( process.szExeFile, name, length ) ; |
| 50 } | 52 } |
| 51 | 53 |
| 52 //------------------------------------------------------- | 54 |
| 53 // process_by_any_exe_name_CI | 55 bool process_by_any_exe_with_any_module::operator()( const PROCESSENTRY32W & pro cess ) |
| 54 //------------------------------------------------------- | 56 { |
| 55 bool process_by_any_exe_name_CI::operator()( const PROCESSENTRY32W & process ) | 57 if (processNames.find(process.szExeFile) != processNames.end()) |
| 56 { | 58 { |
| 57 return names.find( process.szExeFile ) != names.end() ; | 59 if (moduleNames.empty()) |
| 58 } | 60 return true; |
| 59 | 61 |
| 60 //------------------------------------------------------- | 62 ModulesSnapshot ms(process.th32ProcessID); |
| 61 // process_by_any_exe_name_CI_w_ABP | 63 MODULEENTRY32W* me = ms.first(); |
| 62 //------------------------------------------------------- | 64 while (me != NULL) |
| 63 bool process_by_any_exe_name_CI_w_ABP::operator()( const PROCESSENTRY32W & proce ss ) | 65 { |
| 64 { | 66 if (moduleNames.find(me->szModule) != moduleNames.end()) |
| 65 bool moduleFound = false; | 67 { |
|
Eric
2014/03/27 16:52:44
We can eliminate 'moduleFound'
| |
| 66 if ( names.find( process.szExeFile ) != names.end() ) | 68 return true; |
| 67 { | 69 } |
| 68 if (moduleNames.empty()) | 70 me = ms.next(); |
| 69 » » return true; | 71 } |
|
Eric
2014/03/27 16:52:44
We should document that if no module names are pre
| |
| 70 | 72 } |
| 71 » ModulesSnapshot ms( process.th32ProcessID ); | 73 return false; |
| 72 return true; | |
|
Eric
2014/03/27 16:52:44
Stub return for testing something, I assume?
| |
| 73 » MODULEENTRY32* me = ms.begin(); | |
| 74 » while ( me != ms.end() ) | |
| 75 » { | |
| 76 » » if (moduleNames.find( me->szModule ) != moduleNames.end()) | |
| 77 » » { | |
| 78 » » » moduleFound = true; | |
|
Eric
2014/03/27 16:52:44
return true
| |
| 79 » » » break; | |
| 80 » » } | |
| 81 » » me = ms.next(); | |
| 82 » } | |
| 83 } | |
| 84 return moduleFound; | |
|
Eric
2014/03/27 16:52:44
return false
| |
| 85 } | 74 } |
| 86 | 75 |
| 87 //------------------------------------------------------- | 76 //------------------------------------------------------- |
| 88 // wcscmpi | 77 // wcscmpi |
| 89 //------------------------------------------------------- | 78 //------------------------------------------------------- |
| 90 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) | 79 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) |
| 91 { | 80 { |
| 92 // 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. |
| 93 // Loop invariant: s1[0..j) == s2[0..j) | 82 // Loop invariant: s1[0..j) == s2[0..j) |
| 94 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 | 156 |
| 168 //------------------------------------------------------- | 157 //------------------------------------------------------- |
| 169 // Snapshot | 158 // Snapshot |
| 170 //------------------------------------------------------- | 159 //------------------------------------------------------- |
| 171 Snapshot::Snapshot() | 160 Snapshot::Snapshot() |
| 172 : handle( ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ) | 161 : handle( ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ) |
| 173 { | 162 { |
| 174 process.dwSize = sizeof( PROCESSENTRY32W ) ; | 163 process.dwSize = sizeof( PROCESSENTRY32W ) ; |
| 175 } | 164 } |
| 176 | 165 |
| 177 PROCESSENTRY32W * Snapshot::begin() | 166 PROCESSENTRY32W * Snapshot::first() |
| 178 { | 167 { |
| 179 return ::Process32FirstW( handle, & process ) ? ( & process ) : 0 ; | 168 return ::Process32FirstW(handle, &process) ? (&process) : 0; |
| 180 } | 169 } |
| 181 | 170 |
| 182 PROCESSENTRY32W * Snapshot::next() | 171 PROCESSENTRY32W * Snapshot::next() |
| 183 { | 172 { |
| 184 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ; | 173 return ::Process32NextW(handle, &process) ? (&process) : 0; |
| 185 } | 174 } |
| 186 | 175 |
| 187 void Snapshot::refresh() | 176 void Snapshot::refresh() |
| 188 { | 177 { |
| 189 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ; | 178 handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 190 } | 179 } |
| 191 | 180 |
| 192 | 181 |
| 193 //------------------------------------------------------- | 182 //------------------------------------------------------- |
| 194 // ModulesSnapshot | 183 // ModulesSnapshot |
| 195 //------------------------------------------------------- | 184 //------------------------------------------------------- |
| 196 ModulesSnapshot::ModulesSnapshot(DWORD processId) | 185 ModulesSnapshot::ModulesSnapshot(DWORD processId) |
| 197 : handle( ::CreateToolhelp32Snapshot( TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processId ) ) | 186 : handle(::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, p rocessId)) |
| 198 { | 187 { |
| 199 module.dwSize = sizeof( MODULEENTRY32 ) ; | 188 module.dwSize = sizeof(MODULEENTRY32); |
| 200 } | 189 } |
| 201 | 190 |
| 202 MODULEENTRY32W * ModulesSnapshot::begin() | 191 MODULEENTRY32W * ModulesSnapshot::first() |
| 203 { | 192 { |
| 204 return ::Module32FirstW( handle, & module ) ? ( & module ) : 0 ; | 193 return ::Module32FirstW(handle, &module) ? (&module) : 0; |
| 205 } | 194 } |
| 206 | 195 |
| 207 MODULEENTRY32W * ModulesSnapshot::next() | 196 MODULEENTRY32W * ModulesSnapshot::next() |
| 208 { | 197 { |
| 209 return ::Module32NextW( handle, & module ) ? ( & module ) : 0 ; | 198 return ::Module32NextW(handle, &module) ? (&module) : 0; |
| 210 } | |
| 211 | |
| 212 void ModulesSnapshot::refresh(DWORD processId) | |
|
Eric
2014/03/27 16:52:44
refresh() not needed.
| |
| 213 { | |
| 214 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processId ) ; | |
|
Eric
2014/03/27 16:52:44
Not that it matters, but processID isn't initializ
| |
| 215 } | 199 } |
| 216 | 200 |
| 217 | 201 |
| 218 //------------------------------------------------------- | 202 //------------------------------------------------------- |
| 219 // send_message, send_endsession_messages | 203 // send_message, send_endsession_messages |
| 220 //------------------------------------------------------- | 204 //------------------------------------------------------- |
| 221 /** | 205 /** |
| 222 * Default process exit wait time (per message) 5000 ms | 206 * Default process exit wait time (per message) 5000 ms |
| 223 * | 207 * |
| 224 * 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. |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 if ( ! is_running() ) | 445 if ( ! is_running() ) |
| 462 { | 446 { |
| 463 return true ; | 447 return true ; |
| 464 } | 448 } |
| 465 } | 449 } |
| 466 // Assert is_running() | 450 // Assert is_running() |
| 467 } | 451 } |
| 468 // No control path leaves the for-loop. | 452 // No control path leaves the for-loop. |
| 469 } ; | 453 } ; |
| 470 | 454 |
| LEFT | RIGHT |