JNI Strings and Calling a Java Method

K

Kalpesh Modha

Hello.

The problem.
Whats happens is this if I call a java method from the C++ with a string and
try to add to a vector or array I just get I null pointer in the java
method. If I remove a the call to put it in the array it works perfectly.
does anyone know what I going on here ?

I have pasted the code. If you require me a email the code let me know.

Many thanks for your help.

Kalpesh Modha

**************
test.java
public class test {

public static void main(String[] args)
{
highlight hw = new highlight();
hw.doSearch();
}
}
**************
highlight.java

class highlight {

public native void startSearch();
public native void findWindow(String title);

// load the dll that does the windows native stuff.
static
{
System.loadLibrary("highlight");
}


private String myStr;
private String[] str = null;
private int index = 0;


public highlight()
{
str = new String[20];

for (int i=0; i < 19; i++)
{
str = "";
}
}

public void doSearch()
{
this.startSearch();

/*for (int i=0; i < 19; i++)
{
System.out.println(str);
}*/
}

private void output(String mystr)
{
System.out.println("Output:" + mystr);
// remove this comment make to give null pointers str[index] = mystr;
index++;
}
}
**************
highlight.cpp

#include "highlight.h"
#include <windows.h>
#include <winuser.h>
#include <richedit.h>

static JavaVM *vmBuf = NULL;
static JNIEnv *pEnv = NULL;
static jclass cls;
static jmethodID mid;
static jstring kmStr;



// JVM independent entry point for getting the current JVM.
extern "C" {
typedef jint (JNICALL *pJNI_GetCreatedJavaVMs)(JavaVM **, jsize, jsize *);
}


// call back handler for the child window.
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{

return TRUE;
}

// JVM independent helper for getting the current JVM.
pJNI_GetCreatedJavaVMs getJavaVM()
{
// Use the entrypoint of the current VM.
HINSTANCE hMod = ::GetModuleHandle("msjava");
if(hMod == NULL)
hMod = ::GetModuleHandle("jvm");
if(hMod == NULL)
hMod = ::GetModuleHandle("javai");
pJNI_GetCreatedJavaVMs pFunc =
(pJNI_GetCreatedJavaVMs)::GetProcAddress(hMod,"JNI_GetCreatedJavaVMs");
return pFunc;
}




// callback handler for the main window.
BOOL CALLBACK EnumWindowsProcMain(HWND hwnd,LPARAM lParam)
{
// name of the window we have.
char name[255];

// this will connect the Java virtual machine and update the data. If we do
not
// connect to the virtual machine this way we can not give feedback


if (IsWindow(hwnd))
{
if ( IsWindowVisible(hwnd) && GetWindow(hwnd,GW_CHILD) != 0)
{

GetWindowText(hwnd,(LPTSTR) name, sizeof(name));
if (name[0] != '\0')
{
// throw the output to the method that needs it.
kmStr = pEnv->NewStringUTF(name);
pEnv->CallVoidMethod(cls,mid,kmStr);

// extract child windows
//EnumChildWindows(hwnd,(WNDENUMPROC) EnumChildProc,0);
}

}
}

return TRUE;
}

// start the search for all visible windows.
JNIEXPORT void JNICALL
Java_highlight_startSearch(JNIEnv *env, jobject obj)
{
jsize bufLen = 1;
jint nVMs = 0;



// Get the Java VM.
pJNI_GetCreatedJavaVMs pFunc = getJavaVM();
jint nRet = (*pFunc)(&vmBuf,bufLen,&nVMs);

// connect to the Java virtual machine.
if (vmBuf != NULL)
{
// Attach this thread.
vmBuf->AttachCurrentThread((void **)&pEnv,NULL);
}

// find the class that we need.
cls = pEnv->FindClass("highlight");

// get the method we need.
mid = pEnv->GetMethodID(cls, "output", "(Ljava/lang/String;)V");
pFunc = NULL;


EnumWindows( (WNDENUMPROC) EnumWindowsProcMain,0);

return;
}

// this will find the window that we need to extract data from.
JNIEXPORT void JNICALL Java_highlight_findWindow(JNIEnv *env, jobject obj,
jstring title)
{
// Detach this thread.
vmBuf = NULL;
cls = NULL;
mid = NULL;

env->DeleteLocalRef(cls);


char buf[128];
const char *str = env->GetStringUTFChars(title, 0);
printf("%s", str);
printf("\n");
env->ReleaseStringUTFChars(title, str);
}
**************

highlight.h file.
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class highlight */

#ifndef _Included_highlight
#define _Included_highlight
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: highlight
* Method: startSearch
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_highlight_startSearch
(JNIEnv *, jobject);

/*
* Class: highlight
* Method: findWindow
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_highlight_findWindow
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

****end of pasting.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top