I have a Java program and I want to send a command from my Win32 application. Normally I'd use WM_COPYDATA
but what options do I have with Java?
There are some ways to interoperate between Java and Windows. Ordered in power and difficulty:
- For handling window messages, you could use Jawin - it even features a demo of how to handle window messages - or something similar. Of course, if you bind your Java program to a library like Jawin, it will never run on a non-Windows machine
- For simple interaction between Win32 and Java, a socket bound to listen only on localhost would be my favourite choice. The protocol may be simple, but I would prefer a plain text protocol for easier debugging. Be aware that a socket connection can break down if the user terminates a program.
- You can use (local) web services, like suggested in other posts here. On both sides make sure that you use your Webservice/XML libaries to construct and parse the messages, it is far too easy to construct malformed XML if you do string concatenation.
- You can put the functionality of your Windows program into a COM component and use a Java-to-COM bridge: Jacob or j-Interop are popular free libaries for this, j-Integra seems a popular choice for businesses with legacy systems.
- You can put the functionality of your Java program into a COM component and use Sun's Java-ActiveX bridge. From my personal experience, this is a rather awkward option: Development of the Java-ActiveX bridge has stalled since 1.4, the installation of the ActiveX causes your Java component to be installed somewhere in the JRE directory and debugging your Java components inside the ActiveX container is rather cumbersome.
Sidenote: if you are dealing with strings on both sides, always take into account that Java handles strings as something quite different from byte arrays. Especially if you are using Windows ANSI strings be aware that the characters 81, 8D, 8F, 90, and 9D are specified as undefined in the Windows-1252 codepage, therefore Java will produce question marks or exceptions if your Windows strings contain these elements. Therefore, if at all possible, use WChar strings on the Windows side or restrict yourself to safe characters.