简体   繁体   English

如何在Java程序中调用.Net DLL中的方法

[英]How to call a method in .Net DLL in a Java program

I was trying to use a .NET DLL from Java code, the tsMemberFunctions.DLL is loaded successfully, but the code fails to call the actual function. 我试图使用Java代码中的.NET DLL, tsMemberFunctions.DLL已成功加载,但代码无法调用实际函数。

See the snippet below: 请参阅下面的代码:

public class tsMemberFunctions {  
    public native void GetMemberJSONSample();

    static {
        System.loadLibrary("tsMemberFunctions");
        System.out.println("Loaded");
    }

    public static void main(String[] args) {
        new tsMemberFunctions().GetMemberJSONSample();

    }
}

On executing above method I am getting below error: 在执行上述方法时,我收到以下错误:

Loaded
Exception in thread "main" java.lang.UnsatisfiedLinkError: tsMemberFunctions.GetMemberJSONSample()V
    at tsMemberFunctions.GetMemberJSONSample(Native Method)
    at tsMemberFunctions.main(tsMemberFunctions.java:12)

Can someone please tell me if I missed anything or anything is incorrect in the code or suggest better alternative for this use case. 有人可以告诉我,如果我错过任何内容或代码中的任何内容不正确或建议更好的替代此用例。 TIA. TIA。

You have to be very careful with the names and exports. 你必须非常小心名称和出口。

Let's say you have this super simple lib 假设你有这个超级简单的lib

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include "jni.h"
#include <stdio.h>

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

extern "C" JNIEXPORT void JNICALL Java_recipeNo001_HelloWorld_displayMessage

(JNIEnv* env, jclass obj) {

    printf("Hello world!\n");

}

You have to make sure to build your DLL for proper architecture (it will depend on Java version you have - 32/64 bit). 您必须确保构建适当的体系结构的DLL (它将取决于您拥有的Java版本 - 32/64位)。

Let's say you have x64 DLL and x64 JDK , you can call your lib like this 假设您有x64 DLLx64 JDK ,您可以像这样调用您的lib

package recipeNo001;

public class HelloWorld {

    public static native void displayMessage();

    static {
        System.load("C:\\Users\\your_name\\Source\\Repos\\HelloWorld\\x64\\Debug\\HelloWorld.dll");
    }

    public static void main(String[] args) {
      HelloWorld.displayMessage();
    }
}

In your case, I bet you don't have extern "C" in your code - this is why your symbol can't be found by JVM. 在你的情况下,我敢打赌你的代码中没有extern "C" - 这就是JVM无法找到你的符号的原因。

When it comes to tools, I suggest Visual Studio 2019 (when it comes to creating DLL) and IntelliJ for Java code. 说到工具,我建议使用Visual Studio 2019(创建DLL时)和IntelliJ for Java代码。

You can find lots of samples here: http://jnicookbook.owsiak.org/ and here: https://github.com/mkowsiak/jnicookbook 你可以在这里找到很多样本: http//jnicookbook.owsiak.org/和这里: https//github.com/mkowsiak/jnicookbook

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

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