Newbie question about connecting C++ with Java with JNI

Discussion in 'C++' started by wignas@gmail.com, Nov 17, 2006.

  1. Guest

    Hi all, I have a question and would really appreciate any help with
    this.

    So, I have connected a Java class and a Visual Studio C++ Dll-project
    using JNI:


    #include <windows.h>
    #include <string.h>
    #include "prog1.h" //The machine generated header file
    #include "Logic.h" //I want to use the methods in this header


    BOOL WINAPI DllMain(HANDLE hHandle, DWORD dwReason, LPVOID lpReserved)
    {
    return TRUE;
    }

    JNIEXPORT jint JNICALL Java_prog1_Sum(JNIEnv *, jclass, jint a, jint b)
    {
    //TestJava(); //This wont work :(
    return a + b;
    }

    JNIEXPORT jstring JNICALL Java_prog1_saySomething(JNIEnv * env, jclass,
    jstring strString)
    {
    char *lpBuff = (char*)env->GetStringUTFChars(strString, 0);
    _strupr(lpBuff);
    jstring jstr = env->NewStringUTF(lpBuff);
    env->ReleaseStringUTFChars(strString, lpBuff);
    return jstr;
    }


    I have tested to use this JNI-methods from Java and it works fine, but
    now I want call my old C/C++ methods in "Logic.h" from this JNI
    interface. How do I do that? Say I have a simple method in Logic.c like
    this:

    extern void TestJava()
    {
    CAN_DATA_WriteConvX myConv1Settings;
    myConv1Settings.Motor0 = MotorOn;
    outputConv1(myConv1Settings);
    }

    I can't call this from my JNIEXPORT methods, but I can call it from any
    other "ordinary" cpp method. So how should I do for using the old
    methods in Logic.h from Java?


    Thanks for any help. /Jonas
    , Nov 17, 2006
    #1
    1. Advertising

  2. On 17 Nov 2006 00:49:13 -0800, wignas@...com wrote:
    >So, I have connected a Java class and a Visual Studio C++ Dll-project
    >using JNI:


    Questions about JNI and VS-Dlls are not really on topic in a pure C++
    news group.

    >I can't call this from my JNIEXPORT methods, but I can call it from any
    >other "ordinary" cpp method. So how should I do for using the old
    >methods in Logic.h from Java?


    Use the 'ordinary' functions. The functions generated for JNI are just
    wrappers for the 'old methods' to make them callable from Java.

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Nov 17, 2006
    #2
    1. Advertising

  3. If you want, you can use SWIG to generate a c++ wrapper for Java.
    Stephane Wirtel, Nov 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    835
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    890
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,660
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    606
    vasanth
    Jan 25, 2005
  5. Replies:
    5
    Views:
    548
Loading...

Share This Page