Java 调用 dll 字符串返回类型方法

Java calling dll String return type method

本文关键字:返回类型 方法 字符串 dll 调用 Java      更新时间:2023-10-16

我正在尝试通过JAVA应用程序调用DLL文件中的函数。 我发现这个来源 http://blog.mwrobel.eu/how-to-call-dll-methods-from-java/,一切都像魅力一样工作。

但是,我想对 DLL 文件进行一些修改,我希望函数从源返回字符串而不是 char。

这是我的Java代码。

public class Main {
public interface simpleDLL extends Library {
simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary("simpleDLL", simpleDLL.class);
int giveIntGetInt(int a);               // int giveIntGetInt(int a);
}
public static void main(String[] args) {
simpleDLL sdll = simpleDLL.INSTANCE;
String result = sdll.giveCharGetChar("abc");
System.out.println("result: " + result);
int a = 3;
int result1 = sdll.giveIntGetInt(a);  // calling function with int parameter&result
System.out.println("giveIntGetInt("+a+"): " + result1);
}
}

这是我C++代码。

#include "simpleDLL.h"
#include <string>
#include <stdexcept>
using namespace std;
using std::string;

namespace simpleDLLNS
{
string simpleDLL::giveCharGetChar(string a)
{
return "abc";
}
int simpleDLL::giveIntGetInt(int a)
{
return 3*a;
}
void simpleDLL::simpleCall(void)
{
int x = 3;
return;
}
int simpleDLL::giveVoidPtrGetInt(void* param)
{
if(param!=0)
{
int* x = (int*)param;
return *x;
}
else
{
return -1;
}
}
}

除此之外,如果我将字符串更改为字符,它会返回韩语单词。如果我使用的是字符串,Java将遇到错误。请帮忙。

有关如何使用 Java 中的 JNA 将字符串发送到 C 库并从 C 库接收字符串的示例:

http://www.eshayne.com/jnaex/index.html?example=2