Hi!

I´m newbie in C++ Code and JNI, so i have a big problem and i can´t found a solution.

My question is: Is possible to call a Jni method in a normal ,method, so:

I have in my c++ class, two method:

void CVentanasDlg::OnButtonStart(){
    printf("OnButtonStart");
  }

JNIEXPORT void JNICALL Java_JniComm_openDevice(JNIEnv *env, jobject obj){
       m_DeviceID = StartUp( GetConsoleHwnd2(), WM_VX);
  }

Can i call from the method OnButtonStart to the jni method openDevice? if the response is yes, what are the arguments that i need to send to the second function?

Thanks!

Recommended Answers

All 2 Replies

I'm not sure what you're after here, your post isn't worded particularly clearly. But if I understand correctly, and from looking at your code, I think you may have got things a little mixed up here.

The OnButtonStart() function is definitely a C++ function. So no real problems there (other than the use of printf!). But the JNIEXPORT line defines a C++ function which can only be called from Java using JNI (at least as far as I can tell! I could be wrong!).

So are you saying you want to be able to call the C++/JNI function from OnButtonStart(), rather than calling it from Java? If so you might want to consider moving/refactoring the C++ code in the JNI function into a new function and then calling that in the JNI function AND in OnButtonStart.
e.g.

// make this a public static member of 
// whatever class m_DeviceID is a member of
void yourclass::SetDeviceID()
{
    m_DeviceID = StartUp( GetConsoleHwnd2(), WM_VX);
}

Then in your dialog code:

void CVentanasDlg::OnButtonStart(){
std::cout << "OnButtonStart");
yourclass::SetDeviceID(); // call your new function here!
}

And likewise in the JNI function:

JNIEXPORT void JNICALL Java_JniComm_openDevice(JNIEnv *env, jobject obj){
yourclass::SetDeviceID(); // Call the new function here...
}

This would allow the functionality inside the SetDeviceID() function to be called from Java via the JNI function and directly from OnButtonStart.

BTW: The above snippets assume that the new function is a public static function in an external class called yourclass. But depending on where you put the refactored function and how you set it up will depend on how you call it!

Refactoring like this would probably be the easiest way to achieve what you are trying to do... Or at least what I think you're trying to do!

Thanks a lot! That´s exatly that i want!

Thanks again Jason!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.