2013-07-15 3 views
1

하나의 C++ dll 파일이 있습니다. 그리고 나는 그것에서 사용 된 방법을 안다. 내 Java 코드에서 이러한 메서드를 호출해야합니다. DLL 파일을 수정할 권한이 없습니다. 이를위한 솔루션을 제공해주십시오.Java에서 C++ 라이브러리 (DLL) 메소드에 액세스하는 방법

+0

http://stackoverflow.com/questions/14706193/how-to-access-a-method-of-c-library-dll-from-java – Reddy

+0

Google에 'JNI'를 추가 할 수도 있습니다. – alexbuisson

+0

다른 언어에서 dll로 내 보낸 C++ 메서드를 호출 할 수 없습니다. C++에는 신뢰할 수있는 방식으로 이것을 허용하는 표준화 된 ABI가 없습니다. C++ 메소드를 C 인터페이스로 '평면화'해야합니다. – greatwolf

답변

2

나는 정확히 그 목적으로 JavaCPP을 만들었습니다. 나는/복사 페이지에서 일부 샘플 코드와 설명을 붙여 넣을 수 있습니다 :

가장 일반적인 사용 사례는이 C를 포함 LegacyLibrary.h라는 이름의 파일 내부에, 예를 들어, C++ 용으로 작성된 일부 기존 라이브러리를 액세스 포함 ++ 클래스 :

#include <string> 

namespace LegacyLibrary { 
    class LegacyClass { 
     public: 
      const std::string& get_property() { return property; } 
      void set_property(const std::string& property) { this->property = property; } 
      std::string property; 
    }; 
} 

는 작업이 우리가 쉽게이 하나로 자바 클래스를 정의 할 수 있습니다, JavaCPP으로 수행하려면 - 아래의 입증 된 바와 같이 하나의 헤더 파일을 생성하는 파서를 사용할 수 있지만 :

import com.googlecode.javacpp.*; 
import com.googlecode.javacpp.annotation.*; 

@Platform(include="LegacyLibrary.h") 
@Namespace("LegacyLibrary") 
public class LegacyLibrary { 
    public static class LegacyClass extends Pointer { 
     static { Loader.load(); } 
     public LegacyClass() { allocate(); } 
     private native void allocate(); 

     // to call the getter and setter functions 
     public native @StdString String get_property(); public native void set_property(String property); 

     // to access the member variable directly 
     public native @StdString String property();  public native void property(String property); 
    } 

    public static void main(String[] args) { 
     // Pointer objects allocated in Java get deallocated once they become unreachable, 
     // but C++ destructors can still be called in a timely fashion with Pointer.deallocate() 
     LegacyClass l = new LegacyClass(); 
     l.set_property("Hello World!"); 
     System.out.println(l.property()); 
    } 
} 

또는 Java 인터페이스를 생성 할 수 있습니다.

@Properties(target="LegacyLibrary", [email protected](include="LegacyLibrary.h")) 
public class LegacyLibraryConfig implements Parser.InfoMapper { 
    public void map(Parser.InfoMap infoMap) { 
    } 
} 

을 그리고 빌드 다음 명령 : 등이 하나 같이 구성 클래스 헤더 파일을 구문 분석 메이븐/IDE 통합을 포함하여 더 복잡한 예를 들어

$ javac -cp javacpp.jar LegacyLibraryConfig.java 
$ java -jar javacpp.jar LegacyLibraryConfig 
$ javac -cp javacpp.jar LegacyLibrary.java 
$ java -jar javacpp.jar LegacyLibrary 

JavaCPP Presets을 체크 아웃!

+0

위대한 직업! 나는 당신의 도구 인 JavaCPP가 전에 비슷한 문제를 겪었던 것을 알게되어 매우 기쁩니다. 나는 그것을 배우는 데 시간을 할애 할 것이다! 고맙습니다! –

+0

BTW,이 DLL의 세부 사항을 모르는 자바 코드에서 직접 JavaCPP가 DLL 파일의 함수를 호출 할 수 있습니까? –

+0

@AnnieKim 최소한 헤더 파일과 C++ 컴파일러가 필요합니다. 그러나 그 외 모든 것은 꽤 자동적입니다. –

관련 문제