简体   繁体   中英

jna passing several parameter

I'm trying to make my .so works with a java code by using JNA. in a first place i'm just trying to get the parameters that i give with JAVA in a cout<< in my .so (C++) but it seems only the first work lets see my code

here is my .h in my .so

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);
}

then my .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;
}

now let's see my java code

public interface InterfaceLibWestgard extends Library {

    void WGevaluateWithListOnly(WGInputs.ByValue input);

    static InterfaceLibWestgard INSTANCE = (InterfaceLibWestgard) 
Native.loadLibrary("../logic/resources/calculator/libuntitled.so", 
InterfaceLibWestgard.class);
}

the 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
    }

}

and finally the main

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);

    }
}

so when a run it it should write this on the terminal

measure18
date = 10/10/10
heure = 12:12:12
mean 7.5
sd4

but instead i got this

measure18
date = 10/10/10
heure = 12:12:12
mean0
sd3.51276e+151

最后找到需要更改Java端并禁止所有ref表示均值和sd,然后定义像这样的函数WGevaluateWithListOnly(WGInputs.ByValue input,float mean,float sd);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM