| 228 | |
| 229 | template<class T = std::filesystem::path> |
| 230 | T GetModulePath(HMODULE hModule) |
| 231 | { |
| 232 | static constexpr auto INITIAL_BUFFER_SIZE = MAX_PATH; |
| 233 | static constexpr auto MAX_ITERATIONS = 7; |
| 234 | |
| 235 | if constexpr (std::is_same_v<T, std::filesystem::path>) |
| 236 | { |
| 237 | std::u16string ret; |
| 238 | std::filesystem::path pathret; |
| 239 | auto bufferSize = INITIAL_BUFFER_SIZE; |
| 240 | for (size_t iterations = 0; iterations < MAX_ITERATIONS; ++iterations) |
| 241 | { |
| 242 | ret.resize(bufferSize); |
| 243 | size_t charsReturned = 0; |
| 244 | charsReturned = GetModuleFileNameW(hModule, (LPWSTR)&ret[0], bufferSize); |
| 245 | if (charsReturned < ret.length()) |
| 246 | { |
| 247 | ret.resize(charsReturned); |
| 248 | pathret = ret; |
| 249 | return pathret; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | bufferSize *= 2; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | T ret; |
| 260 | auto bufferSize = INITIAL_BUFFER_SIZE; |
| 261 | for (size_t iterations = 0; iterations < MAX_ITERATIONS; ++iterations) |
| 262 | { |
| 263 | ret.resize(bufferSize); |
| 264 | size_t charsReturned = 0; |
| 265 | if constexpr (std::is_same_v<T, std::string>) |
| 266 | charsReturned = GetModuleFileNameA(hModule, &ret[0], bufferSize); |
| 267 | else |
| 268 | charsReturned = GetModuleFileNameW(hModule, &ret[0], bufferSize); |
| 269 | if (charsReturned < ret.length()) |
| 270 | { |
| 271 | ret.resize(charsReturned); |
| 272 | return ret; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | bufferSize *= 2; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | return T(); |
| 281 | } |
| 282 | |
| 283 | template<class T = std::filesystem::path> |
| 284 | T GetThisModulePath() |
no outgoing calls
no test coverage detected