2017-08-02 103 views
-1

我想通过JAVA应用程序调用DLL文件内部的函数。 我发现此源码http://blog.mwrobel.eu/how-to-call-dll-methods-from-java/和一切工作像一个魅力。Java调用dll字符串返回类型方法

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

这是我的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; 
    } 
} 
} 

除此之外,如果我将字符串更改为字符,它会返回我韩文单词。如果我使用String,Java将会出错。请帮忙。

+0

我相信JNA有帮助转换C字符串到Java的特殊工具字符串。你看过这个吗? –

+0

嗨@HovercraftFullOfEels我不知道有这样的事情,因为我查找调用DLL函数返回类型字符串和结果不感兴趣。 –

+0

Java字符串和C/C++字符串是完全不同的动物,如果您打算使用另一个字符串,则需要转换。另外一个字符是字符串,而字符串是引用,另一个主要区别。 –

回答