1827894 Members
1617 Online
109969 Solutions
New Discussion

calling C from java

 
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