Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

A question about JNI programming

Thread view: 
Allen - 11 Dec 2006 08:24 GMT
/********************* 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?
Gordon Beaton - 11 Dec 2006 08:56 GMT
> 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

Signature

[ 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

Allen - 11 Dec 2006 10:14 GMT
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?
Gordon Beaton - 11 Dec 2006 10:34 GMT
> 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

Signature

[ 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.