JNA-depth analysis of simulated C language structure (1)

2010-10-16  来源:本站原创  分类:Java  人气:258 

Original http://blog.csdn.net/shendl/archive/2008/12/26/3599849.aspx

JNA-depth analysis of language structure simulation of C

Preface

A few days ago to write "JNA - JNI Terminator," a text description JNA framework. Finished only to find, forget to write the C language are more difficult to simulate the Struct.

Today make up this article about Struct.

How not to write C language structure simulation, it can not be really solved the problem of calling the dynamic link library.

C structure of language used is too broad.

First note that a large number of the simulation in this article written for the interface class Struct inside class.

This is not the JNA provided, but a programming practice.

Because these structures (Structure subclass), generally do not have reuse value, and therefore more convenient to write the inner class. Naturally, you can also write general class structure.

Example 3 using JNA to use Struct in C function calls

C language development

Continue to use the example 2 in the dll that VSC + + project.

Increase the use of a structure and function of the structure.

Header file add the following:

# Define MYLIBAPI extern "C" __declspec (dllexport)

struct UserStruct {

long id;

wchar_t * name;

int age;

};

MYLIBAPI void sayUser (UserStruct * pUserStruct);

JNA process

The corresponding Java program, the interface in the cases of 2

/ *

* Define a class to simulate the structure of C language

* * /

public static class UserStruct extends Structure {

public NativeLong id;

public WString name;

public int age;

}

public void sayUser (UserStruct.ByReference struct);

Java in the calling code:

UserStruct userStruct = new UserStruct ();

userStruct. id = new NativeLong (100);

userStruct. age = 30;

userStruct. name = new WString ("Shen Dong Liang");

TestDll1. INSTANCE. SayUser (userStruct);

Struct Description

Now, we have implemented in Java language structure of C simulation.

Here, we inherited the Structure class, use this class to simulate the structure of C language.

Must be noted, Structure sub-class field of public order, must be the structure of C language in the same order. Otherwise, an error!

Because, Java dll in C function call is actually a memory as a function of the parameter to dll.

Dll think that this argument is passed over the parameters of C language.

At the same time, C language structure is a strict specification, which defines the memory of the order. Therefore, JNA in the order of the variables simulated structure must not be wrong.

For example, a Struct with two int variables. Int a, int b

If the JNA in the sequence and C in the opposite order, then no error, but the result was the opposite!

Example 4 using the JNA call Struct arrays using nested C functions

If the C language structure is a complex nested structure, how to do it?

Example 3 continued on the basis of the above expansion.

C language development

Header file add the following:

struct CompanyStruct {

long id;

wchar_t * name;

UserStruct users [100];

int count;

};

MYLIBAPI void sayCompany (CompanyStruct * pCompanyStruct);

Source:

void sayCompany (CompanyStruct * pCompanyStruct) {

std:: wcout.imbue (std:: locale ("chs"));

std:: wcout <<L "ID:" <<pCompanyStruct-> id <<std:: endl;

std:: wcout <<L "Name:" <<pCompanyStruct-> name <<std:: endl;

std:: wcout <<L "total number of employees:" <<pCompanyStruct-> count <<std:: endl;

for (int i = 0; i <pCompanyStruct-> count; i + +) {

sayUser (& pCompanyStruct-> users [i]);

}

}

JNA process

Java programs, in the original interface with the following code:

public static class CompanyStruct extends Structure {

public NativeLong id;

public WString name;

public UserStruct.ByValue [] users = new UserStruct.ByValue [100];

public int count;

}

public void sayCompany (CompanyStruct pCompanyStruct);

UserStruct class of rewriting the original:

/ *

* Define a class to simulate the structure of C language

* * /

public static class UserStruct extends Structure {

public static class ByReference extends UserStruct implements Structure.ByReference {}

public static class ByValue extends UserStruct implements Structure.ByValue

{}

public NativeLong id;

public WString name;

public int age;

}

Call the JNA process:

CompanyStruct companyStruct = new CompanyStruct ();

companyStruct. id = new NativeLong (1);

companyStruct. name = new WString ("Google");

companyStruct. count = 9;

UserStruct.ByValue userStructValue = new UserStruct.ByValue ();

userStructValue. id = new NativeLong (100);

userStructValue. age = 30;

userStructValue. name = new WString ("Shen Dong Liang");

for (int i = 0; i <companyStruct. count; i + +) {

companyStruct. users [i] = userStructValue;

}

TestDll1. INSTANCE. SayCompany (companyStruct);

Help

You can see, the program correctly exported.

Readers may have some doubts.

1, why do we add this structure to give UserStruct 2 internal classes?

Look Structure class API that we know, this class has two internal interfaces:

static interface

Structure.ByReference
Tagging interface to indicate the address of an instance of the Structure type is to be used within a Structure definition rather than nesting the full Structure contents.

static interface

Structure.ByValue
Tagging interface to indicate the value of an instance of the Structure type is to be used in function invocations rather than its address.

This internal interface is marked 2, nothing inside.

At runtime, JNA implementation of the framework will use reflection see if you achieve this two interfaces, then a specific treatment.

(This technique is very popular before the java annotation Annotation. Now you can use the run-time Annotation achieve the same effect.

JNA 1999, said the project started, use this old technology is not surprising. Just wondering why no how domestic heard. I am also a chance abroad recently found on its website. Under a try, put it down, so I was confident of Java desktop applications!

)

If your implementation Struct Structure.ByReference Interface, then JNA Think your Struct is a pointer . Pointer to the C language structures .

If your implementation Struct Structure.ByValue Interface, then JNA Think your Struct is a value type , Is the c-language structures .

If you do not realize that two interfaces, then you realize Structure.ByReference Interface . equivalent

Therefore, in cases of 3 I did not realize in this 2 A interface .

2, C language, the structure within the array must be defined. Java best to do so.

C language structure is a contiguous memory, the memory size is determined at compile time.

Therefore, the array must be defined. Otherwise the compilation will not succeed.

The corresponding Java class, we should also defined as an array in the class definition. Despite the fact that the re-assignment can also be used.

However, it is strongly not recommended to do so.

For example, the above

public UserStruct.ByValue [] users = new UserStruct.ByValue [100];

Define an array of 100 elements, if you no longer inside a class definition.

The definition in use, if you do not have the correct assignment, as the 100 elements is not defined, an error occurs.

On the surface, CompanyStruct kind of memory is:

NativeLong id

WString name;

相关文章
  • JNA-depth analysis of simulated C language structure (1) 2010-10-16

    Original http://blog.csdn.net/shendl/archive/2008/12/26/3599849.aspx JNA-depth analysis of language structure simulation of C Preface A few days ago to write "JNA - JNI Terminator," a text description JNA framework. Finished only to find, forget

  • Paste JNA-JNI Terminator. JNA-depth analysis of simulated C-structure the complete source code for two articles 2010-10-16

    Original http://blog.csdn.net/shendl/archive/2008/12/26/3599854.aspx Terminator paste JNA-JNI, JNA-depth analysis of simulated C-structure the complete source code for two articles C language source code: Header # Define MYLIBAPI extern "C" __de

  • Memcached depth analysis of the (transfer) 2010-09-11

    Memcached depth analysis of the (transfer) February 11, 2009 Read Comments comment Memcached is danga.com (operating LiveJournal's engineering team) development of a distributed memory object caching system, used in the dynamic system to reduce datab

  • JNA-depth analysis of simulated C-structure (2) 2010-10-16

    Original http://blog.csdn.net/shendl/archive/2008/12/26/3599849.aspx 2, C language, the structure within the array must be defined. Java best to do so. C language structure is a contiguous memory, the memory size is determined at compile time. Theref

  • c understanding of language structure (share) 2011-05-27

    [Knowledge points] One. Structure type Structure is a data type, you can use the structure variable, therefore, like other types of variables, structural variables in the use of its definition first. Defines the general format of the structure variab

  • ORACLE database query language SQL language structure (a) 2010-12-30

    ORACLE database query language SQL language structure (a) First, the concept description: Database DATABASE, table TABLE, column COLUMN, line ROW, keyword PRIMARY KEY, index INDEX Second, the type of the column: the character CHAR and VARCHAR2, value

  • Depth analysis on the Internet of Things. Pdf 2010-07-13

    Depth analysis on the Internet of Things. Pdf

  • [Transfer] MySQL garbled depth analysis 2010-09-29

    MySQL garbled depth analysis I. Overview The company purchased a number of new PC, for better performance of the PC to a few database server upgrade, replace old machines. POS terminal software companies have set, background data storage is a MySQL 3

  • the exchange value of two variables php php list the usage of language structure 2010-11-01

    php exchange value of two variables: <?php $var1 = 'http://www.k686.com/'; $var2 = 'k686 Green software '; list($var1,$var2) = array($var2,$var1); echo $var1,$var2; ?> The output is: k686 green software http://www.k686.com/ list (PHP 3, PHP 4, PHP 5

  • Depth analysis of the linux system. Htaccess file for more syntax to use tutorial 2011-08-27

    Depth analysis of the linux system. Htaccess file for more syntax to use tutorial Implementation conditions grammar rewrite rules RewriteCond: RewriteCond TestString CondPattern Force field: server config, virtual host, directory,. Htaccess Particula

  • Paste JNA-JNI Terminator. JNA-depth analysis of simulated two articles in C language structure the complete source code (continued) 2010-10-16

    Original http://blog.csdn.net/shendl/archive/2008/12/26/3599854.aspx / ** * @ Param args * / public static void main (String [] args) { / / TODO Auto-generated method stub TestDll1. INSTANCE. Say (new WString ("Hello World!")); System. Out. Prin

  • Memcached depth analysis (reproduced) 2010-03-31

    Memcached is danga.com (operating LiveJournal's engineering team) development of a distributed memory object caching system, used in the dynamic system to reduce database load and improve performance. On this thing, I think many people have used, thi

  • Depth analysis of the Oracle database log files 2010-04-29

    Source: ccidnet.com of: Cheng Yong Xin Time :2006-10-06 As Oracle DBA, we sometimes need to track user data from deletion or malicious operating conditions, then we need not only to identify the database to perform these operations account, also need

  • Memcached depth analysis 2010-07-09

    Memcached is danga.com (operating LiveJournal's engineering team) development of a distributed memory object caching system, used in the dynamic system to reduce database load and improve performance. On this thing, I believe many people have used, t

  • Memcached depth analysis (to) 2010-12-10

    http://www.srcsky.com/kaiyuan/category/123 Memcached is danga.com (operating LiveJournal's engineering team) developed a set of distributed memory object caching system, used in the dynamic system to reduce database load and improve performance. On t

  • Depth analysis of the IOC in Spring framework to achieve the principle (a) 2011-01-02

    The basis of IOC Here we IOC / AOP start, they are the core part of the Spring platform; Although we are mostly just in the beginning level, to do some configuration and the use of external characteristics, but these two core principles and operation

  • JAVA depth analysis 3 (51CTO) 2010-03-30

    1. Java on XML, XML parsing believe people are not familiar, the meaning is extensible markup language. In itself it is a data carrier in the form of tree performance. Then slowly the data into information, information on the difference between a sta

  • [Java performance analysis] JPDA 1) the overall structure 2010-04-27

    JPDA (Java Platform Debugger Architecture) provides a JVM internal access interface, so that we can easily understand the JVM internal state, can be built on the basis of debugging, performance analysis platform. JPDA includes two interface definitio

  • Depth analysis of vertical search engines 2010-11-30

    1. What is vertical search engine The so-called vertical search engines (Vertical Search Engine), is tied to a specific area of a particular population or a particular value needs to have some information and related services. The feature is "special

  • Depth analysis of the iBATIS framework of the principles of system architecture and mapping 2011-05-07

    Original: http://www.ibm.com/developerworks/cn/java/j-lo-ibatis-principle/ iBATIS SQL Map to Java object by mapping the result set of SQL statements and converted into Java objects, compared to other ORM frameworks (iBATIS is not strictly true ORM),