Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How does java call dll pass in pointer? How to get the value of the pointer after calling?
How does java call dll pass in pointer? How to get the value of the pointer after calling?
Suppose I have a dynamic library named test.dll, and its functions are as follows:

extern "C" int add(int* a,int* b,int* c,int d,char* PicPath)

{

FILE * fp

fp=fopen(PicPath," wt ");

for(int I = 0; I

{

c[I]= a[I]+b[I];

fprintf(fp," %d\n ",c[I]);

}

fclose(FP);

Returns1;

}

The function of this function is to add the values of two arrays A and B, and in the stored array C, write and into the specified file, the dimension of the array.

A parameter was passed in.

Questions raised:

How to call the parameter of pointer type in 1.Java;

2. Can plastic parameters be transmitted directly? ;

3. Why 3? PicPath pointer type and why not CString type?

4. How to get the return value of input parameters;

The code of Java calling dynamic link library is as follows:

JNative t= new JNative("test.dll "," add "); //Load the test.dll file and call the add method;

T.setRetVal (type. INT); //Set the return type after calling.

//Use Java to construct pointers and allocate space for pointers.

Pointer a = new pointer (memoryblockfactory.creatememoryblock (4 *10));

Pointer b = new pointer (memoryblockfactory.creatememoryblock (4 *10));

Pointer c = new pointer (memoryblockfactory.creatememoryblock (4 *10));

Strp = createpointer (100);

strP.setStringAt(0," C:\ \ 1 . txt));

int d = 10;

//assign values to arrays a and B.

for(int I = 0; I

a.setIntAt(4 * i,I);

b.setIntAt(4 * i,I);

}

//Set parameters:

t.setParameter(0,a);

t.setParameter( 1,b);

t.setParameter(2,c);

t.setParameter(3,d);

t.setParameter(4,strP);

t . invoke(); //Call the function and execute it

//output the value of sum. Note: an integer value needs to store 4 bytes, so j needs to be multiplied by 4.

for(int j = 0; j

system . out . println(c . getas int(4 * j));

}

//Free memory

a . dispose();

b . dispose();

c . dispose();

Well, such a complete calling example is over. I just wrote the key code in the program, and I need to write another one.

Class, and inherit a base class, in the method of copying the base class, these are relatively simple, you can refer to other online.

Examples.

Answer to the question:

Call the pointer type parameter in 1. Java: You need to create a pointer variable and allocate space for it.

2. The shaped data can be directly transmitted;

3. jnnative doesn't support CString type very well. When calling the CString method, some exceptions are often reported, so I changed the path to.

Character pointer of;

4. Input parameters as output: You can see the above example, how the array C is set and output.