简体   繁体   English

如何在Java中使用JNA lib使用C#函数

[英]How to use C# function in Java using JNA 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#: 我花了很多时间尝试在我的Java应用程序中使用C#函数但没有成功......我在C#中编写了以下lib:

public class Converter
{

    public Converter()
    {
    }

    public bool ConvertHtmlToPdf(String directoryPath)
    {
        //DO SOMETHING
    }
}

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. 这个dll调用另一个dll进行一些操作,但是当我编译它时,我可以在我的Realse文件夹中找到Dlls,一切似乎都没问题,所以我使用32位选项,64位和任何CPU选项编译它只是为了确保它不是我的问题。

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: 使用Dependency Walker在32位和任何CPU选项中分析我的dll文件,它说IESHIMS.DLL无法找到,并显示以下消息:

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. 警告:由于延迟加载相关模块中缺少导出功能,至少有一个模块具有未解析的导入。

It doesn't occur with the 64bit file, nevertheless I can't find my ConvertHtmlToPdf function. 64位文件没有出现,但我找不到我的ConvertHtmlToPdf函数。

As I don't know if it is relevant or not, my second step was in the Java Code. 由于我不知道它是否相关,我的第二步是在Java代码中。

To load my library I do: 要加载我的库我做:

System.setProperty("jna.library.path", "C:\\Program Files (x86)\\Facilit\\Target App\\lib");

and: 和:

public interface IConversorLibrary extends Library {

    IConversorLibrary INSTANCE = (IConversorLibrary) Native.loadLibrary("converter", IConversorLibrary.class);

    void ConvertHtmlToPdf(String directoryPath);
}

(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: (lib似乎加载成功,因为如果我尝试删除运行它的应用程序的dll文件,说它无法删除,因为它正在使用),最后:

IConversorLibrary.INSTANCE.ConvertHtmlToPdf(directoryPath);

But the result is not really as I wish: 但结果并非如我所愿:

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. 我不知道我做错了什么,我尝试了许多教程和很多东西,但是任何事情似乎都有效,任何帮助都非常感激。

As said by technomage: 正如technomage所说:

JNA can load from DLLs that use C linkage. JNA可以从使用C链接的DLL加载。 AC# class does not by default support any kind of C linkage. 默认情况下,AC#class不支持任何类型的C链接。 C++ supports C linkage with the extern "C" notation. C ++支持与外部“C”表示法的C链接。

This article shows a way to make C# DLLs methods callable like C-style DLL, unfortunately it's a quite complex. 本文展示了一种使C#DLLs方法可以像C风格的DLL一样调用的方法,遗憾的是它非常复杂。

This Nugget is super easy to use and works perfectly. 这个Nugget非常易于使用,效果很好。 https://www.nuget.org/packages/UnmanagedExports https://www.nuget.org/packages/UnmanagedExports

You need Visual Studio 2012 (express). 您需要Visual Studio 2012(快速)。 Once installed, just add [RGiesecke.DllExport.DllExport] before any static function you want to export. 安装后,只需在要导出的任何静态函数之前添加[RGiesecke.DllExport.DllExport] That's it! 而已!

Example: 例:

C# C#

[RGiesecke.DllExport.DllExport]
public static int YourFunction(string data)
{
     /*Your code here*/
     return 1;
}

Java Java的

Add the import at the top: 在顶部添加导入:

   import com.sun.jna.Native;

Add the interface in your class. 在您的课程中添加界面。 Its your C# function name preceded by the letter "I": 它的C#函数名称前面带有字母“I”:

  public interface IYourFunction extends com.sun.jna.Library
    {
       public int YourFunction(String tStr);
    };

Call your DLL where you need it in your class: 在课堂上调用您需要的DLL:

IYourFunction iYourFunction = (IYourFunction )Native.loadLibrary("full or relative path to DLL withouth the .dll extention", IYourFunction.class);//call JNA
        System.out.println("Returned: " + IYourFunction.YourFunction("some parameter"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM