Need help with JNA on Kotlin Multiplatform

Hi guys.

Im trying to set messaging between two instances of my kotlin desktop (windows) project.
The app is a music player and the idea is that once an instance of the app is already running, new instances will message that instance with file to play instead of opening new instances, then close them.

I tried using a local file to read and write but its not reliable enough, so now im trying to use JNA to send and receive messages between the two instances.

this is my function to listen and receive messages:

private fun setupMessagesListener(){ val user32 = User32.INSTANCE val messagePump = Thread { val msg = MSG() val window = user32.FindWindow(null, "MyAppName") User32.INSTANCE.PostMessage(window, WinUser.WM_USER, WinDef.WPARAM(0), WinDef.LPARAM(0)) while (user32.GetMessage(msg, window, 0, 0) > 0){ if (msg.message == WinUser.WM_COPYDATA){ val copyData = COPYDATASTRUCT(msg.pointer) val chars = copyData.lpData.getCharArray(0, copyData.cbData / 2) val filePath = String(chars) val file = File(filePath) if (file.exists()){ handleFileArgument(filePath) } } } user32.TranslateMessage(msg) user32.DispatchMessage(msg) } messagePump.start()} 

and this is the function to send messages:

private fun sendFilePathMessage(filePath: String){ val user32 = User32.INSTANCE val activeInstance = user32.FindWindow(null, "MyAppName") activeInstance?.let { val wideString = filePath.toCharArray() val size = (wideString.size * Char.SIZE_BYTES) + Char.SIZE_BYTES val buffer = Memory(size.toLong()) buffer.setWideString(0, filePath) val copyData = COPYDATASTRUCT().apply { dwData = ULONG_PTR(0) cbData = size lpData = buffer } val pointer = copyData.pointer copyData.write() user32.SendMessage(activeInstance, WinUser.WM_COPYDATA, null, WinDef.LPARAM(Pointer.nativeValue(pointer))) } } 

when i debug my listener function, it always stop at while condition and never gets inside the loop, nor it comes back.
when i try to evaluate
user32.GetMessage(msg, window, 0, 0)
the debugger never finishes its just stuck.

submitted by /u/iParki
[link] [comments]