Skip to content

Commit

Permalink
[GR-60376] Resolve launcher executable path on Windows too
Browse files Browse the repository at this point in the history
PullRequest: graal/19547
  • Loading branch information
msimacek committed Dec 11, 2024
2 parents 19fae97 + 399bd6c commit 6dc9495
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 6dc9495

Please sign in to comment.