Skip to content

Commit

Permalink
Use Unicode version of Windows API to get image path
Browse files Browse the repository at this point in the history
  • Loading branch information
pejovica committed Feb 13, 2023
1 parent 6c41297 commit f83f84d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@
import com.oracle.svm.core.windows.headers.Process;
import com.oracle.svm.core.windows.headers.WinBase;
import com.oracle.svm.core.windows.headers.WinBase.HANDLE;
import com.oracle.svm.core.windows.headers.WindowsLibC.WCharPointer;

@AutomaticallyRegisteredImageSingleton(ProcessPropertiesSupport.class)
public class WindowsProcessPropertiesSupport extends BaseProcessPropertiesSupport {

@Override
public String getExecutableName() {
CCharPointer path = UnsafeStackValue.get(WinBase.MAX_PATH, CCharPointer.class);
WinBase.HMODULE hModule = LibLoaderAPI.GetModuleHandleA(WordFactory.nullPointer());
int result = LibLoaderAPI.GetModuleFileNameA(hModule, path, WinBase.MAX_PATH);
return result == 0 ? null : CTypeConversion.toJavaString(path);
return getModulePath(LibLoaderAPI.GetModuleHandleA(WordFactory.nullPointer()));
}

@Override
Expand Down Expand Up @@ -122,10 +120,16 @@ public String getObjectFile(PointerBase symbolAddress) {
(CCharPointer) symbolAddress, module) == 0) {
return null;
}
return getModulePath(module.read());
}

CCharPointer path = UnsafeStackValue.get(WinBase.MAX_PATH, CCharPointer.class);
int result = LibLoaderAPI.GetModuleFileNameA(module.read(), path, WinBase.MAX_PATH);
return result == 0 ? null : CTypeConversion.toJavaString(path);
private static String getModulePath(WinBase.HMODULE module) {
WCharPointer path = UnsafeStackValue.get(WinBase.MAX_PATH, WCharPointer.class);
int length = LibLoaderAPI.GetModuleFileNameW(module, path, WinBase.MAX_PATH);
if (length == 0 || length == WinBase.MAX_PATH) {
return null;
}
return WindowsSystemPropertiesSupport.toJavaString(path, length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected String javaLibraryPathValue() {
return libraryPath.toString();
}

private static String toJavaString(WCharPointer wcString, int length) {
static String toJavaString(WCharPointer wcString, int length) {
return asCharBuffer(wcString, length).toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.word.PointerBase;

import com.oracle.svm.core.windows.headers.WindowsLibC.WCharPointer;
import com.oracle.svm.core.windows.headers.WinBase.HMODULE;
import com.oracle.svm.core.windows.headers.WinBase.HMODULEPointer;
import com.oracle.svm.core.windows.headers.WindowsLibC.WCharPointer;

// Checkstyle: stop

Expand All @@ -48,10 +48,6 @@ public class LibLoaderAPI {
@CFunction(transition = NO_TRANSITION)
public static native int FreeLibrary(HMODULE hLibModule);

/** Retrieves the fully qualified path of the file that contains the specified module. */
@CFunction(transition = NO_TRANSITION)
public static native int GetModuleFileNameA(HMODULE hModule, CCharPointer lpFilename, int nSize);

/** Retrieves the fully qualified path of the file that contains the specified module. */
@CFunction(transition = NO_TRANSITION)
public static native int GetModuleFileNameW(HMODULE hModule, WCharPointer lpFilename, int nSize);
Expand Down

0 comments on commit f83f84d

Please sign in to comment.