Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6222549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:20:03+00:00 2026-05-24T08:20:03+00:00

I need to communicate with C# application using Window messaging from Java application. From

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:20:04+00:00Added an answer on May 24, 2026 at 8:20 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a P2P application, and I need to get it to communicate
I need to communicate with an XML-RPC server from a .NET 2.0 client. Can
I need to communicate with a hardware device using TCP and with Windows I
Hey, I'm using php 5 and need to communicate with another server that runs
My application is using SWIG to communicate between c++ and python on windows. suppose
I need to communicate a service application with vcl application. My old application was
I need to communicate with legacy php application. The API is just a php
We have an AS400 that we need to communicate with using our .NET applications.
I have a client/server application that communicates with .Net remoting. I need my clients
I need to communicate with a RS232 device, I have no specs or information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.