Using panama with C# dll

Christoph Läubrich laeubi at laeubi-soft.de
Mon Jun 19 10:10:56 UTC 2023


Hi Maurizio,

thanks for your input, yes I'm currently using libmono[2] to embed C# 
runtime and this is working to the basics but is of course a bit 
cumbersome because one has two indirection and each require some 
inconvenient pointer passing and method callings by string names:

panama > libmono
libmono > C#

but as you can inspect the assembly for classes and methods and so on, 
there is also another approach with jni4net [4] that generates aJNI 
interface and according to their documentation it seems they are able to 
directly register using JNI#RegisterNatives [6].

This makes me think that probably jextract could be made to work by 
using a C# assembly as an input and generate a binding with a nice java 
interface, there is also already IKVM [7] that is a JVM that exposes 
mono framework inside a JVM to javacode, so if this would work inside 
standard OpenJDK (maybe one would need a new JDK module jdk.sharp for 
the runtime) but maybe I'm just a bit too enthusiastic :-)

And as both C# and Java are not that far apart (Object,s Exceptions, 
Reflection, Platform Independet Bytecode) it seems a good match to have 
some deeper inter-op.

[4] https://github.com/jni4net/jni4net/tree/0.8.8.0
[5] 
http://zamboch.blogspot.com/2009/11/how-calling-from-java-to-net-works-in.html
[6] 
https://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp17734
[7] https://www.mono-project.com/docs/about-mono/languages/java/

Am 19.06.23 um 11:47 schrieb Maurizio Cimadamore:
> I am not an expert on this topic (and I'd ask others to chime in and/or 
> validate the stuff below), but I'd assume that options to call into a C# 
> assembly are a bit limited (since a #NET assembly doesn't use classic C 
> ABI):
> 
> * use a compiler that understands #NET (obviously great if you work in 
> visual studio)
> * use COM (I have seen examples in this sense on this mailing list)
> * use some 3rd party solution which allows to "export" symbols from C# [1]
> * try to find a way to embed a #NET runtime in your Java application [2]
> 
> I believe the dlls in the project you refer to are plain #NET 
> assemblies, so I don't think a regular (e.g. non-MSVC) compiler would be 
> able to do much with them - and that conclusion would extend to the 
> Linker API.
> 
> Of these approaches, the last seem promising, as your Panama application 
> would interact not with the assembly directlty, but with a library that 
> implements the #NET functionalities (think of it as some kind of JNI for 
> #NET). From there, you have ways to obtain references to #NET methods, 
> and call them [3].
> 
> Of course, I have not tried any of this :-)
> 
> [1] - https://github.com/3F/DllExport
> [2] - https://www.mono-project.com/docs/advanced/embedding/
> [3] - 
> https://www.mono-project.com/docs/advanced/embedding/#invoking-methods-in-the-cil-universe
> 
> 
> On 17/06/2023 13:24, Christoph Läubrich wrote:
>> I'm wondering if anyone has already used panama with a c# DLL[2]?
>>
>> I especially came across this example [1] that says
>>
>> > The assemblies can be used to read RAW files on Windows, Linux,
>> > and MacOS using C# or other languages that can acces a .Net assembly
>>
>>
>> So I'm wondering if similar is possible using panama/java as well?
>>
>>
>> [1] https://github.com/thermofisherlsms/RawFileReader
>> [2] 
>> https://github.com/thermofisherlsms/RawFileReader/tree/main/libs/NetStandard20


More information about the panama-dev mailing list