Skip to content

Commit 399bd6c

Browse files
committed
Resolve launcher executable path on Windows too
1 parent b25dea7 commit 399bd6c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sdk/src/org.graalvm.launcher.native/src/launcher.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,23 @@ static std::string exe_path() {
209209
#elif defined (_WIN32)
210210
char *realPath = (char *)malloc(_MAX_PATH);
211211
GetModuleFileNameA(NULL, realPath, _MAX_PATH);
212+
/* try to do a realpath equivalent */
213+
HANDLE handle = CreateFile(realPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
214+
if (handle != INVALID_HANDLE_VALUE) {
215+
const size_t size = _MAX_PATH + 4;
216+
char *resolvedPath = (char *)malloc(size);
217+
DWORD ret = GetFinalPathNameByHandleA(handle, resolvedPath, size, 0);
218+
/*
219+
* The path returned from GetFinalPathNameByHandleA should always
220+
* use "\\?\" path syntax. We strip the prefix.
221+
* See: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
222+
*/
223+
if (ret < size && resolvedPath[0] == '\\' && resolvedPath[1] == '\\' && resolvedPath[2] == '?' && resolvedPath[3] == '\\') {
224+
strcpy_s(realPath, _MAX_PATH, resolvedPath + 4);
225+
}
226+
free(resolvedPath);
227+
CloseHandle(handle);
228+
}
212229
#endif
213230
std::string p(realPath);
214231
free(realPath);

0 commit comments

Comments
 (0)