jna传递几个参数
jna passing several parameter
我正在尝试使用 JNA 使我的 .so 与 java 代码一起工作。
首先,我只是想在我的 .so (C++) 中的 cout<< 中获取我用 JAVA 给出的参数,但似乎只有第一项工作让我们看到我的代码
这是我的 .so 中的 .h
class WestgardInterface {
private:
static WestgardInterface *ms_instance;
protected:
/*CONSTRUCTOR*/
WestgardInterface();
public:
/*DESTRUCTOR*/
~WestgardInterface();
static WestgardInterface *getInstance();
void WGevaluate( Measure m, double mean, double sd);
};
extern "C" void WGevaluate( Measure m, double mean, double sd){
WestgardInterface* lib = WestgardInterface::getInstance();
return lib->WGevaluate( m, mean, sd);
}
然后是我的.cpp
WestgardInterface *WestgardInterface::ms_instance = nullptr;
/** @details Constructor*/
WestgardInterface::WestgardInterface(){
// TODO Auto-generated constructor stub
}
/** @details Destructor*/
WestgardInterface::~WestgardInterface() {
// TODO Auto-generated destructor stub
}
WestgardInterface *WestgardInterface::getInstance(){
if (nullptr == ms_instance)
{
ms_instance = new WestgardInterface();
}
return ms_instance;
}
void WestgardInterface::WGevaluate(Measure m, double mean, double sd) {
cout<<"measure"<<m.getValue()<<endl<<m.getDate();
cout<<"mean"<<mean<<endl<<"sd"<<sd<<endl;
}
现在让我们看看我的 java 代码
public interface InterfaceLibWestgard extends Library {
void WGevaluateWithListOnly(WGInputs.ByValue input);
static InterfaceLibWestgard INSTANCE = (InterfaceLibWestgard)
Native.loadLibrary("../logic/resources/calculator/libuntitled.so",
InterfaceLibWestgard.class);
}
WGInputs class
public class WGInputs extends Structure {
public Measure.ByValue m;
public double mean;
public double sd;
public static class ByValue extends WGInputs implements Structure.ByValue {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "m" , "mean" , "sd" }); // native variables order
}
}
最后是主要的
public class Westgard {
static {
System.setProperty("jna.library.path","../logic/resources/calculator");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
WGInputs.ByValue jnaInputs = new WGInputs.ByValue();
Measure.ByValue m = new Measure.ByValue();
m.valeur = 18;
Date.ByValue d = new Date.ByValue();
d.m_day = 10;
d.m_month = 10;
d.m_year = 10;
d.m_hour = 12;
d.m_min = 12;
d.m_sec = 12;
m.date = d;
jnaInputs.m = m;
jnaInputs.mean = 7.5;
jnaInputs.sd = 4d;
InterfaceLibWestgard.INSTANCE.WGevaluateWithListOnly(jnaInputs);
}
}
所以当 运行 它应该在终端上写这个
measure18
date = 10/10/10
heure = 12:12:12
mean 7.5
sd4
但我得到了这个
measure18
date = 10/10/10
heure = 12:12:12
mean0
sd3.51276e+151
最终发现只需要更改 java 一侧并将所有 ref 抑制为 mean 和 sd
然后像这样定义函数
void WGevaluateWithListOnly(WGInputs.ByValue 输入,浮动均值,浮动 sd);
我正在尝试使用 JNA 使我的 .so 与 java 代码一起工作。 首先,我只是想在我的 .so (C++) 中的 cout<< 中获取我用 JAVA 给出的参数,但似乎只有第一项工作让我们看到我的代码
这是我的 .so 中的 .h
class WestgardInterface {
private:
static WestgardInterface *ms_instance;
protected:
/*CONSTRUCTOR*/
WestgardInterface();
public:
/*DESTRUCTOR*/
~WestgardInterface();
static WestgardInterface *getInstance();
void WGevaluate( Measure m, double mean, double sd);
};
extern "C" void WGevaluate( Measure m, double mean, double sd){
WestgardInterface* lib = WestgardInterface::getInstance();
return lib->WGevaluate( m, mean, sd);
}
然后是我的.cpp
WestgardInterface *WestgardInterface::ms_instance = nullptr;
/** @details Constructor*/
WestgardInterface::WestgardInterface(){
// TODO Auto-generated constructor stub
}
/** @details Destructor*/
WestgardInterface::~WestgardInterface() {
// TODO Auto-generated destructor stub
}
WestgardInterface *WestgardInterface::getInstance(){
if (nullptr == ms_instance)
{
ms_instance = new WestgardInterface();
}
return ms_instance;
}
void WestgardInterface::WGevaluate(Measure m, double mean, double sd) {
cout<<"measure"<<m.getValue()<<endl<<m.getDate();
cout<<"mean"<<mean<<endl<<"sd"<<sd<<endl;
}
现在让我们看看我的 java 代码
public interface InterfaceLibWestgard extends Library {
void WGevaluateWithListOnly(WGInputs.ByValue input);
static InterfaceLibWestgard INSTANCE = (InterfaceLibWestgard)
Native.loadLibrary("../logic/resources/calculator/libuntitled.so",
InterfaceLibWestgard.class);
}
WGInputs class
public class WGInputs extends Structure {
public Measure.ByValue m;
public double mean;
public double sd;
public static class ByValue extends WGInputs implements Structure.ByValue {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "m" , "mean" , "sd" }); // native variables order
}
}
最后是主要的
public class Westgard {
static {
System.setProperty("jna.library.path","../logic/resources/calculator");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
WGInputs.ByValue jnaInputs = new WGInputs.ByValue();
Measure.ByValue m = new Measure.ByValue();
m.valeur = 18;
Date.ByValue d = new Date.ByValue();
d.m_day = 10;
d.m_month = 10;
d.m_year = 10;
d.m_hour = 12;
d.m_min = 12;
d.m_sec = 12;
m.date = d;
jnaInputs.m = m;
jnaInputs.mean = 7.5;
jnaInputs.sd = 4d;
InterfaceLibWestgard.INSTANCE.WGevaluateWithListOnly(jnaInputs);
}
}
所以当 运行 它应该在终端上写这个
measure18
date = 10/10/10
heure = 12:12:12
mean 7.5
sd4
但我得到了这个
measure18
date = 10/10/10
heure = 12:12:12
mean0
sd3.51276e+151
最终发现只需要更改 java 一侧并将所有 ref 抑制为 mean 和 sd 然后像这样定义函数 void WGevaluateWithListOnly(WGInputs.ByValue 输入,浮动均值,浮动 sd);