I need to communicate with C# application using Window messaging from Java application. From my application I register for messages used for communicating. I am able to successfully get the window handle of the C# application and register messages. C# application responds to the messages by sending WM_COPYDATA response messages. I can get to a point where WM_COPYDATA is received. But I am not sure how to extract the message content from the response message.
Really helps if I can get a sample code which reads content from WM_COPYDATA message from java application using jniwrap and winpack libraries. Will be more helpful if the content of lParam is of Structure type.
I had to edit the code to remove sensitive data
The following code gets the other application's window handle by its window name, registers for request and response messages and then sends the request message with empty content.
private Library user32;
private long appHandle;
public void sendRequest() {
long requestMsgId = (int)this.registerWindowMessage("WM_TBD_SN_REQEST");
long responseMsgId = (int)this.registerWindowMessage("WM_TBD_SN_RESPONSE");
long tbdHandle = findWindow(null, "TestApp");
this.sendWindowsMessage(new Handle(tbdHandle), new Int(requestMsgId), new Handle(this.appHandle), new Pointer.Void());
}
public long sendWindowsMessage(final Parameter... args) {
final Function sendMessage = this.user32.getFunction("SendMessageA");
LongInt longInt = new LongInt();
sendMessage.invoke(longInt, args);
return longInt.getValue();
}
public long findWindow(final String classname, final String windowName) {
final Function findWindow = this.user32.getFunction("FindWindowA");
Parameter cName = null;
if (classname == null || classname.equals("")) {
cName = new Pointer.Void();
}
else {
cName = new AnsiString(classname);
}
LongInt longInt = new LongInt();
findWindow.invoke(longInt, cName, new AnsiString(windowName));
return longInt.getValue();
}
public long registerWindowMessage(String message) {
final Function findWindow = this.user32.getFunction("RegisterWindowMessageA");
LongInt longInt = new LongInt();
findWindow.invoke(longInt, new AnsiString(message));
return longInt.getValue();
}
This is the custom window procedure, that will be substituted in place of the native proc for my application's window
public class MyWindowProc extends WindowProc {
@Override
public void callback() {
if (this._msg.getValue() == Msg.WM_COPYDATA) {
// I can get to this point, but not sure how I can get the information from the message
// The WM_TBD_SN_RESPONSE structure consists of four fields
// 1. hWnd Field --- window handle of the calling application...
// 2. msg Field --- WM_COPYDATA message code
// 3. wData Field --- TDB application's window handle
// 4. pData Field --- contains a CopyDataStruct
// CopyDataStruct.pData – contains the Serial Number ----> how to extract this?
// CopyDataStruct.dwData – contains the message code for WM_TBD_SN_RESPONSE (this should match responseMsgId)
}
else {
super.callback();
}
}
}
Please help. Thanks in advance.