Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-60376] Resolve launcher executable path on Windows too #10282

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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