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...