Skip to content

Commit fac2984

Browse files
committed
Ensure UTF-8 encoding for JNA string marshaling in Windows tray structures and library initialization
1 parent 1d8613c commit fac2984

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTray.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import com.sun.jna.win32.StdCallLibrary
66

77
@Structure.FieldOrder("icon_filepath", "tooltip", "cb", "menu")
88
internal class WindowsNativeTray : Structure() {
9+
companion object {
10+
init {
11+
// Ensure UTF-8 encoding for JNA string marshaling before any structure write
12+
val key = "jna.encoding"
13+
if (System.getProperty(key).isNullOrBlank()) {
14+
System.setProperty(key, "UTF-8")
15+
}
16+
}
17+
}
18+
919
@JvmField
1020
var icon_filepath: String? = null
1121

src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayLibrary.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import com.sun.jna.win32.StdCallLibrary
99
// This object registers the native library and exposes external (native) methods.
1010
internal object WindowsNativeTrayLibrary : StdCallLibrary {
1111
init {
12+
// Ensure JNA marshals Java/Kotlin strings as UTF-8. This must be set
13+
// before the first interaction with JNA/native code, otherwise JNA will
14+
// default to the system ANSI code page on Windows and corrupt Unicode.
15+
val key = "jna.encoding"
16+
if (System.getProperty(key).isNullOrBlank()) {
17+
System.setProperty(key, "UTF-8")
18+
}
1219
// Register the native library "tray" for direct calls
1320
Native.register("tray")
1421
}

src/commonMain/kotlin/com/kdroid/composetray/lib/windows/WindowsNativeTrayMenuItem.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import com.sun.jna.Structure
55

66
@Structure.FieldOrder("text", "icon_path", "disabled", "checked", "cb", "submenu")
77
internal open class WindowsNativeTrayMenuItem : Structure() {
8+
companion object {
9+
init {
10+
// Ensure UTF-8 encoding for JNA string marshaling as early as possible
11+
val key = "jna.encoding"
12+
if (System.getProperty(key).isNullOrBlank()) {
13+
System.setProperty(key, "UTF-8")
14+
}
15+
}
16+
}
17+
818
@JvmField
919
var text: String? = null
1020

0 commit comments

Comments
 (0)