Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C# code from Java?

Does anyone have a good solution for integrating some C# code into a java application?

The code is small, so I could re-write in java, but I would rather reuse the code if possible. Don't repeat yourself, etc.

Also, I know I can expose the C# as a web service or whatever, but it has some security/encryption stuff in there, so I would rather keep it tightly integrated if possible.


Edit: It's going to be on a server-based app, so "downloading" another runtime is irrelevant.

like image 277
Keith G Avatar asked Sep 08 '08 18:09

Keith G


People also ask

What is calling function in C?

When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.

Can you call C from C++?

Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code.

Can I call C from rust?

Rust can link to/call C functions via its FFI, but not C++ functions.


1 Answers

You would use the Java Native Interface to call your C# code compiled into a DLL.

If its a small amount of C#, it would be much easier to port it to Java. If its a lot, this might be a good way to do it.

Here is a highlevel overview of it:

http://en.wikipedia.org/wiki/Java_Native_Interface

Your other option would be to create a COM assembly from the C# code and use J-Interop to invoke it.

http://sourceforge.net/projects/j-interop/

like image 121
FlySwat Avatar answered Oct 04 '22 15:10

FlySwat