| 195 | } |
| 196 | |
| 197 | bool ObjectManager::EnumHandles(PCWSTR type, DWORD pid, bool namedObjectsOnly) { |
| 198 | EnumTypes(); |
| 199 | |
| 200 | ULONG len = 1 << 25; |
| 201 | std::unique_ptr<BYTE[]> buffer; |
| 202 | do { |
| 203 | buffer = std::make_unique<BYTE[]>(len); |
| 204 | auto status = ::NtQuerySystemInformation(SystemExtendedHandleInformation, buffer.get(), len, &len); |
| 205 | if (status == STATUS_INFO_LENGTH_MISMATCH) { |
| 206 | len <<= 1; |
| 207 | continue; |
| 208 | } |
| 209 | if (status == 0) |
| 210 | break; |
| 211 | return false; |
| 212 | } while (true); |
| 213 | |
| 214 | auto filteredTypeIndex = type == nullptr || ::wcslen(type) == 0 ? -1 : _typesNameMap.at(type)->TypeIndex; |
| 215 | |
| 216 | auto p = (SYSTEM_HANDLE_INFORMATION_EX*)buffer.get(); |
| 217 | auto count = p->NumberOfHandles; |
| 218 | _handles.clear(); |
| 219 | _handles.reserve(count); |
| 220 | for (decltype(count) i = 0; i < count; i++) { |
| 221 | auto& handle = p->Handles[i]; |
| 222 | if (pid && handle.UniqueProcessId != pid) |
| 223 | continue; |
| 224 | |
| 225 | if (filteredTypeIndex >= 0 && handle.ObjectTypeIndex != filteredTypeIndex) |
| 226 | continue; |
| 227 | |
| 228 | // skip Object Explorer process? |
| 229 | if (_skipThisProcess && handle.UniqueProcessId == ::GetCurrentProcessId()) |
| 230 | continue; |
| 231 | |
| 232 | std::wstring name; |
| 233 | if (namedObjectsOnly && (name = GetObjectName((HANDLE)handle.HandleValue, (DWORD)handle.UniqueProcessId, handle.ObjectTypeIndex)).empty()) |
| 234 | continue; |
| 235 | |
| 236 | auto hi = std::make_shared<HandleInfo>(); |
| 237 | hi->HandleValue = (ULONG)handle.HandleValue; |
| 238 | hi->GrantedAccess = handle.GrantedAccess; |
| 239 | hi->Object = handle.Object; |
| 240 | hi->HandleAttributes = handle.HandleAttributes; |
| 241 | hi->ProcessId = (ULONG)handle.UniqueProcessId; |
| 242 | hi->ObjectTypeIndex = handle.ObjectTypeIndex; |
| 243 | hi->Name = name; |
| 244 | |
| 245 | _handles.emplace_back(hi); |
| 246 | } |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | const std::vector<std::shared_ptr<ObjectInfo>>& ObjectManager::GetObjects() const { |
| 252 | return _objects; |