Java - C# interop

S

SirThanxALot

Is there a way for interop between C# and Java?
Is it possible to use the JNI with C#
Or do I have to do a workaround with (V)C++?

thanx
srtnaxalt
 
A

Alessandro Angeli [MVP::DigitalMedia]

SirThanxALot said:
Is there a way for interop between C# and Java?

JAVA IN C# 1. You can register your Java classes as COM
objects using the ActiveX Bridge in the JDK and then use COM
InterOp.

JAVA IN C# 2. Use P/Invoke to use the JNI invocation API and
System.Reflection.Emit to call the native JNI funtions in
the JNIEnv structure. This is a hack, it's difficult and
practically undocumented.

C# IN JAVA. You can write a JNI DLL in C# by using
System.Reflection.Emit to call the native JNI funtions in
the JNIEnv structure and then usng ILDasm and ILAsm to
create exported JNI functions from the C# DLL. This is a
hack, it's difficult and practically undocumented.
Is it possible to use the JNI with C#
Or do I have to do a workaround with (V)C++?

Using mC++ is a lot easier. You can think of creating a JNI
wrapper in mC++ instead of writing it all in mC++.

There are a couple of Java/.NET interop libraries somewhere
on the web.

You can also just create the Java part and the .NET part in
2 separate processes and then use socket streams to make
them communicate.
 
T

Thomas Fritsch

SirThanxALot said:
Is there a way for interop between C# and Java?
Is it possible to use the JNI with C#
Or do I have to do a workaround with (V)C++?

thanx
srtnaxalt

Java (via JNI) can interop with C, and C can interop with C++.
So it comes down to the question: Can C# interop with C or C++ ?
 
S

SirThanxALot

Thanx for the most informing reply.
So now it comes down to this:

I need to the C# in Java thingy.
For now, I see the JNI dll writing in C# as a last resort.
I'd better make a VC++ wrapper/bridge

something like
java <-1-> jni <-2-> c++ <-3-> C#
parts 1 and 2 are ok for me
3 remains an issue as Thomas Fritsch stated

I just want to do this. Ihave made a class in C#
I want to create an instance of it (from within C++) and invoke
methods on it.

Thanx,
srtnxalt
 
A

Alessandro Angeli [MVP::DigitalMedia]

SirThanxALot said:
I just want to do this. Ihave made a class in C#
I want to create an instance of it (from within C++) and
invoke methods on it.

You need to use the managed C++ extension in VC7.x (mC++,
for brevity). Compile your C# code into a DLL assembly,
reference the types in this assembly from mC++ and compile
your mC++ into a DLL assembly as well (/clr switch), but
include the native JNI code (mC++ can mix managed and native
code). I think you can also compile your C# into a module
and link this module inside the mC++ DLL, but I never tried.
 
D

dc

What are you really trying to do?

There are a zillion ways to do interop between Java and C#. You can break
them down into categories:
1- in process
2- out of process, directo communications
3- shared resource

For inprocess, you can do it the way Mr Agneli has described. It's a bit
messy. There are other options.

for example
Have you seen IKVM? It is a JVM written implemented on top of the CLR.
What this means is you can take any Java class and run it in a .NET
container.
Some other neat possibilities like, producing Java "stubs" for .NET class
libraries , and then being able to call them from Java code.

www.ikvm.net

For out-of-process, there you are talking about webservices, SOAP, socket
comms.

For shared resource, maybe both the Java app and the C# app read and write
the same file, or queue, or database.

Which way you choose depends on the requirements of your situation.


-Dino
http://blogs.msdn.com/DotNetInterop
 
Joined
Aug 11, 2009
Messages
1
Reaction score
0
Interop between Java and C#

You may try CSharpJavaMerger to see if it can meet your needs. It is an open source tool.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top