jrmchen

Greenhorn
+ Follow
since Mar 26, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by jrmchen

Example of How to use JNI to let Java Call C
Including Java Send Para (no pointer) into C and Java Get Value returned from C function
[Step 1]
>Create HelloWorld.java
-------------------------------------------------------
class HelloWorld {
public native void displayHelloWorld(int mint);
public native int ReturnValue(int mint);
static {
System.loadLibrary("hello");
}

public static void main(String[] args) {
new HelloWorld().displayHelloWorld(123);
int i=new HelloWorld().ReturnValue(123);
System.out.println(i);
}
}
-------------------------------------------------------
[Step 2]
>Command Line Type
d:\jni\javac HelloWorld.java
-------------------------------------------------------
[Step 3]
>Command Line Type
d:\jni\javah HelloWorld
HelloWorld.h would be automaticly Created
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: ReturnValue
* Signature: (I)I
*/

JNIEXPORT jint JNICALL Java_HelloWorld_ReturnValue
(JNIEnv *, jobject, jint);
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
-------------------------------------------------------
[Step 4]
>Write HelloWorldImp.c file
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj,jint mint)
{
printf("My Hello world %d!\n",mint);
return;
}
JNIEXPORT jint JNICALL Java_HelloWorld_ReturnValue(JNIEnv *env, jobject obj,jint mint)
{
return mint+1;
}
-------------------------------------------------------
[Step 5]
>Make hello.dll Using VC commandline
cl -I c:\jdk1.3\include -I c:\jdk1.3\include\win32 -LD HelloWorldImp.c -FeHello.dll
Message Displayed
/dll
/implib:Hello.lib
/out:Hello.dll
HelloWorldImp.obj
Creating library Hello.lib and object Hello.exp
-------------------------------------------------------
[Step 6]
> Run HelloWorld.class
Message Displayed
My Hello world 123!
124
23 years ago
-------In C--------------
typedef struct{
int a;
int b;
} MyStru;
void MyFunction1(MyStru *p)
{
// my coding
}
int MyFunction2(int *p, long *t)
{
// my coding
return 0;
}
---------------------------------------------
How can I call these two functions in Java
I have tried JNI , but only cannot deal with such kind of function.
Can any one help me?
23 years ago
I am now using JNI to call C from Java . But some functions
need to get Structure pointer or more than one pointer value.
Can any one tell me how I can transmit pointer especially Structure Pointer to those C functions ?
I tend to tranmit a class , but failed.
Can Java function get a class as a parameter?
Thanks a lot !
23 years ago
I am now using JNI to call C from Java . But some functions
need to get Structure pointer or more than one pointer value.
Can any one tell me how I can transmit pointer especially Structure Pointer to those C functions ?
I tend to tranmit a class , but failed.
Can Java function get a class as a parameter?
Thanks a lot !
23 years ago
I am now using JNI to call C from Java . But some functions
need to get Structure pointer or more than one pointer value.
Can any one tell me how I can transmit pointer especially Structure Pointer to those C functions ?
I tend to tranmit a class , but failed.
Can Java function get a class as a parameter?
Thanks a lot !
23 years ago