A question about JNI programming

Discussion in 'Java' started by Allen, Dec 11, 2006.

  1. Allen

    Allen Guest

    /********************* JAVA implementation

    public class LogMesg {

    public String loggerName;

    public int length;

    public byte[] buf;
    }

    public class LoggerAdapter {

    public static native int readMesg(LogMesg logMesg);

    static {

    System.loadLibrary("demo");
    }
    }

    /*********************************** C++ implementation
    JNIEXPORT jint JNICALL Java_jni_logger_LoggerAdapter_readMesg
    (JNIEnv *env, jclass cls, jobject obj)
    {
    // Lookup the String field ('loggerName') in 'obj'
    jfieldID strLoggerNameFieldId = env->GetFieldID(cls, "loggerName",
    "Ljava/lang/String");
    if (strLoggerNameFieldId == 0)
    {
    printf("\nField [loggerName] not found");
    return -1;
    }
    }

    The program runs error saying that Field [loggerName] not found.
    I think cls is an instance of LoggerAdapter, not a LogMesg.
    But how to get loggerName field in this situation?
     
    Allen, Dec 11, 2006
    #1
    1. Advertising

  2. On 11 Dec 2006 00:24:14 -0800, Allen wrote:
    > I think cls is an instance of LoggerAdapter, not a LogMesg.
    > But how to get loggerName field in this situation?


    The first two arguments received by the native method are always env
    and this (for instance methods), or env and "this class" (for static
    methods). Remaining arguments correspond to those mentioned explicitly
    in the native declaration, in this case an instance of LogMsg.

    So of cource cls in this case refers to LoggerAdapter, since the
    method is a static method belonging to that class.

    To look up methods in LogMsg, you need to start by obtaining a
    reference to the LogMsg class. Since you've passed a LogMsg object to
    the method, just pass that to GetObjectClass().

    If you hadn't passed an instance of LogMsg, you could have used
    FindClass().

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 11, 2006
    #2
    1. Advertising

  3. Allen

    Allen Guest

    Thank you.

    Yes, I can get logMesg fields now.

    But there rises another question.

    I use map file (i.e. CreateFileMapping & MapViewOfFile) to share data.
    Testing as three individual Win32 EXE, it is OK for map file R/W. But
    if I access the map file via JNI, it cannot read a single byte! Why?
     
    Allen, Dec 11, 2006
    #3
  4. On 11 Dec 2006 02:14:50 -0800, Allen wrote:
    > I use map file (i.e. CreateFileMapping & MapViewOfFile) to share data.
    > Testing as three individual Win32 EXE, it is OK for map file R/W. But
    > if I access the map file via JNI, it cannot read a single byte! Why?


    Probably you're doing something wrong.

    A more specific answer is impossible without knowing exactly what
    you're doing in both cases, and exactly what it is that fails.

    Off hand this doesn't really sound like a JNI problem.

    CreateFileMapping and MapViewOfFile seem to be Windows API's, so
    probably someone else will have to answer anyway.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 11, 2006
    #4
    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:
    866
    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:
    928
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,733
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    630
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    850
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page