我正在开发一个使用jsf,spring和hibernate的web应用程序。此应用程序是 类似于其他网站,如money.rediff.com
和finance.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);
};
您不能只将Java String传递给C/C++函数。你的回调应该从'回调'继承。请参阅http://stackoverflow.com/q/10158582/1374704和http://stackoverflow.com/q/11849595/1374704 – manuell
我编辑了我的问题,请看看它。 –
我不熟悉JNA。看来您确实可以使用Java String类来调用期望C char *参数的本地函数。不好意思告诉你,否则。 – manuell