I am writing a java twain driver using JNI and I have a C++ method that returns an object to the calling java method - I have paste the method below.
/*
* Class: com_trovare_twain_JTwain
* Method: selectSource
* Signature: ()Lcom/trovare/twain/Source;
*/
JNIEXPORT jobject JNICALL Java_com_trovare_twain_JTwain_selectSource
(JNIEnv * env, jclass obj)
{
jobject source = NULL;
Twain twain;
if(twain.selectSource())
{
jclass sourceClass = env->FindClass("com/trovare/twain/Source");
source = env->AllocObject(sourceClass);
SourceAdaptor sourceAdaptor(env,source);
source = sourceAdaptor.createFromSourceId(twain.getSourceId());
//output some debug - the source id
jmethodID getID = env->GetMethodID(sourceClass,GET_ID,GET_ID_SIG);
TW_INT32 id = env->CallIntMethod(source,getID);
printf("Source selected id = [%d]\n", twain.getSourceId()->Id);
}
if(source == NULL)
{
printf("returning NULL source\n");
}
return source;
}
When this method is called, I can see from my printf line that the source object is being created and populated as expected.
However in my Java code the returned object contains default values for all its fields.
In my calling java code I output the contents of the returned object and this debug is displaying before my C++ output so in my logs it looks like my C++ isn't getting executed immediately, but I suspect this is not the case. Either way I don't know why my returned object isn't being populated correctly when it is obvioulsy is prior to the return statement.
Help and guidance much appreciated.
FYI My Java code
JTwain jtwain = new JTwain();
Source source = jtwain.selectSource();
System.out.println(source.toString());