如何在Java中使用JNA lib使用C#函数 [英] How to use C# function in Java using JNA lib
问题描述
我花了很多时间尝试在我的Java应用程序中使用C#函数但没有成功...
我用C#写了下面的lib:
I've spent many hours trying to use a C# function inside my Java Application but had no success... I wrote the following lib in C#:
public class Converter
{
public Converter()
{
}
public bool ConvertHtmlToPdf(String directoryPath)
{
//DO SOMETHING
}
}
这个dll调用另一个dll来进行一些操作,但是当我编译它时,我可以在我的Realse文件夹中找到Dlls,一切似乎都没问题,所以我使用32位选项,64位和任意编译它CPU选项只是为了确保它不是我的问题。
This dll calls another dll to make some operations but when I compile it I can find Dlls in my Realse folder and everything seems to be ok, so I compiled it using 32bit option, 64bit, and Any CPU option just to make sure that it's not my issue.
使用依赖性Walker 在32位和任何CPU选项中,它表示无法找到IESHIMS.DLL,并显示此消息:
Analizing my dll files with Dependency Walker in 32 bit and Any CPU option it says that IESHIMS.DLL can't be found, and show this message:
警告:找不到至少一个延迟加载依赖模块。
警告:由于延迟加载相关模块中缺少导出功能,至少有一个模块具有未解析的导入。
Warning: At least one delay-load dependency module was not found. Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.
不会出现64位文件,但是我找不到我的ConvertHtmlToPdf函数。
It doesn't occur with the 64bit file, nevertheless I can't find my ConvertHtmlToPdf function.
由于我不知道它是否相关,我的第二步是在Java代码中。
As I don't know if it is relevant or not, my second step was in the Java Code.
要加载我的库我做:
System.setProperty("jna.library.path", "C:\\Program Files (x86)\\Facilit\\Target App\\lib");
和:
public interface IConversorLibrary extends Library {
IConversorLibrary INSTANCE = (IConversorLibrary) Native.loadLibrary("converter", IConversorLibrary.class);
void ConvertHtmlToPdf(String directoryPath);
}
(lib似乎加载成功,因为如果我尝试删除dll文件与我的应用程序运行它说,无法删除因为它在使用)
和最后:
(The lib seems to be load successful, cause if I try to delete dll file with my application running it says that can't be deleted cause it's in using) and finally:
IConversorLibrary.INSTANCE.ConvertHtmlToPdf(directoryPath);
但结果并非如我所愿:
java.lang.UnsatisfiedLinkError: Error looking up function 'ConvertHtmlToPdf': Could not find the specified procedure.
我不知道我做错了什么,我尝过很多教程和很多东西,但任何似乎都有效,任何帮助都非常感激。
I don't know what I'm doing wrong, I've tried many tutorials and many things, but anything seems to work, any help is really appreciated.
推荐答案
正如technomage所说:
As said by technomage:
JNA可以从使用C链接的DLL加载。一个C#类没有通过
默认支持任何一种C链接。 C ++支持与
externC符号的C链接。
JNA can load from DLLs that use C linkage. A C# class does not by default support any kind of C linkage. C++ supports C linkage with the extern "C" notation.
这篇文章展示了一种使C#DLLs方法像C风格的DLL一样可调用的方法,遗憾的是它非常复杂。
This article shows a way to make C# DLLs methods callable like C-style DLL, unfortunately it's a quite complex.
这篇关于如何在Java中使用JNA lib使用C#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!