| 17488 | |
| 17489 | |
| 17490 | DWORD GetProcessName(DWORD aProcessID, LPTSTR aBuf, DWORD aBufSize, bool aGetNameOnly) |
| 17491 | { |
| 17492 | *aBuf = '\0'; // Set default. |
| 17493 | HANDLE hproc; |
| 17494 | // Windows XP/2003 would require PROCESS_QUERY_INFORMATION, but those OSes are not supported. |
| 17495 | if ( !(hproc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, aProcessID)) ) |
| 17496 | return 0; |
| 17497 | |
| 17498 | // Benchmarks showed that attempting GetModuleBaseName/GetModuleFileNameEx |
| 17499 | // first did not help performance. Also, QueryFullProcessImageName appeared |
| 17500 | // to be slower than the following. |
| 17501 | DWORD buf_length = GetProcessImageFileName(hproc, aBuf, aBufSize); |
| 17502 | if (buf_length) |
| 17503 | { |
| 17504 | LPTSTR cp; |
| 17505 | if (aGetNameOnly) |
| 17506 | { |
| 17507 | // Convert full path to just name. |
| 17508 | cp = _tcsrchr(aBuf, '\\'); |
| 17509 | if (cp) |
| 17510 | tmemmove(aBuf, cp + 1, _tcslen(cp)); // Includes the null terminator. |
| 17511 | } |
| 17512 | else |
| 17513 | { |
| 17514 | // Convert device path to logical path. |
| 17515 | TCHAR device_path[MAX_PATH]; |
| 17516 | TCHAR letter[3]; |
| 17517 | letter[1] = ':'; |
| 17518 | letter[2] = '\0'; |
| 17519 | // For simplicity and because GetLogicalDriveStrings does not exist on Win2k, it is not used. |
| 17520 | for (*letter = 'A'; *letter <= 'Z'; ++(*letter)) |
| 17521 | { |
| 17522 | DWORD device_path_length = QueryDosDevice(letter, device_path, _countof(device_path)); |
| 17523 | if (device_path_length > 2) // Includes two null terminators. |
| 17524 | { |
| 17525 | device_path_length -= 2; |
| 17526 | if (!_tcsncmp(device_path, aBuf, device_path_length) |
| 17527 | && aBuf[device_path_length] == '\\') // Relies on short-circuit evaluation. |
| 17528 | { |
| 17529 | // Copy drive letter: |
| 17530 | aBuf[0] = letter[0]; |
| 17531 | aBuf[1] = letter[1]; |
| 17532 | // Contract path to remove remainder of device name. |
| 17533 | tmemmove(aBuf + 2, aBuf + device_path_length, buf_length - device_path_length + 1); |
| 17534 | buf_length -= device_path_length - 2; |
| 17535 | break; |
| 17536 | } |
| 17537 | } |
| 17538 | } |
| 17539 | } |
| 17540 | } |
| 17541 | |
| 17542 | CloseHandle(hproc); |
| 17543 | return buf_length; |
| 17544 | } |
| 17545 | |
| 17546 | __int64 pow_ll(__int64 base, __int64 exp) |
| 17547 | { |
no outgoing calls
no test coverage detected