Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JNI C++ Function not being called immediately, or maybe not

843829Mar 16 2008 — edited Mar 17 2008
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());
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2008
Added on Mar 16 2008
3 comments
238 views