Skip to content

Commit

Permalink
OrcLib: WinApi: add GetModuleFileNameExApi
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Jan 26, 2021
1 parent 8bfa2ff commit 988fe17
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/OrcLib/WinApiHelper.cpp
Expand Up @@ -10,6 +10,7 @@

#include <windows.h>
#include <safeint.h>
#include <psapi.h>

#include "WinApiHelper.h"

Expand Down Expand Up @@ -207,4 +208,38 @@ std::wstring GetModuleFileNameApi(HMODULE hModule, std::error_code& ec) noexcept
return GetModuleFileNameApi(hModule, 32767 * sizeof(wchar_t), ec);
}

std::wstring GetModuleFileNameExApi(HANDLE hProcess, HMODULE hModule, size_t cbMaxOutput, std::error_code& ec) noexcept
{
std::wstring path;

try
{
path.resize(cbMaxOutput);

DWORD cch = ::GetModuleFileNameExW(hProcess, hModule, path.data(), path.size());

DWORD lastError = GetLastError();
if (cch == 0 || lastError != ERROR_SUCCESS)
{
ec.assign(lastError, std::system_category());
return {};
}

path.resize(cch);
path.shrink_to_fit();
}
catch (const std::length_error& e)
{
ec = std::make_error_code(std::errc::not_enough_memory);
return {};
}

return path;
}

std::wstring GetModuleFileNameExApi(HANDLE hProcess, HMODULE hModule, std::error_code& ec) noexcept
{
return GetModuleFileNameExApi(hProcess, hModule, 32767 * sizeof(wchar_t), ec);
}

} // namespace Orc
6 changes: 6 additions & 0 deletions src/OrcLib/WinApiHelper.h
Expand Up @@ -50,6 +50,12 @@ std::wstring GetModuleFileNameApi(HMODULE hModule, size_t cbMaxOutput, std::erro
// GetModuleFileName wrapper with default maximum buffer size of 32767 characters
std::wstring GetModuleFileNameApi(HMODULE hModule, std::error_code& ec) noexcept;

// GetModuleFileNameEx wrapper with custom maximum buffer size
std::wstring GetModuleFileNameExApi(HANDLE hProcess, HMODULE hModule, size_t cbMaxOutput, std::error_code& ec) noexcept;

// GetModuleFileNameEx wrapper with default maximum buffer size of 32767 characters
std::wstring GetModuleFileNameExApi(HANDLE hProcess, HMODULE hModule, std::error_code& ec) noexcept;

} // namespace Orc

#pragma managed(pop)

0 comments on commit 988fe17

Please sign in to comment.