call c++ function in Java with input and output ar

2019-03-03 12:07发布

问题:

I have a c++ code which has been connected to a visual basic user interface by someone else. Here is one of the functions code that connects c++ to 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();
    }
}

I want to replace the visual basic part with a Java GUI and it seems confusing for me to write the connection code. Is there anyone who can help me call three c++ functions with passing arguments to the native method and receiving results from it, by JNA/ SWIG/ Runtime or any other methods you think it would work easier and better?

回答1:

Here is an instructional example to help get you started. In this snippet, Java2Win64 is the DLL that contains the native code to execute. Function functionMaryam() takes 1 param as int and returns an int. Easy to expand for any data type. public class 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);
}
// ------------------------------------------


标签: jni swig jna