使用GraalVM将Quarkus应用生成DLL供C#调用时创建Isolate报错的问题求助
使用GraalVM将Quarkus应用生成DLL供C#调用时创建Isolate报错的问题求助
我现在需要把Quarkus应用打包成DLL文件,试过IKVM、IKVM.net、JNI这些方法都没成功,目前打算用GraalVM来实现。
我已经能成功编译Java类,用的命令是:
javac *.java
也能生成DLL,命令是:
native-image --shared -H:Name=dbexample -H:-CheckToolchain --enable-native-access=ALL-UNNAMED --no-fallback
用dumpbin /EXPORTS dbexample.dll查看生成的DLL,能看到Java里的方法确实被导出了,但调用的时候却报错,看起来是内存访问相关的通用错误,比如提示“Exited Thread 44280”。
我的Java代码
import org.graalvm.nativeimage.IsolateThread; import org.graalvm.nativeimage.c.function.CEntryPoint; import org.graalvm.nativeimage.Isolates; import org.graalvm.nativeimage.Isolates.CreateIsolateParameters; public final class LogicConnectionImpl implements LogicConnection { @CEntryPoint(name = "IsolateCreate") public static IsolateThread createIsolate(@SuppressWarnings("unused") IsolateThread thread) { var context = Isolates.createIsolate(CreateIsolateParameters.getDefault()); return context; } @CEntryPoint(name = "doSomething") public static void doSomething(@SuppressWarnings("unused") IsolateThread thread) { System.out.println("Hello, World from inside JAVA!"); } @CEntryPoint(name = "test") public static int test(@SuppressWarnings("unused") IsolateThread thread) { return 23; } @CEntryPoint(name = "test2") public static void test2(@SuppressWarnings("unused") IsolateThread thread, int value) { int value2 = value + 1; System.out.println("Hello, World from inside JAVA!"); } public void test3(String thisTest) { System.out.println("Hello, World from inside JAVA! " + thisTest); } }
我的C#代码
using System; using System.Runtime.InteropServices; class Program { // Define the P/Invoke signatures [DllImport("dbexample.dll", EntryPoint = "doSomething")] public static extern void DoSomething(IntPtr thread); [DllImport("dbexample.dll", EntryPoint = "test")] public static extern int Test(IntPtr thread); [DllImport("dbexample.dll", EntryPoint = "test2")] public static extern void Test2(IntPtr thread, int value); [DllImport("dbexample.dll", EntryPoint = "IsolateCreate")] public static extern IntPtr IsolateCreate(IntPtr thread); static void Main(string[] args) { try { IntPtr isolateThread = IsolateCreate(IntPtr.Zero); if (isolateThread == IntPtr.Zero) { Console.WriteLine("Failed to create IsolateThread."); return; } // Call the methods from the DLL DoSomething(isolateThread); int result = Test(isolateThread); Console.WriteLine("Result from test: " + result); Test2(isolateThread, 42); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } }
报错发生在这一行:
IntPtr isolateThread = IsolateCreate(IntPtr.Zero);
我觉得问题出在方法参数(IntPtr.Zero)上。根据我了解到的信息,调用doSomething这类方法不能直接传IntPtr.Zero,必须用Java应用里createIsolate方法返回的IsolateThread。但问题是,被导出到DLL的createIsolate方法本身也需要一个IsolateThread类型的参数,而在C#里我只能传IntPtr.Zero进去,这就陷入了死循环。
感觉我离成功只差一步,但就是找不到这个关键的调整点,有没有大佬能帮我看看?
备注:内容来源于stack exchange,提问作者AppWebDeveloper
相关产品推荐
相关产品推荐

