2017-08-02 99 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++字符串是完全不同的動物,如果您打算使用另一個字符串,則需要轉換。另外一個字符是字符串,而字符串是引用,另一個主要區別。 –

回答