Monday, December 15, 2014

An Introduction to JNI using a Bottom Up Approach: Step 2

Continued from An Introduction to JNI using a Bottom Up Approach: Step 1. Note: These steps may be followed without the first step below, if you remove the two references to PrintOpenWindows in the C++ code and remove "PrintOpenWindows.cpp" from the commands to compile and run "CPPHelper.exe".

Step 2: C++ Main Function

  1. Comment out the main function in "PrintOpenWindows.cpp".
    /*  Uncomment to compile and run PrintOpenWindows.exe 
    int main()
    {
        cout << "PRINT_OPEN_WINDOWS ENTRY START" << endl;
        printOpenWindowsEntryPoint();
        cout << "PRINT_OPEN_WINDOWS ENTRY FINISH" << endl;
    }
    */
  2. In your working directory, add an empty file, named "CPPHelper.h"
    #ifndef _CPP_HELPER_H
    #define _CPP_HELPER_H

    #ifdef __cplusplus
            extern "C" {
    #endif
            void cppHelperEntryPoint();
    #ifdef __cplusplus
            }
    #endif

    #endif
  3. In your working directory, add the following code, in a file named "CPPHelper.cpp":
    #include "CPPHelper.h"
    #include "PrintOpenWindows.h" // Comment to ignore PrintOpenWindows
    #include  <iostream>

    using namespace std;

    void cppHelperExitPoint()
    {
        cout << "CPP_HELPER EXIT START" << endl;
        printOpenWindowsEntryPoint(); // Comment to ignore PrintOpenWindows
        cout << "CPP_HELPER EXIT FINISH" << endl;
    }

    int main()
    {
        cout << "CPP_HELPER MAIN START" << endl;
        cppHelperExitPoint();
        cout << "CPP_HELPER MAIN FINISH" << endl;
    }

    void cppHelperEntryPoint()
    {
        cout << "CPP_HELPER ENTRY START" << endl;
        cppHelperExitPoint(); // Comment to ignore PrintOpenWindows
        cout << "CPP_HELPER ENTRY FINISH" << endl;
    }
  4. Compile the code and run "CPPHelper.exe", using the following commands:
    cd /D "C:\Users\Scott\workspace\myrepo\my-app\src\main\resources\dlls"

    "C:\MinGW64\bin\g++.exe" -o CPPHelper.exe CPPHelper.cpp PrintOpenWindows.cpp

    CPPHelper.exe
Continued at An Introduction to JNI using a Bottom Up Approach: Step 3.

This post was reposted from http://scottizu.wordpress.com/2013/08/21/an-introduction-to-jni-using-a-bottom-up-approach-step-2/, originally written on August 21st, 2013.

No comments:

Post a Comment