Solution for Java to Call DLL with pointer or Variable Parameter

Source: Internet
Author: User

[Introduction] assume that I have a dynamic library named test. dll. One of the functions is as follows.

Enterprise Software hotspot articles

How to release Linux disk space easily breaks Windows XP boot password

 

Suppose I have a dynamic library named test. dll, and one of the functions is 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 <p = ""> <>

 

{

 

C [I] = A [I] + B [I];

 

Fprintf (FP, "% d/N", C [I]);

 

}

 

Fclose (FP );

 

Return 1;

 

}

 

This function adds the values of arrays A and B, and writes to the specified file in array C, the dimension of the array.

 

It is determined by the input parameters.

 

Question:

 

1. How to call pointer-type parameters in Java;

 

2. Can Integer Parameters be passed in directly ?;

 

3. Why is picpath pointer-type and cstring-type;

 

4. How to obtain the return values of input parameters;

 

The Java code for calling the 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); // sets the return type after the call.

 

// Construct a pointer in Java and allocate space for the pointer

 

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

 

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

 

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

 

Pointer 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, );

 

T. setparameter (1, B );

 

T. setparameter (2, c );

 

T. setparameter (3, D );

 

T. setparameter (4, STRP );

 

T. Invoke (); // call the function and execute

 

// Output and value. Note: an integer value must be stored in 4 bytes. Therefore, multiply the value before J by 4.

 

For (Int J = 0; j <= "" J ++) = "">

 

System. Out. println (C. getasint (4 * j ));

 

}

 

// Release the memory

 

A. Dispose ();

 

B. Dispose ();

 

C. Dispose ();

 

Well, this is the end of a complete call example. I just wrote the key code in the program.

 

Class, and inherit a base class, methods in the secondary base class, these are relatively simple, you can refer to other online

 

Example.

 

Answer:

 

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

 

2. integer data can be passed in directly;

 

3. jnative does not support the cstring type very well. When calling the cstring method, some exceptions are often reported, so I change the path

 

Character pointer;

 

4. input parameters as output: see the preceding example to learn how to set and output array C.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.