Skip to content

Commit

Permalink
Resolve launcher executable path on Windows too
Browse files Browse the repository at this point in the history
  • Loading branch information
msimacek committed Dec 10, 2024
1 parent b25dea7 commit 399bd6c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions sdk/src/org.graalvm.launcher.native/src/launcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ static std::string exe_path() {
#elif defined (_WIN32)
char *realPath = (char *)malloc(_MAX_PATH);
GetModuleFileNameA(NULL, realPath, _MAX_PATH);
/* try to do a realpath equivalent */
HANDLE handle = CreateFile(realPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle != INVALID_HANDLE_VALUE) {
const size_t size = _MAX_PATH + 4;
char *resolvedPath = (char *)malloc(size);
DWORD ret = GetFinalPathNameByHandleA(handle, resolvedPath, size, 0);
/*
* The path returned from GetFinalPathNameByHandleA should always
* use "\\?\" path syntax. We strip the prefix.
* See: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
*/
if (ret < size && resolvedPath[0] == '\\' && resolvedPath[1] == '\\' && resolvedPath[2] == '?' && resolvedPath[3] == '\\') {
strcpy_s(realPath, _MAX_PATH, resolvedPath + 4);
}
free(resolvedPath);
CloseHandle(handle);
}
#endif
std::string p(realPath);
free(realPath);
Expand Down

0 comments on commit 399bd6c

Please sign in to comment.