Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JNI char * in c -- to -- jcharArray to -- char[] in Java


  • From: David_Miller@xxxxxxxxx (David_Miller@xxxxxxxxx)
  • Subject: JNI char * in c -- to -- jcharArray to -- char[] in Java
  • Date: Tue, 29 Feb 2000 09:29:49 -0500

The following is an excerpt from some code I have that reads a named pipe and
places the
result in a byte array.  To use chars is a little more complicated because each
char is 16 bits
long.   Instead of the GetByteArrayElements() you would use
GetCharArrayElements().
JNIEXPORT jint JNICALL Java_..._npRead
  (JNIEnv *jne, jobject, jint hPipe, jbyteArray bfr, jint nOffset, jint nLength)
{
     BOOL bRet;
     DWORD nBytesRead;
      jboolean isCopy;

      // get the new pipe name in a non-null terminated string
     BYTE *pBuf = (*jne).GetByteArrayElements(bfr, &isCopy);

     // Modify pBuf at this point.

      (*jne).ReleaseByteArrayElements(bfr, pBuf, 0);

     return (long) nBytesRead;
}

To return the jchararray, generate your char * and then convert to jstring using
NewStringUTF().  Then get jchar * using GetStringChars().  Create the new array
using NewCharArray() and copy from the above into its jchar * using
GetCharArrayElements().
Don't forget to release all unneeded references in the above process or you will
have a memory leak.

Kinda complicated but ought to work.  There probably is a better way to do this,
 but this
was off the top of my head.

David Miller




Suresh Sankar <sureshsankar@xxxxxxxxxxxx> on 02/29/2000 02:38:32 AM

To:   "Advanced Java (E-mail)" <advanced-java@xxxxxxxxxxxxxxxx>
cc:    (bcc: David Miller/GV/Kyrus)
Groups:   Don't Expand
Subject:  JNI char * in c -- to -- jcharArray to -- char[] in Java



We have a C function which generate a [ char *] .
We want to receive this [char *] in java method as { char[] }

suppose the java native method looks like

public native char[] SessionKeyGenerate(String passphrase);

the [ .h ] file

gives me the function declaration as

JNIEXPORT jcharArray JNICALL Java_.._.._.._.._SessionkeyGenerate(JNIEnv
*,jobject, jchararray);

how do i  convert a char * to a jcharArray so that it can be returned to
the Java function.

Please give pointers to some good jni tutorials.

Thanks
Suresh Sankar
Technology Communication Systems (I) Pvt Ltd.
New Delhi
INDIA

---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx





---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx