Communicate Java and C# - c#

I'm working on a project that uses an RFID reader, which only works with a library in C#. The thing is I'd really like to work with Java (develop the rest of the program, GUI, etc), and use the C# program just to ask the reader to read the information and return a string to the Java program.
So, is there a way I could do this?
Thanks in advance.

One way to approach this is to look at it as a problem of interprocess communication. There are a bunch of options (assuming Java has access to the necessary Windows API's which I'm assuming it does, but I'm not really a Java dev).
Named Pipes, TCP/IP, Filesystem, Mailslots, etc.
Here's a good article on some options: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx
Another option, which I don't know enough to speak about, is trying to load a .Net library into your java process.

Couldn't you use sockets?
They both do support it, but I never tried to do it between different languages.
Good luck.

If you don't mind delving too deep you could use the Java Native Interface to generate code to marshall calls from Java to C# and back again. You'd need to build a "bridge" in c/c++ (c++ is generally slightly easier).
This way you get in-process communication, which is the fastest way to work :-)

Related

Call Java Methods in Eclipse Plugin from C# program

I have written a C# program which has to call two methods in an eclipse plugin process.
I need an idea on how to realize the communication between the C# process and the java process.
You could use Sockets or else Inter Process Communication (IPC) Structures (more here). The best one depends on what you are doing and how you are doing it, which is not something you are exposing in your question.
Maybe try this: Ikvm
its a JVM that's implemented in .net and it provide several tools to help the development.

C# app create objects from Java classes?

Can a C# app create objects defined in Java .class files?
Is any interop between C# and Java possible, with C# as the host language?
What you are looking for is a combination of PInvoke and JNI.
You will have to create classes in Java. Expose them via JNI. Access these through PInvoke in C#. You have a to and fro communication between C# to Java.
http://en.wikipedia.org/wiki/PInvoke
http://en.wikipedia.org/wiki/Java_Native_Interface
If you need direct instantiation you need JNBridge otherwise you can expose the Java program as a server and communicate with any RPC technology (TCP, http etc.)
I have used IKVM in the past. It works well for large projects, but might be too much for you. It all depends on where you're going with this. But if you want complete interop it's worth looking into.
Otherwise, if you an expain your intentions better someone might have a better idea.

Interacting with java code from C#

We've written a Java program which we are looking to use and interact with from C#. What are our options? Optimally it would be possible to compile the Java application as a library (.DLL) that we could reference from C# perhaps using P/Invoke. This, however, doesn't appear to be an option according to the first few searches online.
We opt to be able to use ASP.NET to built a search engine powered by the Java code, so if this opens up for any other options please let us know.
Sorry, you cannot call java code / classes Directly from C# code.
One way of doing this is to wrap up your java classes in a java Web Service and call classes indirectly through that web service interface in your C# code.
Another way is using
javareg.exe which exposes java classes as COM. You can find it at following location:
C:\Program Files\Microsoft VisualStudio\VIntDev98\bin\javareg.exe
Following posts might help as well
Calling Java Classes Directly from
.NET (uses runtime bridge)
Calling Java from Microsoft.NET
The simplest approach would probably be to publish the functionality of your java library as web services and add a web-reference from your asp.net application.
Java isn't meant to be embedded in another program, so you need a bridge. The most simple solution is to use a socket: Create a Java process which listens for commands on a socket. In the C#, send the commands to the socket and read the answers.
The main problem here is serialization but if you use XML, it's not such a big pain anymore. Try the built-in XML serialization (see this article) or custom frameworks like XStream or Simple.
It is certainly possible to wrap Java in a .dll, and has been a part of the core Java platform for over 10 years. JNI (Java Native Interface) has an interface for embedding a JVM in your code, meaning you can run Java classes using C-style linking. Note that this will require that you write a simple C wrapper, there are samples within:
http://java.sun.com/docs/books/jni/html/invoke.html#11202
As some of these other posts suggest, sometimes it's desirable to be less tightly coupled, so you may want to consider using another design. One option would be a simple database, where the Java application regularly polls for requests from the C# code. If you want tighter coupling, for things like call-backs, you can look at distributed interfaces.

C++ return values for Web Service

I have some .dll native C++ programs which mainly return int/double values, array structures and string values. These values should be taken by a Web Service program made in C#.
I would like to know if it is really necessary to modify my C++ programs and adapt to Web service, i.e. return values such as a XML string/file together with a XSD string,/file. Personally I think I should not modify them because I think C# can receive C++ values using interop and easily serialize using components of .Net library.
However, I would like to receive comments about the best, fast and effective way to pass C++ values to a Web Service.
Thanks!!
I think you can do it as you stated.
In the past, I achieved the same or similar by writing a C++/CLI wrapper around my native classes and consumed those from C#. This didn't incur the overhead of C# interop, which I've noticed can be quite expensive.
I think P/Invoke is what you want here. It will allow you to pass your simple and composite types between managed and unmanaged code, and you won't have to write any C++/CLI wrapper assemblies.
This (MSDN) is a good start for P/Invoke. If you scroll down here's a section called 'Specifying Custom Marshaling for User-Defined Structs'. This will allow you to pass your user-defined structs back and forth.
Look up MarshallAs and you can see all the primitive native types you can marshall. The DllImport attribute is something you will want to search for as well.
If performance becomes an issue, I would recommend serializing/deserializing into either named pipes or a local socket, but I'm not totally clear on the performance chararistics there. Good Luck!
The best, fastest, most efficient and effective way to expose your C++ application as a web service is to put C++ web service code on top of it.
See GSoap for a very fast, open source, implementation - one that is 3-5 times faster than the .NET and Java equivalents.
As long as you can return it to C#, C# should be able to return it from a web service. You should not have to do any manual serialization at all.
If you choose to go the serialization route you might want to look at 'thrift' (http://incubator.apache.org/thrift/).
From the website:
Thrift is a software framework for
scalable cross-language services
development. It combines a software
stack with a code generation engine to
build services that work efficiently
and seamlessly between C++, Java,
Python, PHP, Ruby, Erlang, Perl,
Haskell, C#, Cocoa, Smalltalk, and
OCaml.
Originally developed at Facebook,
Thrift was open sourced in April 2007
and entered the Apache Incubator in
May, 2008.

How does one share data between a C++app and a C# app without files?

I have a C++ application with data that needs to be shared with a C# application.
I'm currently transferring the data via files, but with speed and quantity of the data becoming an issue I would like to find a way to share the data through memory.
I'm a beginner to intermediate programmer at best, and so far I have heard of two methods that may be able to help me do this: Socket Programming & Memory Mapped Files
So my questions is, which is the best way to do this? (yes, speed is a factor)
and any info or links to info that could help me in my research and comprehension of the method you suggest would be very much appreciated.
Thank you,
You could use named pipes for interprocess communication. I haven't used it from c++ land yet though..
Sockets, IMO. It is standard, fast (even more so if you are running in the same machine) and very flexible. Memory mapped files I'm not sure if it is supported by C# but I could be wrong.
This is a fairly difficult question, mostly because there is no right answer. You definitely could use sockets or memory mapped files to communicate between two processes. Other alternatives are COM or simply posting a windows message from one process to another.
.NET - COM interoperability
Erm. I would suggest a different approach. I believe you would find it much easier to make an object (or objects) in managed C++. You can keep everything in your project native C++, except this file/class, which you would compile with /clr.
The /clr class will be able to interop trivially with .net, since it is .net. Then you can add regular C++ methods to the object to get data in and out as you see fit. Typically I would suggest copying data across the boundary so you don't need to screw around with pinning or anything.
It takes a little getting used to, but this approach is very flexible.
Here's a little something that talks about it better than I can: http://blogs.microsoft.co.il/blogs/sasha/archive/2008/02/16/net-to-c-bridge.aspx
I'd probably suggest googling interprocess communication to do this. Each OS has a different method, but you basically need to signal one application from the other (with the signal encoding the data and datatypes).
Using sockets is a good method, but has the problem that if the loopback address of the machine is kaput (which can easily be done through mis-configuration while playing around with ip stuff) then your app won't be able to communicate. On a more serious note, if the loopback address is compromised and made to point at an attackers machine then your application is suddenly sending data to a malicious source, although this may or may not be a problem for you.
Memory mapped files aren't natively available in C#, but you could probably do something using P/Invoke.

Categories