Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trouble with NewStringUTF

843829Apr 6 2007 — edited Apr 12 2007
I am using a dll and JNI ... and getting ACCESS VIOLATION EXCEPTION ... so I'm doing something wrong.

When I test the C code, it seems to work... so my problem is somewhere between adding the JNI and pointers/jstrings.

Here's a piece of it..
#include <windows.h>
#include "jni.h">
.....

#define BUFFER_SIZE 32767

char* getComputerName(void);


char* getComputerName()
{
    char* buffer;
    DWORD dwSize = BUFFER_SIZE;
    char* cName;

    GetComputerName(buffer, &dwSize);
    printf("Computer name:      %s\n", buffer);

    // Wasn't sure if I needed to return a copy of the buffer or
    // not, so tried it to see if it would make a difference.
    strcpy(cName, buffer);
    return cName;
}
JNIEXPORT jstring JNICALL Java_ComputerInfo_getComputerName
  (JNIEnv *env, jobject obj)
{    
    jstring retValue;
    char* name;
    name = getComputerName();
    printf("name returned as:  %s", name);
    
    retValue = (*env)->NewStringUFT(env, name);
    return retValue;
}
This CRASHES ...

The output shows that getComputerName displays the computer name in the printf okay.

The JNI method shows that the name returned okay.

However when I create a jstring to be returned ... it crashes...
with an ACCESS VIOLATION.

What am I doing wrong?

Do you have to make copies of the char* buffer to return? Is something holding on to the char*? Lost in C code ... Java is so much better...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 10 2007
Added on Apr 6 2007
15 comments
867 views