用输入和输出参数调用Java中的c ++函数

时间:2013-03-27 14:51:28

标签: java-native-interface swig jna

我有一个c ++代码,已被其他人连接到可视化基本用户界面。这是将c ++连接到visual basic的函数代码之一:

   extern "C" void PASCAL EXPORT RCS( stAct* act,stResourceDirectory* resDir,   stCalendar* calendar, short  numOfAct, short numOfRes, short numOfCal, int nDataDate )
{
Network network;
short id;
Activity* p_act;
node<Activity>* p_node;

    // Setting
    network.create_calendars (calendar, numOfCal);
    network.set_data_date (nDataDate);
    set_activity(network, act, numOfAct );
// only for id, duration, and description   
    set_resource(network, act, resDir, numOfAct, numOfRes);
// create resource profile and add required resource for every activity

    network.CPM ();

    p_node = network.get_network_head_p();  

    while (p_node != NULL ) {
    p_act = p_node->refer_data();
    id = p_act->get_ID ();

    act[id].TF_in_CPM = p_act->get_TF_min ();
    act[id].FF_in_CPM = p_act->get_FF();
    act[id].EST_in_CPM = p_act->get_EST ();
    act[id].EFT_in_CPM = p_act->get_EFT ();
    act[id].LST_in_CPM = p_act->get_LST ();
    act[id].LFT_in_CPM = p_act->get_LFT ();
    p_node = p_node->get_link();
    }
    network.RCS();  
p_node = network.get_network_head_p();  

while (p_node != NULL ) {
    p_act = p_node->refer_data();
    id = p_act->get_ID ();

    act[id].TF_in_RCS = p_act->get_TF_min ();
    act[id].FF_in_RCS = p_act->get_FF();
    act[id].EST_in_RCS = p_act->get_EST ();
    act[id].EFT_in_RCS = p_act->get_EFT ();
    act[id].LST_in_RCS = p_act->get_LST ();
    act[id].LFT_in_RCS = p_act->get_LFT ();

    p_node = p_node->get_link();
    }
}

我想用Java GUI替换visual basic部分,编写连接代码似乎很困惑。是否有人可以帮助我调用三个c ++函数,将参数传递给本机方法并从中接收结果,通过JNA / SWIG / Runtime或您认为它将更容易和更好地工作的任何其他方法?

1 个答案:

答案 0 :(得分:0)

这是一个帮助您入门的教学示例。在此代码段中,Java2Win64是包含要执行的本机代码的DLL。函数functionMaryam()将1个参数作为int并返回一个int。易于扩展,适用于任何数据类型。 公共课JnaExampleMaryam {

// ------------------------------------------
// Java2Win.class
// ------------------------------------------
public interface Java2Win extends Library {
    Java2Win call = (Java2Win) Native.loadLibrary("Java2Win64", Java2Win.class);
    int functionMaryam(int i);
}
// ------------------------------------------

// ------------------------------------------
// Test
// ------------------------------------------
public static void main(final String args[]) throws Exception {
    final File file = new File("rootToDLL", "Java2Win64.dll");
    LibraryLoader.loadLibrary(file);

    int result = Java2Win.call.functionMaryam(42);
}
// ------------------------------------------