Re: Allocating an array of pointers



In article <de5huv$s4j$1@xxxxxxxxxxxxxxxxxxxx>,
lkd1@xxxxxxxxxxx (Lin DeNoyer) wrote:

> More info about what I need to do:
>
>
> Yes, I am trying to call a native (C) function.
> I AM calling a native function, and I know about the JNI interface.
>
> However, that function needs an array - an array that the function will
> fill with pointers.
>
> The C function allocates a bunch of "things" (actually structures).
> It doesn't do all the necessary work on these "things" in the first
> pass, because the processing takes a long time. So it processes
> along merrily until it is time to share the processor, then it
> returns to the calling program. When the calling program is ready,
> it calls this C function again, and the function merrily chunks doing its
> thing. It uses those pointers to know where the data is that it is working
> on. And so on. Until the C function is done, and it passes back the
> results.
>
> LKD

I've got some JNI code used for licensing in our application that
accompanies a Java class with a member variable of type "int" which I've
found to be compatible with pointers from my C code. Maybe an idea from
that can help here.

Here's the code I use:


// Get the "Class" of License
cls = (*env)->GetObjectClass(env, obj);
// Get the fieldID of the "licenseJob" member variable
fid = (*env)->GetFieldID(env, cls, "licenseJob", "I");
if (fid == 0) {
// This is a problem...
// So check it back in and report an error
lc_checkin(job, LM_CI_ALL_FEATURES, 0);
lc_free_job(job);
return 888;
}
// Put the "job" object into the "licenseJob" object for reference
(*env)->SetIntField(env, obj, fid, (jint)job);


In this code, "env" is the JNIEnv pointer that's part of every JNI
function, and "obj" is the jobject which is also there identifying the
containing object of which the JNI method is a native member -- in other
words, these are the first two parameters to the JNI function. The int
member variable I referred to is named "licenseJob".

The variable "cls" is a jclass in my JNI function, and "fid" is a
jfieldID. I use the GetObjectClass function to get access to my Java
object's Class object. Then I use the GetFieldID call to get the field
id of the "licenseJob" variable. The variable "job" is a pointer
returned earlier when I checked out a license from the license manager,
and you can see where I check it back in if my "fid" isn't good. To
wrap that up, I put the "job" pointer into the "licenseJob" variable by
calling SetIntField, so that it now carries a pointer.

The entire purpose for that int member variable is to be accessed and
used only in my JNI code, so I never touch it in my Java code except for
its declaration as a private member variable.

To extend this, you could possibly have a variable of type int[] in your
Java code, so that each entry could contain a single pointer. There
doesn't seem to be any logical reason why this can't work. It
essentially lets you have a data structure defined and carried in your
class that's actually used and manipulated in your C code.

HTH.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
.