| 18 | namespace OpenKneeboard { |
| 19 | |
| 20 | wchar_t* GetFullPathForCurrentExecutable() { |
| 21 | static std::once_flag sOnce; |
| 22 | // `QueryFullProcessImageNameW()` requires us to provide a size; it doesn't |
| 23 | // support the usual 'pass a nullptr and get the size back, then call again' |
| 24 | // thing. |
| 25 | static wchar_t sBuffer[MAX_PATH] {}; |
| 26 | |
| 27 | std::call_once(sOnce, [&buffer = sBuffer]() { |
| 28 | memset(buffer, 0, std::size(buffer) * sizeof(buffer[0])); |
| 29 | // `QueryFullProcessImageNameW()` requires a real handle, |
| 30 | // not the pseudo-handle returned by `GetCurrentProcess()`. |
| 31 | winrt::handle process {OpenProcess( |
| 32 | PROCESS_QUERY_LIMITED_INFORMATION, false, GetCurrentProcessId())}; |
| 33 | if (!process) { |
| 34 | dprint( |
| 35 | "OpenProcess(..., GetCurrentProcessID()) failed: {} @ {}", |
| 36 | GetLastError(), |
| 37 | std::source_location::current()); |
| 38 | return; |
| 39 | } |
| 40 | auto characterCount = static_cast<DWORD>(std::size(buffer)); |
| 41 | const auto result |
| 42 | = QueryFullProcessImageNameW(process.get(), 0, buffer, &characterCount); |
| 43 | if (result == 0) { |
| 44 | dprint( |
| 45 | "QueryFullProcessImageNameW() returned {}, failed with {:#018x} @ {}", |
| 46 | result, |
| 47 | GetLastError(), |
| 48 | std::source_location::current()); |
| 49 | } |
| 50 | }); |
| 51 | return sBuffer; |
| 52 | } |
| 53 | |
| 54 | }// namespace OpenKneeboard |
no test coverage detected