I am trying to send a String from Java (First app) to c++ (the Second app with a message-only window).
So firstly, what I tried is this: I adapted this example into my code and got this on the java side
STRMSG msg = new STRMSG();
msg.message = "test";
//msg.write(); // Idk exactly why because i thought JNA does this before every call. Works the same without
cds = new COPYDATASTRUCT();
cds.dwData = new ULONG_PTR(UniqueWindowMessageID);
cds.cbData = msg.size();
cds.lpData = msg.getPointer();
cds.write(); // But here it is somehow needed because otherwise the message will not arrive.
JNA.USER32.INSTANCE.SendMessage(
msgOnlyWnd, WinUser.WM_COPYDATA,
null,
new LPARAM(Pointer.nativeValue(cds.getPointer()))
);
Oh, and STRMSG
looks like this (also adapted from the example):
public class STRMSG extends Structure {
public STRMSG() {
super();
}
public STRMSG(Pointer p) {
super(p);
read();
}
public String message;
protected List<String> getFieldOrder() {
return Arrays.asList("message");
}
}
On the c++ side, I got my message-only window, and I can catch the WM_COPYDATA message successfully.
But somehow, my String value is empty here:
case WM_COPYDATA: {
PCOPYDATASTRUCT cds = (PCOPYDATASTRUCT)lParam;
if (cds->dwData == UniqueWindowMessageID) { // <- works
STRMSG* msgStruct = (STRMSG*)cds->lpData; // <- The message struct is corrupt i guess
cout << msgStruct->message << endl; // Will just endl in the console
}
break;
}
This is the struct on the c++ side:
const struct STRMSG {
string message;
};
Besides this attempt, I also tried to adapt my struct so it contains a BYTE* (byte[]) and the size so I can create a String out of the bytes and the size using this: (source)
std::string s(reinterpret_cast<char const*>(inputParam), lengthParam);
But my struct isn't that what it should be in any way. So the data is somehow faulty, and IDK what I am doing wrong here.
Any help is appreciated.
EDIT: I also looked at the data while debugging. I didn't get anything:
EDIT2:
After the comment Remy my COPYDATASTRUCT looks like this:
byte[] msg = "test".getBytes();
cds = new COPYDATASTRUCT();
cds.dwData = new ULONG_PTR(UniqueWindowMessageID);
cds.cbData = msg.length;
cds.lpData = msg // how??
cds.write();
But how should I map the cds.lpData
to the byte[]
?