C++ Object 到 java,将 object 传递到 java 后属性不正确(int,double)

C++ Object to java, attributes are not correct after passing the object to java (int, double)

我想将 DLL 的 C++ classobject 导出到 java。问题是,在我将 object 导出到 java 之后,所有 属性 值(如 int 和 double)在 java 中都不正确 - 为什么会这样?我怎样才能防止这种情况发生以使其正常工作?这是我的意思的一些截图。

以下日志的第一段是从我的 C++ DLL 中打印出来的。第二段是从我的 java 程序中打印出来的。

第一次执行我的程序

第二次执行我的程序

如您所见,C++ 中我的人类 object 和 java 的所有值都不相同。 age 属性 的值在我的 java class 执行后每次都会改变,但是 java 中的属性 SalaryInt 和 SalaryDouble 在执行时会一致地改变。在调试时,我可以看我的 object 大约一秒钟。而这一秒,object 看起来像这样:

"second"之后的样子如下图:

在这里你可以看到我的代码。

HumanBean.java

package at.xxxx.xxxxx;

public class HumanBean {

    String name;
    int alter;
    int gehalt;
    double gehaltDouble;

    public HumanBean(String name, int alter, int gehalt, double gehaltDouble) {
        super();
        this.name = name;
        this.alter = alter;
        this.gehalt = gehalt;
        this.gehaltDouble = gehaltDouble;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAlter() {
        return alter;
    }
    public void setAlter(int alter) {
        this.alter = alter;
    }
    public int getGehalt() {
        return gehalt;
    }
    public void setGehalt(int gehalt) {
        this.gehalt = gehalt;
    }
    public double getGehaltDouble() {
        return gehaltDouble;
    }
    public void setGehaltDouble(double gehaltDouble) {
        this.gehaltDouble = gehaltDouble;
    }


    @Override
    public String toString() {
        return "HumanBean [name=" + name + ", alter=" + alter + ", gehalt=" + gehalt + ", gehaltDouble=" + gehaltDouble
                + "]";
    }
}

Controller.java (现在我不会 post 本机方法声明,我调用的唯一方法是 erstelleHuman)

import at.xxxx.xxxx.HumanBean;

public class Controller {

    public static void main(String[] args) {

        System.out.println("library: "
                + System.getProperty("java.library.path"));

        HumanController humanController = new HumanController();

        HumanBean tati = HumanController.erstelleHuman();
        System.out.println("----------------------  erstelleHuman -----------------------");
        System.out.println("Tati Age = " + tati.getAlter());
        System.out.println("Tati SalaryInt = " + tati.getGehalt());
        System.out.println("Tati SalaryDouble = " + tati.getGehaltDouble());

    }

}

我的包装器.cpp-File

#pragma comment(lib, "Human.lib")
#include "HumanHeader.h"
#include "HumanController.h"
#include <iostream>

/*
 * Class:     HumanController
 * Method:    erstelleHuman
 * Signature: ()Lat/xxxx/calculatorhuman/HumanBean;
 */
JNIEXPORT jobject JNICALL Java_HumanController_erstelleHuman(JNIEnv *env, jclass obj) {
    Human exportierterHuman = Human::erstelleHumanExport(45, "Dragan Stankovic", 1769.34, 987563);

    jclass cls = (*env).FindClass("at/xxxxx/calculatorhuman/HumanBean");
    //jclass cls = (*env).FindClass(env, "at/xxxxx/calculatorhuman/HumanBean");

    jmethodID cid = (*env).GetMethodID(cls, "<init>", "(Ljava/lang/String;IID)V");
    std::cout << "------------------------Java_HumanController_erstelleHuman ----------------" << endl;
    std::cout << "exportierterHuman Objekt c++ Name " << exportierterHuman.name << endl;
    std::cout << "exportierterHuman Objekt c++ Alter " << exportierterHuman.alter << endl;
    std::cout << "exportierterHuman Objekt c++ GehaltInt " << exportierterHuman.gehaltInt << endl;
    std::cout << "exportierterHuman Objekt c++ GehaltDouble " << exportierterHuman.gehaltsDouble << endl;
    return (*env).NewObject(cls, cid, exportierterHuman.name, exportierterHuman.alter, exportierterHuman.gehaltInt, exportierterHuman.gehaltsDouble);

}

.cpp-File,正在被我包着

#include "HumanHeader.h"
#include <iostream>

Human::Human(int x, string y, double z, int g) {
    alter = x;
    name = y;
    gehaltsDouble = z;
    gehaltInt = g;
}

 Human Human::erstelleHumanExport(int alter, string name, double gehaltDouble, int gehaltInt) {
     return Human(alter, name, gehaltDouble, gehaltInt);
 }

Header .cpp-file 被包裹

#pragma once
#include <iostream>

using namespace std;
    class Human
    {
    public:

        int alter;
        string name;
        bool volljaehrig;
        double gehaltsDouble;
        int gehaltInt;

        Human(int x, string y, double z, int g);


        // funktion mit dllExport
        static __declspec(dllexport) void triggerGeburtstagExport(Human human);
        static __declspec(dllexport) bool isVolljaehrigExport(Human human);
        static __declspec(dllexport) void gebeGehaltAusExport(Human human);
        static __declspec(dllexport) Human erstelleHumanExport(int alter, string name, double gehaltsDouble, int gehaltInt);


    };

感谢@macinj,我让它工作了。我只是改变了

return (*env).NewObject(cls, cid, exportierterHuman.name, exportierterHuman.alter, exportierterHuman.gehaltInt, exportierterHuman.gehaltsDouble);

到以下代码片段 ->

jstring name = (*env).NewStringUTF(exportierterHuman.name.c_str());
return (*env).NewObject(cls, cid, name, exportierterHuman.alter, exportierterHuman.gehaltInt, exportierterHuman.gehaltsDouble);