6

我花了很多时间尝试在我的 Java 应用程序中使用 C# 函数,但没有成功......我用 C# 编写了以下库:

public class Converter
{

    public Converter()
    {
    }

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

这个 dll 调用另一个 dll 进行一些操作,但是当我编译它时,我可以在我的 Realse 文件夹中找到 Dll,一切似乎都正常,所以我使用 32 位选项、64 位和任何 CPU 选项编译它只是为了确保它不是我的问题。

使用 32 位的Dependency Walker和任何 CPU 选项分析我的 dll 文件,它说找不到 IESHIMS.DLL,并显示以下消息:

警告:至少没有找到一个延迟加载依赖模块。警告:由于延迟加载依赖模块中缺少导出功能,至少有一个模块存在未解析的导入。

64 位文件不会出现这种情况,但我找不到我的 ConvertHtmlToPdf 函数。

由于我不知道它是否相关,我的第二步是在 Java 代码中。

要加载我的库,我会这样做:

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);
}

(该库似乎加载成功,因为如果我尝试在我的应用程序运行时删除 dll 文件,它会说无法删除,因为它正在使用中)最后:

IConversorLibrary.INSTANCE.ConvertHtmlToPdf(directoryPath);

但结果并不如我所愿:

java.lang.UnsatisfiedLinkError: Error looking up function 'ConvertHtmlToPdf': Could not find the specified procedure.

我不知道我做错了什么,我尝试了很多教程和很多东西,但任何东西似乎都有效,任何帮助都非常感谢。

4

2 回答 2

3

这个 Nugget 非常易于使用并且运行良好。https://www.nuget.org/packages/UnmanagedExports

您需要 Visual Studio 2012 (express)。安装后,只需 [RGiesecke.DllExport.DllExport]在要导出的任何静态函数之前添加。而已!

例子:

C#

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

爪哇

在顶部添加导入:

   import com.sun.jna.Native;

在你的类中添加接口。它是您的 C# 函数名称,前面带有字母“I”:

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

在课堂上需要的地方调用 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"));
于 2013-10-11T13:11:56.573 回答
3

正如technomage所说:

JNA 可以从使用 C 链接的 DLL 加载。AC# 类默认不支持任何类型的 C 链接。C++ 支持使用外部“C”表示法的 C 链接。

本文展示了一种使 C# DLL 方法像 C 样式 DLL 一样可调用的方法,不幸的是它非常复杂。

于 2013-10-09T13:36:44.550 回答