dcsimg
JNI + callback
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Laurent_Delrue
Posted On:   Friday, April 27, 2001 02:53 AM

Hi, I have a C++ dll (written with Borland C++ Builder) which contains a Form with on it a Timer object. On the other side, I have a Java class calling functions from that C++ dll. The purpose is : Java calls function A() from C++ Dll, Function A() starts the Timer, At given Timer intervals, the C++ dll does a callback in the Java class. The C++ dll Form is created in the JNI_OnLoad function. The problem: The Timer does not run at all, so no callback is performed. I think that in fact it stops as soon function A() exits. I've remarked this when un error occurred in A(), causing a Message Box to appear and waiting to c   More>>

Hi,

I have a C++ dll (written with Borland C++ Builder) which contains a
Form with on it a Timer object.
On the other side, I have a Java class calling functions from that
C++ dll.

The purpose is :

Java calls function A() from C++ Dll,
Function A() starts the Timer,
At given Timer intervals, the C++ dll does a callback in the Java
class.


The C++ dll Form is created in the JNI_OnLoad function.

The problem: The Timer does not run at all, so no callback is
performed.
I think that in fact it stops as soon function A() exits. I've
remarked this when un error occurred in A(), causing a Message Box to
appear and waiting to click the OK button, the Timer was running.

Some code ::

Header file :

			
//-------------------------------------------------------------------

#ifndef CallBackH
#define CallBackH
//-------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
//-------------------------------------------------------------------
class TCallBackForm : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
void __fastcall Timer1Timer(TObject *Sender);
private: // User declarations
public: // User declarations
JavaVM *cached_jvm;
jobject Loader;
jclass Class_Test;
jmethodID MID_Test_notify;
__fastcall TCallBackForm(TComponent* Owner);
void __fastcall enableTimer();
};

extern "C" JNIEXPORT void JNICALL Java_Test_initCppSide(JNIEnv *env,
jobject obj);
//-------------------------------------------------------------------
extern PACKAGE TCallBackForm *CallBackForm;
//-------------------------------------------------------------------
#endif


Some functions in the cpp file ::
			
//The A() function I was talking about :
JNIEXPORT void JNICALL Java_Test_initCppSide(JNIEnv *env, jobject
obj) {

// Enable Timer
CallBackForm->Timer1->Enabled = true;
printf("Timer enabled
");
}


void __fastcall TCallBackForm::Timer1Timer(TObject *Sender)
{
printf("Timer is running
");
}
//-------------------------------------------------------------------

The line "Timer is running" never appears.

Any ideas what I am doing wrong here ??

Kind regards,
Laurent.    <<Less

Re: JNI + callback

Posted By:   Jacob_Marner  
Posted On:   Sunday, April 29, 2001 03:11 PM

The problem is that to recieve Windows callback you need to have a GUI thread in C++ to listen for messages and then send to event to Java.


Since your code is running in the Java GUI all the time except when starting the timer the timer has no chance of telling your program that it runs.


What you need to do is to make a thread run in the Windows message loop and one in the Java GUI. This is no simple thing to do, but fortunately I have written a program that allows you to do these things.


Check out
http://www.rolemaker.dk/JWindows/


This will allow you to make native Windows programs in Java and thereby make both Windows timers and forms. (One of the included samples does just that)


If you are interested in achieving this yourself the code is open source so you can see how it is done.


Jacob Marner

About | Sitemap | Contact