1752793 Members
6098 Online
108789 Solutions
New Discussion юеВ

calling C from java

 
SAMI AHMAD
Regular Advisor

Re: calling C from java

Hi Hoff !

i wouldnt love to make the current method work as this would open ways of interfacing.
but can you point me to a sample code using such snmp trap java calls?

thanks
Hoff
Honored Contributor

Re: calling C from java

Well, I looked around for this stuff using the following query:

http://www.google.com/search?q=java+send+snmp+trap

I'm sure this could be further refined.

SAMI AHMAD
Regular Advisor

Re: calling C from java

tHANKS Hoff I will look into it. First let me get this JNI working :)
SAMI AHMAD
Regular Advisor

Re: calling C from java

Martin !
I changed the code to use Getstringchars but I am still getting the same problem, string is not being translated..

SUNNY2$ TYPE sndsnmp_c.c;
#include "SYSTEMCALL.H"
#include
#include
#include

#define size 512
static char cmdbuffer[size];

/* main(int argc, char **argv ) */

JNIEXPORT jint JNICALL Java_SYSTEMCALL_SNMPTRAP
(JNIEnv *env, jobject obj , jstring msg) {

int i;
int sts;
char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\"";

jboolean *iscopy;
jstring ret;
jint len;
const jchar *fmt;
fmt = (*env)->GetStringChars(env, msg, iscopy);
len = (*env)->GetStringLength(env, msg);
ret = (*env)->NewString(env, fmt,len);

printf("%s","now in C routine");
printf("%s", ret);

return 0;
}

SUNNY2$ java "SYSTEMCALL"
Warning: JIT compiler "SUNCOMPILER" not found. Will use interpreter.
This is java ..calling the C routing
now in C routineh├В┬▓Ux├В┬▓U├Г `U├о┬д┬╗U0l├Е┬╕z;l├Е┬╕z6
SAMI AHMAD
Regular Advisor

Re: calling C from java

ok Iam very close to fixing this code. I am getting the correct length of the string but Iam only seeing the first character of the string , how can I capture the whole ?

jboolean *iscopy=0;
jstring ret;
jint len;
const jchar *fmt; << it wont lt me use
jstring here .
fmt = (*env)->GetStringChars(env, msg, iscopy);
len = (*env)->GetStringLength(env, msg);

printf("%d\n", len);
printf("%s\n","now in C routine");
printf("%s", fmt);



SUNNY2$ java "SYSTEMCALL"
Warning: JIT compiler "SUNCOMPILER" not found. Will use interpreter.
This is java ..calling the C routing
18 <<<<< correct string length
now in C routine
N <<<<< only the first char of msg string
which is 'NEW TEST TRAP MSGS'
RBrown_1
Trusted Contributor

Re: calling C from java

>>>>>>>>>>>
N <<<<< only the first char of msg string
<<<<<<<<<<<

I know nothing about this but I think you should run this in the debugger to see what fmt really looks like. Are these wide characters?

>>>>>>>>>>>
printf("%s", fmt);
<<<<<<<<<<<

Why no '\n' here?

GetStringLength told you the length was 18. But if the length is only 18, there may not be a terminating NULL in the string.

Seeing what the program is doing from the inside will be really useful to you.
Martin Vorlaender
Honored Contributor

Re: calling C from java

Sami,

it seems I was too sure about everything. GetStringChars returns a Unicode string. If the string contains Latin-1 characters, every second byte will be zero. That's why you only see the first character of your string.

The correct function is indeed GetStringUTFChars. That one returns a const char* that you can use in a C program (without resorting to wchar_t etc.)

Sorry for causing confusion.

cu,
Martin
SAMI AHMAD
Regular Advisor

Re: calling C from java

Hi Robert!

here is the java program thats sending the string, how do I null terminate the string? :

SUNNY2$ TYPE SND_SNMP.JAVA
class SYSTEMCALL {
public native int SNMPTRAP (String msg);
static {
System.loadLibrary("SNDSNMP_C");
}
public static void main (String[] args) {

System.out.println("This is java ..calling the C routing");
int result = new SYSTEMCALL().SNMPTRAP("NEW TEST TRAP MSGS");
}
}

SAMI AHMAD
Regular Advisor

Re: calling C from java

ok let me try GetStringUTFChars , will get back to you
SAMI AHMAD
Regular Advisor

Re: calling C from java

yes that did it, its working now !
Thanks everyone for your support .

JNIEXPORT jint JNICALL Java_SYSTEMCALL_SNMPTRAP
(JNIEnv *env, jobject obj , jstring msg) {

int i;
int sts;
char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\"";

const char *tmp = (*env)->GetStringUTFChars(env,msg,0);
char retstring[512];
strcpy(retstring,tmp);

printf("%s\n","now in C routine");
printf("%s\n", retstring);
(*env)->ReleaseStringUTFChars(env, msg, tmp);

fflush(stdout);

$ java "SYSTEMCALL"
Warning: JIT compiler "SUNCOMPILER" not found. Will use interpreter.
This is java ..calling the C routing
now in C routine
NEW TEST TRAP MSGS