2014-01-23 36 views
0

我正在開發一個使用jsf,spring和hibernate的web應用程序。此應用程序是 類似於其他網站,如money.rediff.comfinance.yahoo.com。我們公司有 與實時數據提供商達成協議。他提供了一個exe file 這在安裝時會生成一個dll file,pdf文件列出了dll文件的方法和其他 許可證文件。如何工作實現JNA回調?

我用依賴沃克,發現這些方法裝飾有一些符號。我 正試圖通過以下方式在JNA的幫助下調用dll文件的方法。

這裏是我的代碼,

DllImplementation的.java

public interface CLibrary extends StdCallLibrary 
    { 
     CLibrary INSTANCE = (CLibrary) Native.loadLibrary("DeskApi", CLibrary.class, new 

HashMap() {{ 

       put("Initialise", "? 

[email protected]@@[email protected]@@Z"); 
       put("GetQuote","[email protected]@@[email protected]"); 
       put("DeleteQuote","[email protected]@@[email protected]"); 
       put("VersionInfo","[email protected]@@[email protected]"); 

       //Other functions 
       }}); 

    public int Initialise(String serialkey,CDeskApiCallback callBack); 
    public int GetQuote(String symbol, int periodicity, long lasttimeupdate, long 

echo); 
    public int DeleteQuote(String symbol, int periodicity); 
    public int VersionInfo(String versionOut); 

    } 




    public static void main(String argv[]) { 

      try{ 

       CDeskApiCallback callback = new CDeskApiCallback(); 

       String key = "[email protected]"; 

       int retValue = CLibrary.INSTANCE.Initialise(key,callback); 

       System.out.println("Initialise() ="+retValue); 




      } catch (UnsatisfiedLinkError e) { 

       e.printStackTrace(); 
      } 
     } 
} 

CDeskApiCallback.java

public class CDeskApiCallback { 


    public native int realtime_notify(String symbol, Pointer recent); 
    public native int quote_notify(String symbol, int interval, int nMaxSize, 

    Pointer quotes, long echo); 

} 

Quotation.java

public class Quotation extends Structure implements Structure.ByReference{ 
    public NativeLong DateTime; // 8 byte 
    public float Price; 
    public float Open; 
    public float High; 
    public float Low; 
    public float Volume; 
    public float OpenInterest; 

    public Quotation(){ 
     super(); 
     System.out.println("in Quotation()"); 
     read(); 
    } 

} 

RecentInfo.java

public class RecentInfo extends Structure implements Structure.ByReference{ 

    public float fOpen; 
    public float fHigh; 
    public float fLow; 
    public float fLast; 
    public float fTradeVol; 
    public float fTotalVol; 
    public float fOpenInt; 
    public float fPrev; 
    public float fBid; 
    public float fAsk; 
    public int  iBidSize; 
    public int  iAskSize; 
    public NativeLong DateTime; 

    public RecentInfo(){ 
     super(); 
     read(); 
     } 

} 

爲了當我執行的主要方法我得到一個-1作爲一個整數返回值測試代碼。從軟件提供商的pdf中可以看出它是一個錯誤。我怎樣才能實現回調機制。

我是JNA新功能。任何線索或幫助將非常可觀。


編輯:

@manuell

首先感謝您的幫助的。我根據建議進行了更改,但沒有用處。我正在粘貼由軟件提供商給出的頭文件。請建議...

#pragma once 
//version 0.0 Beta 1 
#define DAILY_PERIOD 24*60 
#define MIN_PERIOD 1 

#ifdef API_DLL 
#define METHOD_TYPE __declspec(dllexport) 
#else 
#define METHOD_TYPE __declspec(dllimport) 
#endif 
//data is provided in the struct below 
struct Quotation { 
         unsigned long DateTime; // 8 byte 
         float Price; 
         float Open; 
         float High; 
         float Low; 
         float Volume; 
         float OpenInterest;      
       }; 

struct RecentInfo 
{ 
    float fOpen; 
    float fHigh; 
    float fLow; 
    float fLast; 
    float fTradeVol; 
    float fTotalVol; 
    float fOpenInt; 
    float fPrev; 
    float fBid; 
    int  iBidSize; 
    float fAsk; 
    int  iAskSize; 
    unsigned long DateTime; // 8 byte 
}; 

//callback class which is to be implemented by the client application 
class METHOD_TYPE CDeskApiCallback 
{ 
    public: 
     /* 
     Description : Tick Update from the server for symbol requested 
     Parameters : 1. symbol of interest 
         2. Data, please note value -1 indicates no update. 
     Return  : 0 in case of success and -1 in case of error 
     */ 
    virtual int realtime_notify(const char* symbol, RecentInfo *pRecent)=0; 
     /* 
     Description : Vwap Update from the server for symbol requested 
     Parameters : 1. symbol of interest 
         2. update of interval requested 
         3. data size 
         4. data 
         5. user message 
     Return  : 0 in case of success and -1 in case of error 
     */ 
    virtual int quote_notify(const char* symbol, int interval, int nMaxSize, Quotation *pQuotes, unsigned long echo)=0;  
}; 

//this is the control class from which requests are initiated. 
class METHOD_TYPE CDeskApi 
{ 
public: 
    CDeskApi(void); 

     /* 
    Description : Initiates a connection to NEST system 
    Parameters : 1. serialkey provided to implement the api 
        2. object of CDeskApiCallback implemented 

    Return  : 0 in case of success and -1 in case of error 
     */ 
    int Initialise(char *serialkey, CDeskApiCallback* callback); 
     /* 
     Description : Request data from the server 
     Parameters : 1. symbol of interest 
         2. intervals of 1 min, multiples of 1 min, DAILY_PERIOD in case of daily. 
         3. data to be retrieved from. in no of seconds since epoch 
         4. identifier, which is returned in the callback   
     Return  : 0 in case of success and -1 in case of error 
     */ 

    int GetQuote(char * symbol, int periodicity, unsigned long lasttimeupdate, unsigned long echo); 
     /* 
     Description : Delete a Prior Request to the server 
     Parameters : 1. symbol of interest 
         2. interval, send -1 to delete all requested information of the symbol 
     Return  : 0 in case of success and -1 in case of error 
     */ 
    int DeleteQuote(char * symbol, int periodicity); 
     /* 
     Description : Delete a Prior Request to the server 
     Parameters : 1. symbol of interest 
         2. interval, send -1 to delete all requested information of the symbol 
     Return  : 0 in case of success and -1 in case of error 
     */ 
    int VersionInfo(char * versionout); 
    ~CDeskApi(void); 
}; 
+0

您不能只將Java String傳遞給C/C++函數。你的回調應該從'回調'繼承。請參閱http://stackoverflow.com/q/10158582/1374704和http://stackoverflow.com/q/11849595/1374704 – manuell

+0

我編輯了我的問題,請看看它。 –

+0

我不熟悉JNA。看來您確實可以使用Java String類來調用期望C char *參數的本地函數。不好意思告訴你,否則。 – manuell

回答

2

原生DLL是C++版本,導出類。

這意味着,例如,初始化方法是「*this」方法。

這也意味着回調參數(來自Initialize的第二個參數)應該是指向VTBL(因爲C++ CDeskApiCallback類是抽象類)的指針。

您可能能夠使用Java Pointer類型來處理這兩個問題,但這將是棘手的黑客攻擊。默認情況下,規則很明確:您不能在C++類上下文中使用JNA。

你只剩下3種選擇(加棘手的一個):

  1. 嘗試使用Bridj
  2. 嘗試使用jnaerator
  3. 自己寫的一個C++ DLL包裝和JNA使用它(或JNI)。

第三個涉及基本的C/C++知識和Visual Studio(Express)環境。