JNI help - dll hell!!

G

Gordon Beaton

*my error environment is a jsp webapp running on tomcat5.0, through
IE5.0

*I have created a dll with 2 functions in Borland C++builder 6.0
*I have a java class where I call system.loadLibrary("<dllname>") and
declare the functions native
*I have a main in my class where I test the invocation and return values
from the dll functions
* THE MAIN IS COMPLETELY SUCCESSFULL - JNI WORKS!!
* however - when I call the functions from a servlet that is invoked from a
browser, I get an EXCEPTION_FLT_STACK_CHECK error

The fact that you get the expected results in a different situation is
no guarantee that the code is correct, and my suspicion is that you
have a pointer error of some kind in your native code (in fact it is
typical of pointer errors that the code appears to work correctly in
some situations but crashes in others). Without actually seeing the
code though, I can't be more specific.

Do you get the error if you call *any* methods in the native
library, or just one specfic method?
*So - it seems that just loading the dll causes a system error BUT
only if we are running through a servlet. Is there some windows
library conflict???

I have no idea. Make a trivial ("hello world") DLL and see if that
gives you any problems when you use it from a servlet.

/gordon
 
J

John McClain

*my error environment is a jsp webapp running on tomcat5.0, through IE5.0

*I have created a dll with 2 functions in Borland C++builder 6.0
*I have a java class where I call system.loadLibrary("<dllname>") and
declare the functions native
*I have a main in my class where I test the invocation and return values
from the dll functions
* THE MAIN IS COMPLETELY SUCCESSFULL - JNI WORKS!!
* however - when I call the functions from a servlet that is invoked from a
browser, I get an EXCEPTION_FLT_STACK_CHECK error
* I mimicked the functions in java and called them instead of the dll's
*IT STILL FAILED WITH THE SAME ERROR - BUT, I realized that I had not
commented out the
static
{
System.loadLibrary("<dllname>");
}
* When I commented this out (I did not even comment out the function
declarations), the call to my
mimicked dll functions worked fine.
*So - it seems that just loading the dll causes a system error BUT only if
we are running through a servlet. Is there some
windows library conflict???

*Can someone explain this and give me an answer how to use JNI in a JSP
webapp running on Tomcat
 
J

John McClain

Here is my C++ code, If you see anything here that looks suspicious, please
let me know - I can't see it...

//=============================
//--------------------------------------------------------------------------
-
#include "TCSCryptography.h"
#include <stdlib.h>

char strKey[5] = {'A', 'D', 'D', 'I', '\0'};
int lenStrKey = 4;

//--------------------------------------------------------------------------
-
//Function: Encrypt
//parms:
// env - pointer to JNI class having all needed JNI methods
// obj - the calling class instance
// plainTextStr - the string to encrypt
//returns:
// jcharArray - because we CANT return a string - the resulting
encryption
// could insert embedded nulls - this causes all sorts of
// problems. The first 5 chars of the array
// are the length of the encrypted string (same as the
length of the
// original string)
//--------------------------------------------------------------------------
-
JNIEXPORT jbyteArray JNICALL Java_tcsLic_TCSCryptography_Encrypt(JNIEnv
*env, jclass obj, jstring plainTextStr)
{
jbyte buf[2000];
char tmpLenBuf[6];

/*Put the string to encrypt into a char * and get its length*/
const char *str = env->GetStringUTFChars(plainTextStr, 0);
int lenStr = env->GetStringUTFLength(plainTextStr);

/*Put length of string into first 5 chars of buffer*/
sprintf(tmpLenBuf, "%05d", lenStr);
buf[0] = tmpLenBuf[0];
buf[1] = tmpLenBuf[1];
buf[2] = tmpLenBuf[2];
buf[3] = tmpLenBuf[3];
buf[4] = tmpLenBuf[4];

/*Encrypt the string and fill in the buffer after the length*/
int i,j,iTmp;
for (i = 0, j = 5 ; i< lenStr; i++, j++)
{
/*Add each consecutive element of key to each consecutive element of
plaintext input data*/
iTmp = (int)str + (int)strKey[i%lenStrKey];
/*Insure that we are within the range of printable chars*/
iTmp %= 127;
buf[j] = (char)iTmp;
}

/************** DEBUG ************************/
printf("i is %d, j is %d, ENCRYPT --- key = %s\n, keylen = %d\n, param -
plainTextStr = %s\n, plainTextStrLen = %d\n, encstr = ", i,j, strKey,
lenStrKey, str, lenStr);
for (i = 0; i< lenStr + 5; i++)
{
printf("%c", buf);
}
printf("\n***************************\n");
/**********************************************/

/*, clean up, create a jbyteArray of buf size, fill it with the buffer and
return it*/

jbyteArray jBuf = env->NewByteArray(j);
env->SetByteArrayRegion(jBuf, 0, j, buf);
env->ReleaseStringUTFChars(plainTextStr, str);
return jBuf;

}

//--------------------------------------------------------------------------
-
//Function: Decrypt
//parms:
// env - pointer to JNI class having all needed JNI methods
// obj - the calling class instance
// encryptedCharAr - the char array to decrypt
//returns:
// jstring - because what we want from decryption is the original
string
//--------------------------------------------------------------------------
-
JNIEXPORT jstring JNICALL Java_tcsLic_TCSCryptography_Decrypt(JNIEnv *env,
jclass obj, jbyteArray encryptedCharAr)
{
char buf[2000];

jbyte* tmpEncryptedCharAr = env->GetByteArrayElements(encryptedCharAr, 0);
/*Get the length of the encrypted string from the first 5 chars*/
/* I could not get strncpy to work with tmpEncryptedCharAr*/
char lentmpEncryptedCharAr[6];
lentmpEncryptedCharAr[0] = tmpEncryptedCharAr[0];
lentmpEncryptedCharAr[1] = tmpEncryptedCharAr[1];
lentmpEncryptedCharAr[2] = tmpEncryptedCharAr[2];
lentmpEncryptedCharAr[3] = tmpEncryptedCharAr[3];
lentmpEncryptedCharAr[4] = tmpEncryptedCharAr[4];
lentmpEncryptedCharAr[5] = '\0';
int lenStr = atoi(lentmpEncryptedCharAr);

/*decrypt the string and fill the buffer*/
int i,j, iTmp;
for (i = 5, j = 0; j < lenStr; i++, j++)
{
/*Subtract each consecutive element of key from each consecutive element
of encrypted input data*/
iTmp = (int)tmpEncryptedCharAr - (int)strKey[j%lenStrKey];
/*Insure that we are within the range of printable chars*/
if (iTmp < 0)
iTmp+= 127;
buf[j] = (char)iTmp;
}
buf[j] = '\0';

/************** DEBUG ************************/
printf("DECRYPT --- key = %s\n, keylen = %d\n, param - encryptedCharAr =
", strKey, lenStrKey );
for (i = 0; i< lenStr + 5; i++)
{
printf("%c", tmpEncryptedCharAr);
}
printf("\n");
printf(" encryptedCharArLen = %d\n, decstr =
%s\n****************************\n",lenStr, buf);
/*********************************************/

/*Cleanup, and pass back decryted string*/
env->ReleaseByteArrayElements(encryptedCharAr, tmpEncryptedCharAr,0);
return env->NewStringUTF(buf);
}

//===========================
 
C

Chris Uppal

John McClain wrote:

jbyte buf[2000]; ....
int i,j,iTmp;
for (i = 0, j = 5 ; i< lenStr; i++, j++)
{ ....
buf[j] = (char)iTmp;
}


This may not be the cause of the problem that you are seeing, but it will
corrupt your stack if lenStr >= (2000 - 5)

-- chris
 

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

Similar Threads

JNI for native DLL 0
JNI Invocation API example 4
JNI C++ Wrapper 1
Using JNI Produced Jar Files: java.lang.UnsatisfiedLinkError 5
Java Applet and JNI 3
Help 0
JNI Problems with MINGW32 2
JNI return jobjectArray 7

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top