0

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.

Developer
  • 27
  • 1
  • 10

2 Answers2

2

First off I'm not a Java developer and I haven't tested the code below, but I do understand WM_COPYDATA so I can make a reasonable answer to your question.

The WM_COPYDATA message sends a pointer to (that is the address of a) COPYDATASTRUCT which is defined by Windows as:

struct COPYDATASTRUCT {
  ULONG_PTR dwData;
  DWORD     cbData;
  PVOID     lpData;
}

From Java you will have to read this manually using the methods in the sun.misc.Unsafe class. By manually I mean you will have to calculate the memory addresses yourself.

dwData is an integer value the application can use for itself. lpData is a pointer to a buffer holding data that the application wanted to pass. cbData is the number of bytes in the buffer that lpData contains.

In Windows a ULONG_PTR is 4 bytes on a 32 bit system and 8 bytes on a 64 bit system. A DWORD is 4 bytes always. A PVOID, which is a pointer (i.e. a memory address) is 4 bytes on a 32 bit system and 8 bytes on a 64 bit system.

So on a 32 bit system dwData is at offset 0, cbData at offset 4, and lpData at offset 8. On a 64 bit system dwData is still at offset 0, but cbData is at offset 8, and lpData at offset 16.

import sun.misc;

final int dwDataOffset = 0;
final int cbDataOffset = 4;  // Change to 8 for 64 bit
final int lpDataOffset = 8;  // Change to 16 for 64 bit

int cpDataAddr = this._pData.getValue();         // This will return the address of the struct (I assume this syntax is correct) - change to long for 64 bit
int messageCode= Unsafe.getInt(cpDataAddr+dwDataOffset);  // Change to getLong for 64 bit
int dataSize = Unsafe.getInt(cbDataAddr+cbDataOffset);
int dataAddress = Unsafe.GetInt(cbDataAddr+lpDataOffset); // Change to getLong for 64 bit

// Create a buffer to hold the data from lpData
byte[] data = new byte[dataSize];
for (int i = 0; i < dataSize; i++)
   data[i] = Unsafe.getByte(dataAddress+i);

Once your done data will contain the raw data that the application passed in, which is in your case the license. If the license is a string you should be able to pass the byte array to the String constructor. If it is a more complex data structure you will have to read it in using the Unsafe methods just like we did for the COPYDATASTRUCT

shf301
  • 31,086
  • 2
  • 52
  • 86
2

There is a much easier way to send messages between a Java application and a C# application. Use ZeroMQ messages using the TCP transport tcp://127.0.0.1:portnum The ZeroMQ guide http://zguide.zeromq.org/page:all shows dozens of examples of communication patterns that yu can implement with only a few lines of code.

Some people don't use it because it does not support the IPC transport on Windows, but it does support the TCP transport and that works just fine as an IPC solution because the kernel recognizes that it is a local destination and shortcuts the unneeded TCP/IP stack processing.

You mentioned that you are trying to control a C# program for which you have no access to the source code. This type of thing is often called screen-scraping, and you might be better off by writing a simple C# application using Managed Spy or some C++ code using Spy++ to act as an intermediary for your Java application.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
  • Thank you for suggesting ZeroMQ. Unfortunately we cannot use this solution as the C# application is provided by another company and we cannot change it. – Developer Aug 07 '11 at 20:22