Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java调用dll字符串返回类型方法_Java_C++_Dll_Jna - Fatal编程技术网

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

Java调用dll字符串返回类型方法,java,c++,dll,jna,Java,C++,Dll,Jna,我试图通过JAVA应用程序调用DLL文件中的函数。 我发现这个来源和一切都像一个魅力 但是,我想对DLL文件进行一些修改,我希望函数从源代码返回字符串而不是char 这是我的java代码 public class Main { public interface simpleDLL extends Library { simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary("simpleDLL", simpleDLL.class);

我试图通过JAVA应用程序调用DLL文件中的函数。 我发现这个来源和一切都像一个魅力

但是,我想对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++代码。< /P>

#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;
    }
 }
}
#包括“simpledell.h”
#包括
#包括
使用名称空间std;
使用std::string;
名称空间simpledlns
{
字符串SimpleDell::giveCharGetChar(字符串a)
{
返回“abc”;
}
intsimpledell::giveIntGetInt(intA)
{
返回3*a;
}
void simpledell::simpleCall(void)
{
int x=3;
返回;
}
int simpledell::giveVoidPtrGetInt(void*param)
{
如果(参数!=0)
{
int*x=(int*)参数;
返回*x;
}
其他的
{
返回-1;
}
}
}

除此之外,如果我将字符串更改为char,它将返回韩语单词。如果我使用字符串,Java会出错。请提供帮助。

有关如何使用JNA从Java向C库发送字符串和从C库接收字符串的示例:


我相信JNA有特殊的实用程序来帮助将C字符串转换为Java字符串。你查过了吗?Hi@HovercraftFullOfEels我不知道有类似的东西,因为我查过调用dll函数返回类型字符串,结果不感兴趣。Java字符串和C/C++字符串是完全不同的动物,如果要将它们用作另一个,你需要进行转换。此外,字符是一个原始的,字符串是引用,另一个主要区别是:@ HoviCRAFFTLoopFeels:我把所有东西从字符串转换成字符,在C++和java中都得到了韩语单词。我知道JNI和JNA不完全一样,但是请看这里:也许这会给你一些建议。