Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI help - dll hell!!

Reply
Thread Tools

JNI help - dll hell!!

 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-23-2004
On Mon, 23 Aug 2004 10:12:40 -0800, John McClain wrote:
> *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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
John McClain
Guest
Posts: n/a
 
      08-23-2004
*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


 
Reply With Quote
 
 
 
 
John McClain
Guest
Posts: n/a
 
      08-23-2004
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[i] + (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[i]);
}
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[i] - (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[i]);
}
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);
}

//===========================
"Gordon Beaton" <> wrote in message
news:412a280f$...
> On Mon, 23 Aug 2004 10:12:40 -0800, John McClain wrote:
> > *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
>
> --
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e



 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      08-24-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Second long argument to a JNI call on a Win32 DLL contains zero when it reaches the DLL pcarr01 Java 3 02-17-2005 09:30 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:03 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM
msvcrt.dll, msvcirt.dll, msvcrt20.dll and msvcrt40.dll, explanation please! Snoopy NZ Computing 16 08-25-2003 12:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57