A question about JNI programming

A

Allen

/********************* 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?
 
G

Gordon Beaton

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
 
A

Allen

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?
 
G

Gordon Beaton

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JNI Error in passing array 0
accessing objects in JNI 6
jni trouble 1
JNI and GetFieldID 12
JNI & C & EVENTS 2
CallBack JNI help 11
Get not primary type in a class with JNI 0
JNI Strings and Calling a Java Method 0

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top