Using panama with C# dll

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


Yes it feels it *should* be possible, but my FFM skills are currently to 
basic to wrap my head around it (especially method handles), so jextract 
is a great help and I wished something similar would be there for C# 
assemblies :-)

About the conversions I think the basic primitives match quite well 
between Java and .net ad mono already offers UTf8 strings, so one is 
quite close here if one not expect a complete Object > Object mapping 
but something similar jextract generates to get/set struct members but 
for methods/properties.

Am 19.06.23 um 12:29 schrieb Maurizio Cimadamore:
> I see what you mean re. two indirections and pointer passing.
> 
> When reading the Mono embedding doc, I noticed that libmono has 
> something that looks useful:
> 
> https://www.mono-project.com/docs/advanced/embedding/#unmanaged-to-managed-thunks
> 
> This allows to create a function pointer out of a mono method. Then the 
> pointer can be treated as a foreign call by the linker (e.g. you can 
> link against that pointer).
> 
> It's a slightly less dynamic approach (and probably more efficient), in 
> that it allows, in principle, to define a library which can be used to 
> "lookup" Mono methods (this will create thunks), and then link against 
> them (using the linker).
> 
> The biggest issue would remain to marshal/unmarhal args/returns to and 
> from Mono-land. But it seems like a useful library on top of the Linker 
> could be defined using these building blocks? (then of course, one could 
> also define a tool which uses said library to generate all the MHs they 
> need in one go, a la jextract).
> 
> Maurizio
> 
> On 19/06/2023 11:10, Christoph Läubrich wrote:
>> 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